From bb637bab94a8c2565e9d097054d86eba2b6ae7e6 Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Mon, 11 Sep 2023 10:07:48 +0500 Subject: [PATCH] Add backspacing to prompt --- src/input.c | 1 + src/kilo.c | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/input.c b/src/input.c index 70af415..9e62080 100644 --- a/src/input.c +++ b/src/input.c @@ -31,6 +31,7 @@ void input_process_key(KEY c) { break; case CTRL_KEY('L'): + case ESCAPE: case NOP: break; diff --git a/src/kilo.c b/src/kilo.c index 06113f1..449c057 100644 --- a/src/kilo.c +++ b/src/kilo.c @@ -71,17 +71,27 @@ char *editor_prompt(const char *prompt) { ui_draw_screen(); KEY key = terminal_read_key(); - if (key == ESCAPE) goto failure; - else if (key == ENTER) { - if (buf_size > 0) goto success; - else goto failure; - } else if (isprint(key)) { - if (buf_size + 1 >= buf_cap) - buf = realloc(buf, buf_cap *= 2); + switch (key) { + case BACKSPACE: + if (buf_size > 0) + buf[--buf_size] = '\0'; + break; + case ENTER: + if (buf_size > 0) goto success; + else goto failure; + break; + case ESCAPE: + goto failure; + break; + default: + if (isprint(key)) { + if (buf_size + 1 >= buf_cap) + buf = realloc(buf, buf_cap *= 2); - buf[buf_size] = key; - buf_size++; - buf[buf_size] = '\0'; + buf[buf_size] = key; + buf_size++; + buf[buf_size] = '\0'; + } } }