InputHandler::prompt takes a color pair to use

This commit is contained in:
Maxime Coste 2013-04-04 19:03:59 +02:00
parent f540566b1b
commit 6e2fa38c15
5 changed files with 28 additions and 17 deletions

View File

@ -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
---------------

View File

@ -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} },
}
{}

View File

@ -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<InputModes::Normal*>(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<String>& choices,

View File

@ -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

View File

@ -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<SelectMode mode, bool forward>
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<bool matching>
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
{