From 63191f1900fe805db49654605ffe4eba9fc547b9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 16 Sep 2011 09:18:51 +0000 Subject: CommandManager: support per command configurable completion --- src/command_manager.hh | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'src/command_manager.hh') diff --git a/src/command_manager.hh b/src/command_manager.hh index e46c5a77..037ec2c9 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -5,6 +5,7 @@ #include #include #include +#include #include "exception.hh" #include "completion.hh" @@ -20,17 +21,53 @@ struct wrong_argument_count : runtime_error typedef std::vector CommandParameters; typedef std::function Command; +typedef std::function CommandCompleter; + +class PerArgumentCommandCompleter +{ +public: + typedef std::function ArgumentCompleter; + typedef std::vector ArgumentCompleterList; + + PerArgumentCommandCompleter(const ArgumentCompleterList& completers) + : m_completers(completers) {} + + PerArgumentCommandCompleter(ArgumentCompleterList&& completers) + : m_completers(completers) {} + + PerArgumentCommandCompleter(std::initializer_list completers) + : m_completers(completers) {} + + CandidateList operator()(const CommandParameters& params, + size_t token_to_complete, + size_t pos_in_token) const; + +private: + ArgumentCompleterList m_completers; +}; + class CommandManager { public: void execute(const std::string& command_line); Completions complete(const std::string& command_line, size_t cursor_pos); - void register_command(const std::string& command_name, Command command); - void register_command(const std::vector& command_names, Command command); + void register_command(const std::string& command_name, + Command command, + const CommandCompleter& completer = CommandCompleter()); + + void register_command(const std::vector& command_names, + Command command, + const CommandCompleter& completer = CommandCompleter()); private: - std::unordered_map m_commands; + struct CommandAndCompleter + { + Command command; + CommandCompleter completer; + }; + std::unordered_map m_commands; }; } -- cgit v1.2.3