From 381d884c16831965b9714d948ee79e8a72d7f417 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 24 Jan 2012 23:18:59 +0000 Subject: [PATCH] use a settable get_key function to be able to override key reading --- src/main.cc | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main.cc b/src/main.cc index 98d716bf..bc3fc4ae 100644 --- a/src/main.cc +++ b/src/main.cc @@ -163,7 +163,7 @@ void draw_window(Window& window) move(cursor_position.line, cursor_position.column); } -Key get_key() +Key ncurses_get_key() { char c = getch(); @@ -298,13 +298,21 @@ std::string ncurses_prompt(const std::string& text, Completer completer = comple return result; } -static std::function prompt_func = ncurses_prompt; +static std::function prompt_func = ncurses_prompt; std::string prompt(const std::string& text, Completer completer = complete_nothing) { return prompt_func(text, completer); } +static std::function get_key_func = ncurses_get_key; + +Key get_key() +{ + return get_key_func(); +} + + void print_status(const std::string& status) { int x,y; @@ -426,8 +434,11 @@ void do_go(Window& window, int count) } else { - char c = getch(); - switch (c) + Key key = get_key(); + if (key.modifiers != Key::Modifiers::None) + return; + + switch (key.key) { case 'g': case 't': @@ -973,6 +984,14 @@ void exec_string(const CommandParameters& params, 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) { size_t begin = pos; while (pos < keys.size() and keys[pos].key != '\n') @@ -986,7 +1005,11 @@ void exec_string(const CommandParameters& params, 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; while(pos < keys.size())