diff options
author | John Hawthorn <john.hawthorn@gmail.com> | 2016-06-19 23:58:44 -0700 |
---|---|---|
committer | John Hawthorn <john.hawthorn@gmail.com> | 2016-06-20 23:31:07 -0700 |
commit | 1852e205670b6cb959a5b17604e8cd27e38bdb9e (patch) | |
tree | 58c45a1d51676464b9d6d01ce35e0b94142993c3 | |
parent | d7ee7efb6e33fe85fc19c922fb7c444d4ef8dcc9 (diff) |
Track last search
This allows us to avoid explicitly calling out to choices_search
-rw-r--r-- | src/tty_interface.c | 22 | ||||
-rw-r--r-- | src/tty_interface.h | 1 |
2 files changed, 17 insertions, 6 deletions
diff --git a/src/tty_interface.c b/src/tty_interface.c index 00f665a..bffc874 100644 --- a/src/tty_interface.c +++ b/src/tty_interface.c @@ -110,15 +110,30 @@ static void action_emit(tty_interface_t *state) { #define KEY_DEL 127 #define KEY_ESC 27 +static void update_search(tty_interface_t *state) { + choices_search(state->choices, state->search); + strcpy(state->last_search, state->search); +} + +void update_state(tty_interface_t *state) { + if (strcmp(state->last_search, state->search)) + update_search(state); +} + void tty_interface_init(tty_interface_t *state, tty_t *tty, choices_t *choices, options_t *options) { state->tty = tty; state->choices = choices; state->options = options; + strcpy(state->search, ""); + strcpy(state->last_search, ""); + state->exit = -1; if (options->init_search) strncpy(state->search, options->init_search, SEARCH_SIZE_MAX); + + update_search(state); } int tty_interface_run(tty_interface_t *state) { @@ -126,7 +141,6 @@ int tty_interface_run(tty_interface_t *state) { choices_t *choices = state->choices; char *search = state->search; - choices_search(choices, search); char ch; while (state->exit < 0) { draw(state); @@ -136,29 +150,24 @@ int tty_interface_run(tty_interface_t *state) { if (search_size < SEARCH_SIZE_MAX) { search[search_size++] = ch; search[search_size] = '\0'; - choices_search(choices, search); } } else if (ch == KEY_DEL || ch == KEY_CTRL('H')) { /* DEL || Backspace (C-H) */ if (search_size) search[--search_size] = '\0'; - choices_search(choices, search); } else if (ch == KEY_CTRL('U')) { /* C-U */ search_size = 0; search[0] = '\0'; - choices_search(choices, search); } else if (ch == KEY_CTRL('W')) { /* C-W */ if (search_size) search[--search_size] = '\0'; while (search_size && !isspace(search[--search_size])) search[search_size] = '\0'; - choices_search(choices, search); } else if (ch == KEY_CTRL('N')) { /* C-N */ choices_next(choices); } else if (ch == KEY_CTRL('P')) { /* C-P */ choices_prev(choices); } else if (ch == KEY_CTRL('I')) { /* TAB (C-I) */ strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX); - choices_search(choices, search); } else if (ch == KEY_CTRL('C') || ch == KEY_CTRL('D')) { /* ^C || ^D */ clear(state); tty_close(tty); @@ -176,6 +185,7 @@ int tty_interface_run(tty_interface_t *state) { } } } + update_state(state); } return state->exit; diff --git a/src/tty_interface.h b/src/tty_interface.h index d6b05de..bb53cfe 100644 --- a/src/tty_interface.h +++ b/src/tty_interface.h @@ -13,6 +13,7 @@ typedef struct { options_t *options; char search[SEARCH_SIZE_MAX + 1]; + char last_search[SEARCH_SIZE_MAX + 1]; int exit; } tty_interface_t; |