use a settable get_key function to be able to override key reading

This commit is contained in:
Maxime Coste 2012-01-24 23:18:59 +00:00
parent e0b216d576
commit 381d884c16

View File

@ -163,7 +163,7 @@ void draw_window(Window& window)
move(cursor_position.line, cursor_position.column); move(cursor_position.line, cursor_position.column);
} }
Key get_key() Key ncurses_get_key()
{ {
char c = getch(); char c = getch();
@ -298,13 +298,21 @@ std::string ncurses_prompt(const std::string& text, Completer completer = comple
return result; return result;
} }
static std::function<std::string (const std::string&, Completer)> prompt_func = ncurses_prompt; static std::function<std::string (const std::string&, Completer)> prompt_func = ncurses_prompt;
std::string prompt(const std::string& text, Completer completer = complete_nothing) std::string prompt(const std::string& text, Completer completer = complete_nothing)
{ {
return prompt_func(text, completer); return prompt_func(text, completer);
} }
static std::function<Key ()> get_key_func = ncurses_get_key;
Key get_key()
{
return get_key_func();
}
void print_status(const std::string& status) void print_status(const std::string& status)
{ {
int x,y; int x,y;
@ -426,8 +434,11 @@ void do_go(Window& window, int count)
} }
else else
{ {
char c = getch(); Key key = get_key();
switch (c) if (key.modifiers != Key::Modifiers::None)
return;
switch (key.key)
{ {
case 'g': case 'g':
case 't': case 't':
@ -973,6 +984,14 @@ void exec_string(const CommandParameters& params,
KeyList keys = parse_keys(params[0]); KeyList keys = parse_keys(params[0]);
auto prompt_save = prompt_func;
auto get_key_save = get_key_func;
auto restore_funcs = on_scope_end([&]() {
prompt_func = prompt_save;
get_key_func = get_key_save;
});
prompt_func = [&](const std::string&, Completer) { prompt_func = [&](const std::string&, Completer) {
size_t begin = pos; size_t begin = pos;
while (pos < keys.size() and keys[pos].key != '\n') while (pos < keys.size() and keys[pos].key != '\n')
@ -986,7 +1005,11 @@ void exec_string(const CommandParameters& params,
return result; return result;
}; };
auto restore_prompt = on_scope_end([&]() { prompt_func = ncurses_prompt; }); get_key_func = [&]() {
if (pos >= keys.size())
throw runtime_error("no more characters");
return keys[pos++];
};
int count = 0; int count = 0;
while(pos < keys.size()) while(pos < keys.size())