From 47513bd32c256c4f35e3a8ced7d9fd7e15903530 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Tue, 16 Feb 2021 20:38:53 -0500 Subject: terminal application with ipc, shift+pause state dumper, hello world for terminal, meminfo popup program --- src/user/libterm/readline.c | 106 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/user/libterm/readline.c (limited to 'src/user/libterm/readline.c') diff --git a/src/user/libterm/readline.c b/src/user/libterm/readline.c new file mode 100644 index 0000000..b37801d --- /dev/null +++ b/src/user/libterm/readline.c @@ -0,0 +1,106 @@ +#include + +#include +#include + +#include + +//returns length of string without null terminator +//max_length doesn't include null terminator +uint32_t read_line(char *sz, uint32_t max_length, const char *prompt) { + term_add_sz(prompt); + term_add_char(' '); + term_paint(); + + uint32_t l = 0; + uint32_t c = 0; + + while (1) { + struct key_packet kp = term_get_key_blocking(); + switch (kp.key_id) { + case KEY_LEFT_ARROW: + if (c) { + --c; + term_cursor_left(); + term_paint(); + } + continue; + case KEY_RIGHT_ARROW: + if (c != l) { + ++c; + term_cursor_right(); + term_paint(); + } + continue; + case KEY_HOME: + while (c) { + --c; + term_cursor_left(); + } + term_paint(); + continue; + case KEY_END: + while (c != l) { + ++c; + term_cursor_right(); + } + term_paint(); + continue; + case KEY_DELETE: + if (c != l) { + ++c; + term_cursor_right(); + } + case KEY_BSPACE: + if (!c) + continue; + --c; + --l; + for (uint32_t i = c; i < l; ++i) + sz[i] = sz[i + 1]; + term_cursor_left(); + term_add_sn_no_ww(sz + c, l - c); + term_add_char(' '); + for (uint32_t i = l + 1; i > c; --i) + term_cursor_left(); + term_paint(); + continue; + case KEY_ENTER: + while (c != l) { + ++c; + term_cursor_right(); + } + sz[l] = '\0'; + term_add_char('\n'); + term_paint(); + return l; + default: + if (l == max_length) + continue; + char ch = key_to_char(kp); + if (!ch) + continue; + if (c == l) { + ++l; + term_add_char(sz[c++] = ch); + term_paint(); + continue; + } + if (!(kp.modifiers & INSERT)) { + term_add_char(sz[c++] = ch); + term_paint(); + continue; + } + for (uint32_t i = l; i > c; --i) + sz[i] = sz[i - 1]; + sz[c] = ch; + ++l; + term_add_sn_no_ww(sz + c, l - c); + ++c; + for (uint32_t i = l; i > c; --i) + term_cursor_left(); + term_paint(); + continue; + } + } +} \ No newline at end of file -- cgit v1.2.3