From 9bb9eda302ba20ce875ba702003458807da2003e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 4 Apr 2013 19:09:34 +0200 Subject: [PATCH] Dynamically change prompt color for regex prompt depending on regex validity --- src/input_handler.cc | 16 ++++++++++++++++ src/input_handler.hh | 1 + src/main.cc | 15 +++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/input_handler.cc b/src/input_handler.cc index 1222b379..a8b161cb 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -433,6 +433,15 @@ public: m_callback(line, PromptEvent::Change, context()); } + void set_prompt_colors(ColorPair colors) + { + if (colors != m_prompt_colors) + { + m_prompt_colors = colors; + display(); + } + } + private: void display() const { @@ -799,6 +808,13 @@ void InputHandler::prompt(const String& prompt, ColorPair prompt_colors, completer, callback)); } +void InputHandler::set_prompt_colors(ColorPair prompt_colors) +{ + InputModes::Prompt* prompt = dynamic_cast(m_mode.get()); + if (prompt) + prompt->set_prompt_colors(prompt_colors); +} + void InputHandler::menu(const memoryview& choices, MenuCallback callback) { diff --git a/src/input_handler.hh b/src/input_handler.hh index d4509752..7b2d771f 100644 --- a/src/input_handler.hh +++ b/src/input_handler.hh @@ -49,6 +49,7 @@ public: // not change the mode itself void prompt(const String& prompt, ColorPair prompt_colors, Completer completer, PromptCallback callback); + void set_prompt_colors(ColorPair prompt_colors); // 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 dd2f7f1b..cd89515d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -326,6 +326,11 @@ void do_select_regex(Context& context) if (not ex.empty()) context.editor().multi_select(std::bind(select_all_matches, _1, ex)); } + else if (event == PromptEvent::Change) + { + const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0; + context.input_handler().set_prompt_colors(get_color(ok ? "Prompt" : "Error")); + } }); } @@ -343,6 +348,11 @@ void do_split_regex(Context& context) if (not ex.empty()) context.editor().multi_select(std::bind(split_selection, _1, ex)); } + else if (event == PromptEvent::Change) + { + const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0; + context.input_handler().set_prompt_colors(get_color(ok ? "Prompt" : "Error")); + } }); } @@ -393,6 +403,11 @@ void do_keep(Context& context) throw runtime_error("no selections remaining"); editor.select(std::move(keep)); } + else if (event == PromptEvent::Change) + { + const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0; + context.input_handler().set_prompt_colors(get_color(ok ? "Prompt" : "Error")); + } }); }