Browse code

Merge pull request #77 from syrrim/uni

Add utf-8 support to input, fixes #21

John Hawthorn authored on 09/09/2018 20:07:59 • GitHub committed on 09/09/2018 20:07:59
Showing 1 changed files

... ...
@@ -7,6 +7,14 @@
7 7
 #include "tty_interface.h"
8 8
 #include "../config.h"
9 9
 
10
+static int isprint_unicode(char c){
11
+	return isprint(c) || c & (1<<7);
12
+}
13
+
14
+static int is_boundary(char c) {
15
+	return ~c & (1<<7) || c & (1<<6);
16
+}
17
+
10 18
 static void clear(tty_interface_t *state) {
11 19
 	tty_t *tty = state->tty;
12 20
 
... ...
@@ -99,7 +107,10 @@ static void draw(tty_interface_t *state) {
99 107
 		tty_moveup(tty, num_lines);
100 108
 	}
101 109
 
102
-	tty_setcol(tty, strlen(options->prompt) + state->cursor);
110
+	tty_setcol(tty, 0);
111
+	fputs(options->prompt, tty->fout);
112
+	for(size_t i=0; i<state->cursor; i++)
113
+		fputc(state->search[i], tty->fout);
103 114
 	tty_flush(tty);
104 115
 }
105 116
 
... ...
@@ -142,9 +153,13 @@ static void action_del_char(tty_interface_t *state) {
142 153
 		if(state->cursor == 0) {
143 154
 			return;
144 155
 		}
156
+		size_t original_cursor = state->cursor;
145 157
 
146 158
 		state->cursor--;
147
-		memmove(&state->search[state->cursor], &state->search[state->cursor + 1], length - state->cursor);
159
+		while(!is_boundary(state->search[state->cursor]) && state->cursor)
160
+			state->cursor--;
161
+
162
+		memmove(&state->search[state->cursor], &state->search[original_cursor], length - original_cursor + 1);
148 163
 	}
149 164
 }
150 165
 
... ...
@@ -182,13 +197,19 @@ static void action_next(tty_interface_t *state) {
182 197
 }
183 198
 
184 199
 static void action_left(tty_interface_t *state) {
185
-	if (state->cursor > 0)
200
+	if (state->cursor > 0){
186 201
 		state->cursor--;
202
+		while(!is_boundary(state->search[state->cursor]) && state->cursor)
203
+			state->cursor--;
204
+	}
187 205
 }
188 206
 
189 207
 static void action_right(tty_interface_t *state) {
190
-	if (state->cursor < strlen(state->search))
208
+	if (state->cursor < strlen(state->search)){
191 209
 		state->cursor++;
210
+		while(!is_boundary(state->search[state->cursor]))
211
+			state->cursor++;
212
+	}
192 213
 }
193 214
 
194 215
 static void action_beginning(tty_interface_t *state) {
... ...
@@ -319,7 +340,7 @@ static void handle_input(tty_interface_t *state, const char *s) {
319 340
 
320 341
 	/* No matching keybinding, add to search */
321 342
 	for (int i = 0; input[i]; i++)
322
-		if (isprint(input[i]))
343
+		if (isprint_unicode(input[i]))
323 344
 			append_search(state, input[i]);
324 345
 
325 346
 	/* We have processed the input, so clear it */