summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2018-09-09 13:07:59 -0700
committerGitHub <noreply@github.com>2018-09-09 13:07:59 -0700
commit1a57c9b67055845645acb78ada9092ed5b4b3a17 (patch)
tree2d898585f1779a261dff03f2566f963feef5c505 /src
parentc2b8c00272b1b25b5aecd9946440fc6d7a720e4b (diff)
parent8dd7a9f49c2b65f28025902106f364ff11d4170d (diff)
Merge pull request #77 from syrrim/uni
Add utf-8 support to input, fixes #21
Diffstat (limited to 'src')
-rw-r--r--src/tty_interface.c31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/tty_interface.c b/src/tty_interface.c
index 4d4d52f..a76cfa9 100644
--- a/src/tty_interface.c
+++ b/src/tty_interface.c
@@ -7,6 +7,14 @@
#include "tty_interface.h"
#include "../config.h"
+static int isprint_unicode(char c){
+ return isprint(c) || c & (1<<7);
+}
+
+static int is_boundary(char c) {
+ return ~c & (1<<7) || c & (1<<6);
+}
+
static void clear(tty_interface_t *state) {
tty_t *tty = state->tty;
@@ -99,7 +107,10 @@ static void draw(tty_interface_t *state) {
tty_moveup(tty, num_lines);
}
- tty_setcol(tty, strlen(options->prompt) + state->cursor);
+ tty_setcol(tty, 0);
+ fputs(options->prompt, tty->fout);
+ for(size_t i=0; i<state->cursor; i++)
+ fputc(state->search[i], tty->fout);
tty_flush(tty);
}
@@ -142,9 +153,13 @@ static void action_del_char(tty_interface_t *state) {
if(state->cursor == 0) {
return;
}
+ size_t original_cursor = state->cursor;
state->cursor--;
- memmove(&state->search[state->cursor], &state->search[state->cursor + 1], length - state->cursor);
+ while(!is_boundary(state->search[state->cursor]) && state->cursor)
+ state->cursor--;
+
+ memmove(&state->search[state->cursor], &state->search[original_cursor], length - original_cursor + 1);
}
}
@@ -182,13 +197,19 @@ static void action_next(tty_interface_t *state) {
}
static void action_left(tty_interface_t *state) {
- if (state->cursor > 0)
+ if (state->cursor > 0){
state->cursor--;
+ while(!is_boundary(state->search[state->cursor]) && state->cursor)
+ state->cursor--;
+ }
}
static void action_right(tty_interface_t *state) {
- if (state->cursor < strlen(state->search))
+ if (state->cursor < strlen(state->search)){
state->cursor++;
+ while(!is_boundary(state->search[state->cursor]))
+ state->cursor++;
+ }
}
static void action_beginning(tty_interface_t *state) {
@@ -319,7 +340,7 @@ static void handle_input(tty_interface_t *state, const char *s) {
/* No matching keybinding, add to search */
for (int i = 0; input[i]; i++)
- if (isprint(input[i]))
+ if (isprint_unicode(input[i]))
append_search(state, input[i]);
/* We have processed the input, so clear it */