xaizek / libvle (License: GPLv3+) (since 2019-04-21)
Library for building Vim-like applications.
<root> / Mode.hpp (e73eed3c13a37a9842a66b4363b235221a7a74f1) (3,181B) (mode 100644) [raw]
// libvle -- library for building Vim-like applications
// Copyright (C) 2019 xaizek <xaizek@posteo.net>
//
// This file is part of libvle.
//
// libvle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libvle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libvle.  If not, see <https://www.gnu.org/licenses/>.

#ifndef LIBVLE__MODE_HPP__
#define LIBVLE__MODE_HPP__

#include <deque>
#include <functional>
#include <string>
#include <unordered_set>
#include <vector>

namespace vle {

// Single shortcut.
struct Shortcut
{
    // Canonical (the most complete) type of handler.  `count` is negative when
    // it's not available.
    using handler_t = void(int count);

    std::vector<std::wstring> shortcuts; // Shortcuts.
    std::function<handler_t> handler;    // Handler to be called on match.
    std::string descr;                   // Description of the shortcut.

    // Creates a shortcut that doesn't use count.
    Shortcut(std::wstring shortcut, std::function<void(void)> handler,
             std::string descr);
    // Creates a shortcut that uses count.
    Shortcut(std::wstring shortcut, std::function<void(int)> handler,
             std::string descr);
    // Creates a set of shortcuts that don't use count.
    Shortcut(std::vector<std::wstring> shortcuts,
             std::function<void(void)> handler, std::string descr);
    // Creates a set of shortcuts that use count.
    Shortcut(std::vector<std::wstring> shortcuts,
             std::function<void(int)> handler, std::string descr);
};

// Contains mode configuration.
class Mode
{
public:
    // Initializes empty mode with the specified id.
    explicit Mode(std::string id);

    Mode(const Mode &rhs) = delete;
    Mode(Mode &&rhs) = default;
    Mode & operator=(const Mode &rhs) = delete;
    Mode & operator=(Mode &&rhs) = default;

public:
    // Retrieves id of the mode.
    const std::string & getId() const
    { return id; }

    // Sets whether count is used by shortcuts.
    void setUsesCount(bool uses)
    { useCount = uses; }
    // Checks whether count is used by shortcuts.
    bool usesCount() const
    { return useCount; }

    // Registers a shortcut.  Throws `std::invalid_argument` if shortcut is too
    // long or a duplicate.
    void addShortcut(Shortcut shortcut);
    // Retrieves shortcuts of this mode.
    const std::deque<Shortcut> & getShortcuts() const
    { return shortcuts; }

private:
    // Identifier of the mode.
    std::string id;
    // Known hortcut values of this mode.
    std::unordered_set<std::wstring> seenShortcuts;
    // Shortcuts of this mode.
    std::deque<Shortcut> shortcuts;
    // Whether count is used by shortcuts.
    bool useCount;
};

}

#endif // LIBVLE__MODE_HPP__
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/libvle

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/libvle

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master