Browse code

Track last search

This allows us to avoid explicitly calling out to choices_search

John Hawthorn authored on 20/06/2016 06:58:44
Showing 2 changed files

... ...
@@ -110,15 +110,30 @@ static void action_emit(tty_interface_t *state) {
110 110
 #define KEY_DEL 127
111 111
 #define KEY_ESC 27
112 112
 
113
+static void update_search(tty_interface_t *state) {
114
+	choices_search(state->choices, state->search);
115
+	strcpy(state->last_search, state->search);
116
+}
117
+
118
+void update_state(tty_interface_t *state) {
119
+	if (strcmp(state->last_search, state->search))
120
+		update_search(state);
121
+}
122
+
113 123
 void tty_interface_init(tty_interface_t *state, tty_t *tty, choices_t *choices, options_t *options) {
114 124
 	state->tty = tty;
115 125
 	state->choices = choices;
116 126
 	state->options = options;
117 127
 
128
+	strcpy(state->search, "");
129
+	strcpy(state->last_search, "");
130
+
118 131
 	state->exit = -1;
119 132
 
120 133
 	if (options->init_search)
121 134
 		strncpy(state->search, options->init_search, SEARCH_SIZE_MAX);
135
+
136
+	update_search(state);
122 137
 }
123 138
 
124 139
 int tty_interface_run(tty_interface_t *state) {
... ...
@@ -126,7 +141,6 @@ int tty_interface_run(tty_interface_t *state) {
126 141
 	choices_t *choices = state->choices;
127 142
 	char *search = state->search;
128 143
 
129
-	choices_search(choices, search);
130 144
 	char ch;
131 145
 	while (state->exit < 0) {
132 146
 		draw(state);
... ...
@@ -136,29 +150,24 @@ int tty_interface_run(tty_interface_t *state) {
136 150
 			if (search_size < SEARCH_SIZE_MAX) {
137 151
 				search[search_size++] = ch;
138 152
 				search[search_size] = '\0';
139
-				choices_search(choices, search);
140 153
 			}
141 154
 		} else if (ch == KEY_DEL || ch == KEY_CTRL('H')) { /* DEL || Backspace (C-H) */
142 155
 			if (search_size)
143 156
 				search[--search_size] = '\0';
144
-			choices_search(choices, search);
145 157
 		} else if (ch == KEY_CTRL('U')) { /* C-U */
146 158
 			search_size = 0;
147 159
 			search[0] = '\0';
148
-			choices_search(choices, search);
149 160
 		} else if (ch == KEY_CTRL('W')) { /* C-W */
150 161
 			if (search_size)
151 162
 				search[--search_size] = '\0';
152 163
 			while (search_size && !isspace(search[--search_size]))
153 164
 				search[search_size] = '\0';
154
-			choices_search(choices, search);
155 165
 		} else if (ch == KEY_CTRL('N')) { /* C-N */
156 166
 			choices_next(choices);
157 167
 		} else if (ch == KEY_CTRL('P')) { /* C-P */
158 168
 			choices_prev(choices);
159 169
 		} else if (ch == KEY_CTRL('I')) { /* TAB (C-I) */
160 170
 			strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX);
161
-			choices_search(choices, search);
162 171
 		} else if (ch == KEY_CTRL('C') || ch == KEY_CTRL('D')) { /* ^C || ^D */
163 172
 			clear(state);
164 173
 			tty_close(tty);
... ...
@@ -176,6 +185,7 @@ int tty_interface_run(tty_interface_t *state) {
176 185
 				}
177 186
 			}
178 187
 		}
188
+		update_state(state);
179 189
 	}
180 190
 
181 191
 	return state->exit;
... ...
@@ -13,6 +13,7 @@ typedef struct {
13 13
 	options_t *options;
14 14
 
15 15
 	char search[SEARCH_SIZE_MAX + 1];
16
+	char last_search[SEARCH_SIZE_MAX + 1];
16 17
 
17 18
 	int exit;
18 19
 } tty_interface_t;