diff options
author | Benji Dial <benji6283@gmail.com> | 2021-02-16 20:38:53 -0500 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2021-02-16 20:38:53 -0500 |
commit | 47513bd32c256c4f35e3a8ced7d9fd7e15903530 (patch) | |
tree | cafdf75d52a954814726e07445063c41bb6599f9 /src/user/libterm/readline.c | |
parent | bd7facc4b5f53481dc85a15ba123361b2758655b (diff) | |
download | portland-os-47513bd32c256c4f35e3a8ced7d9fd7e15903530.tar.gz |
terminal application with ipc, shift+pause state dumper, hello world for terminal, meminfo popup program
Diffstat (limited to 'src/user/libterm/readline.c')
-rw-r--r-- | src/user/libterm/readline.c | 106 |
1 files changed, 106 insertions, 0 deletions
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 <libterm/terminal.h> + +#include <knob/block.h> +#include <knob/key.h> + +#include <stdint.h> + +//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 |