From 6e2fa38c15e7e1bdb50498697b22a195782a4018 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 4 Apr 2013 19:03:59 +0200 Subject: [PATCH] InputHandler::prompt takes a color pair to use --- README.asciidoc | 1 + src/color_registry.cc | 1 + src/input_handler.cc | 24 ++++++++++++++++-------- src/input_handler.hh | 4 ++-- src/main.cc | 15 ++++++++------- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index b446bf7bf..8836fb9c0 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -411,6 +411,7 @@ there are some builtins color aliases: * +Error+: colors of error messages * +StatusLine+: colors used for the status line * +StatusCursor+: colors used for the status line cursor + * +Prompt+: colors used prompt displayed on the status line Shell expansion --------------- diff --git a/src/color_registry.cc b/src/color_registry.cc index b30afb284..b0add9de0 100644 --- a/src/color_registry.cc +++ b/src/color_registry.cc @@ -52,6 +52,7 @@ ColorRegistry::ColorRegistry() { "Error", { Color::Black, Color::Red } }, { "StatusLine", { Color::Cyan, Color::Default } }, { "StatusCursor", { Color::Black, Color::Cyan } }, + { "Prompt", { Color::Cyan, Color::Blue} }, } {} diff --git a/src/input_handler.cc b/src/input_handler.cc index cb9cabbf4..1222b3799 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -286,12 +286,12 @@ class Prompt : public InputMode { public: Prompt(InputHandler& input_handler, const String& prompt, - Completer completer, PromptCallback callback) - : InputMode(input_handler), m_prompt(prompt), + ColorPair colors, Completer completer, PromptCallback callback) + : InputMode(input_handler), m_prompt(prompt), m_prompt_colors(colors), m_completer(completer), m_callback(callback) { m_history_it = ms_history[m_prompt].end(); - context().ui().print_status(line_with_cursor(m_prompt, m_prompt.char_length())); + display(); } void on_key(const Key& key) override @@ -429,15 +429,22 @@ public: m_current_completion = -1; m_line_editor.handle_key(key); } - auto curpos = m_prompt.char_length() + m_line_editor.cursor_pos(); - context().ui().print_status(line_with_cursor(m_prompt + line, curpos)); + display(); m_callback(line, PromptEvent::Change, context()); } private: + void display() const + { + auto display_line = line_with_cursor(m_line_editor.line(), m_line_editor.cursor_pos()); + display_line.insert(display_line.begin(), { m_prompt, m_prompt_colors }); + context().ui().print_status(display_line); + } + PromptCallback m_callback; Completer m_completer; const String m_prompt; + ColorPair m_prompt_colors; Completions m_completions; int m_current_completion = -1; String m_prefix; @@ -784,11 +791,12 @@ void InputHandler::repeat_last_insert() assert(dynamic_cast(m_mode.get()) != nullptr); } -void InputHandler::prompt(const String& prompt, Completer completer, - PromptCallback callback) +void InputHandler::prompt(const String& prompt, ColorPair prompt_colors, + Completer completer, PromptCallback callback) { m_mode_trash.emplace_back(std::move(m_mode)); - m_mode.reset(new InputModes::Prompt(*this, prompt, completer, callback)); + m_mode.reset(new InputModes::Prompt(*this, prompt, prompt_colors, + completer, callback)); } void InputHandler::menu(const memoryview& choices, diff --git a/src/input_handler.hh b/src/input_handler.hh index ad93ef9f3..d45097525 100644 --- a/src/input_handler.hh +++ b/src/input_handler.hh @@ -47,8 +47,8 @@ public: // abort or validation with corresponding PromptEvent value // returns to normal mode after validation if callback does // not change the mode itself - void prompt(const String& prompt, Completer completer, - PromptCallback callback); + void prompt(const String& prompt, ColorPair prompt_colors, + Completer completer, PromptCallback callback); // enter menu mode, callback is called on each selection change, // abort or validation with corresponding MenuEvent value diff --git a/src/main.cc b/src/main.cc index 932fad61e..dd2f7f1b6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -145,7 +145,8 @@ void do_swap_case(Context& context) void do_command(Context& context) { context.input_handler().prompt( - ":", std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3), + ":", get_color("StatusLine"), + std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3), [](const String& cmdline, PromptEvent event, Context& context) { if (event == PromptEvent::Validate) CommandManager::instance().execute(cmdline, context); @@ -154,7 +155,7 @@ void do_command(Context& context) void do_pipe(Context& context) { - context.input_handler().prompt("|", complete_nothing, + context.input_handler().prompt("|", get_color("StatusLine"), complete_nothing, [](const String& cmdline, PromptEvent event, Context& context) { if (event != PromptEvent::Validate) @@ -174,7 +175,7 @@ template void do_search(Context& context) { SelectionList selections = context.editor().selections(); - context.input_handler().prompt("/", complete_nothing, + context.input_handler().prompt("/", get_color("StatusLine"), complete_nothing, [selections](const String& str, PromptEvent event, Context& context) { try { @@ -313,7 +314,7 @@ void do_paste(Context& context) void do_select_regex(Context& context) { - context.input_handler().prompt("select: ", complete_nothing, + context.input_handler().prompt("select: ", get_color("Prompt"), complete_nothing, [](const String& str, PromptEvent event, Context& context) { if (event == PromptEvent::Validate) { @@ -330,7 +331,7 @@ void do_select_regex(Context& context) void do_split_regex(Context& context) { - context.input_handler().prompt("split: ", complete_nothing, + context.input_handler().prompt("split: ", get_color("Prompt"), complete_nothing, [](const String& str, PromptEvent event, Context& context) { if (event == PromptEvent::Validate) { @@ -375,7 +376,7 @@ template void do_keep(Context& context) { constexpr const char* prompt = matching ? "keep matching: " : "keep not matching: "; - context.input_handler().prompt(prompt, complete_nothing, + context.input_handler().prompt(prompt, get_color("Prompt"), complete_nothing, [](const String& str, PromptEvent event, Context& context) { if (event == PromptEvent::Validate) { @@ -484,7 +485,7 @@ void do_rotate_selections(Context& context) if (count == 0) count = 1; context.editor().rotate_selections(count); -}; +} enum class SelectFlags {