From 5c5487f45d606f2756c393ef6fee12617010386d Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 17 Jun 2014 19:19:39 -0400 Subject: [PATCH] Allow scripts to go through the readline backend As a stop gap measure, the readline backend was by-passed during script execution. This changes the prompt code to allow scripts to interact with the readline code by decoding the symbolic keys and serving them to readline as bytes. Since the script executing is mainly targeting towards the test suite this will make it possible to test the readline backend. However, it does cause script behavior to act differently based on whether readline is supported or not. --- include/tig/display.h | 2 +- src/display.c | 27 ++++++++++++++++++++++++++- src/prompt.c | 5 +---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/tig/display.h b/include/tig/display.h index 2ba25a9..9a6dda6 100644 --- a/include/tig/display.h +++ b/include/tig/display.h @@ -18,9 +18,9 @@ #include "tig/keys.h" int get_input(int prompt_position, struct key *key, bool modifiers); +int get_input_char(void); extern WINDOW *status_win; -extern FILE *opt_tty; void update_status(const char *msg, ...); void report(const char *msg, ...) PRINTF_LIKE(1, 2); diff --git a/src/display.c b/src/display.c index b2f01c1..89c1657 100644 --- a/src/display.c +++ b/src/display.c @@ -28,7 +28,7 @@ static WINDOW *display_win[2]; static WINDOW *display_title[2]; static WINDOW *display_sep; -FILE *opt_tty; +static FILE *opt_tty; bool open_external_viewer(const char *argv[], const char *dir, bool confirm, bool refresh, const char *notice) @@ -510,6 +510,31 @@ read_script(struct key *key, int delay) } int +get_input_char(void) +{ + if (is_script_executing()) { + static struct key key; + static int bytes_pos; + + if (!key.modifiers.multibytes || bytes_pos >= strlen(key.data.bytes)) { + if (!read_script(&key, 0)) + return 0; + bytes_pos = 0; + } + + if (!key.modifiers.multibytes) { + if (key.data.value < 128) + return key.data.value; + die("Only ASCII control characters can be used in prompts: %d", key.data.value); + } + + return key.data.bytes[bytes_pos++]; + } + + return getc(opt_tty); +} + +int get_input(int prompt_position, struct key *key, bool modifiers) { struct view *view; diff --git a/src/prompt.c b/src/prompt.c index e96deb3..7f642f7 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -360,7 +360,7 @@ readline_toggle_generator(const char *text, int state) static int readline_getc(FILE *stream) { - return getc(opt_tty); + return get_input_char(); } static char ** @@ -444,9 +444,6 @@ read_prompt(const char *prompt) { static char *line = NULL; - if (is_script_executing()) - return read_prompt_incremental(prompt, TRUE, NULL, NULL); - if (line) { free(line); line = NULL; -- 2.11.4.GIT