Browse code

Move all action into their own functions

John Hawthorn authored on 20/06/2016 07:08:50
Showing 1 changed files

... ...
@@ -106,6 +106,42 @@ static void action_emit(tty_interface_t *state) {
106 106
 	state->exit = EXIT_SUCCESS;
107 107
 }
108 108
 
109
+static void action_del_char(tty_interface_t *state) {
110
+	if (*state->search)
111
+		state->search[strlen(state->search) - 1] = '\0';
112
+}
113
+
114
+static void action_del_word(tty_interface_t *state) {
115
+	size_t search_size = strlen(state->search);
116
+	if (search_size)
117
+		state->search[--search_size] = '\0';
118
+	while (search_size && !isspace(state->search[--search_size]))
119
+		state->search[search_size] = '\0';
120
+}
121
+
122
+static void action_del_all(tty_interface_t *state) {
123
+	strcpy(state->search, "");
124
+}
125
+
126
+static void action_prev(tty_interface_t *state) {
127
+	choices_prev(state->choices);
128
+}
129
+
130
+static void action_next(tty_interface_t *state) {
131
+	choices_next(state->choices);
132
+}
133
+
134
+static void action_autocomplete(tty_interface_t *state) {
135
+	strncpy(state->search, choices_get(state->choices, state->choices->selection), SEARCH_SIZE_MAX);
136
+}
137
+
138
+static void action_exit(tty_interface_t *state) {
139
+	clear(state);
140
+	tty_close(state->tty);
141
+
142
+	state->exit = EXIT_FAILURE;
143
+}
144
+
109 145
 #define KEY_CTRL(key) ((key) - ('@'))
110 146
 #define KEY_DEL 127
111 147
 #define KEY_ESC 27
... ...
@@ -138,7 +174,6 @@ void tty_interface_init(tty_interface_t *state, tty_t *tty, choices_t *choices,
138 174
 
139 175
 int tty_interface_run(tty_interface_t *state) {
140 176
 	tty_t *tty = state->tty;
141
-	choices_t *choices = state->choices;
142 177
 	char *search = state->search;
143 178
 
144 179
 	char ch;
... ...
@@ -152,26 +187,19 @@ int tty_interface_run(tty_interface_t *state) {
152 187
 				search[search_size] = '\0';
153 188
 			}
154 189
 		} else if (ch == KEY_DEL || ch == KEY_CTRL('H')) { /* DEL || Backspace (C-H) */
155
-			if (search_size)
156
-				search[--search_size] = '\0';
190
+			action_del_char(state);
157 191
 		} else if (ch == KEY_CTRL('U')) { /* C-U */
158
-			search_size = 0;
159
-			search[0] = '\0';
192
+			action_del_all(state);
160 193
 		} else if (ch == KEY_CTRL('W')) { /* C-W */
161
-			if (search_size)
162
-				search[--search_size] = '\0';
163
-			while (search_size && !isspace(search[--search_size]))
164
-				search[search_size] = '\0';
194
+			action_del_word(state);
165 195
 		} else if (ch == KEY_CTRL('N')) { /* C-N */
166
-			choices_next(choices);
196
+			action_next(state);
167 197
 		} else if (ch == KEY_CTRL('P')) { /* C-P */
168
-			choices_prev(choices);
198
+			action_prev(state);
169 199
 		} else if (ch == KEY_CTRL('I')) { /* TAB (C-I) */
170
-			strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX);
200
+			action_autocomplete(state);
171 201
 		} else if (ch == KEY_CTRL('C') || ch == KEY_CTRL('D')) { /* ^C || ^D */
172
-			clear(state);
173
-			tty_close(tty);
174
-			state->exit = EXIT_FAILURE;
202
+			action_exit(state);
175 203
 		} else if (ch == KEY_CTRL('M')) { /* CR */
176 204
 			action_emit(state);
177 205
 		} else if (ch == KEY_ESC) { /* ESC */
... ...
@@ -179,9 +207,9 @@ int tty_interface_run(tty_interface_t *state) {
179 207
 			if (ch == '[' || ch == 'O') {
180 208
 				ch = tty_getchar(tty);
181 209
 				if (ch == 'A') { /* UP ARROW */
182
-					choices_prev(choices);
210
+					action_prev(state);
183 211
 				} else if (ch == 'B') { /* DOWN ARROW */
184
-					choices_next(choices);
212
+					action_next(state);
185 213
 				}
186 214
 			}
187 215
 		}