Cli: return to last line with UP arrow

This commit is contained in:
DrZlo13 2021-12-07 01:40:32 +10:00
parent f996b5414d
commit 0ca098b103
1 changed files with 65 additions and 39 deletions

View File

@ -25,6 +25,7 @@ struct Cli {
mstring_t* line; mstring_t* line;
mstring_t* prev_line; mstring_t* prev_line;
size_t cursor_position; size_t cursor_position;
bool esc_mode;
void* context; void* context;
CliWrite write_cb; CliWrite write_cb;
@ -38,6 +39,7 @@ Cli* cli_init(void) {
cli->context = NULL; cli->context = NULL;
cli->write_cb = NULL; cli->write_cb = NULL;
cli->flush_cb = NULL; cli->flush_cb = NULL;
cli->esc_mode = false;
return cli; return cli;
} }
@ -156,7 +158,30 @@ static void cli_handle_backspace(Cli* cli) {
} }
void cli_handle_char(Cli* cli, uint8_t c) { void cli_handle_char(Cli* cli, uint8_t c) {
if(cli->esc_mode) {
switch(c) { switch(c) {
case '[':
cli->esc_mode = true;
break;
case 'A':
if(mstring_size(cli->line) == 0 && mstring_cmp(cli->line, cli->prev_line) != 0) {
// Set line buffer and cursor position
mstring_set(cli->line, mstring_get_cstr(cli->prev_line));
cli->cursor_position = mstring_size(cli->line);
// Show new line to user
cli_write_str(cli, mstring_get_cstr(cli->line));
}
cli->esc_mode = false;
break;
default:
cli->esc_mode = false;
break;
}
} else {
switch(c) {
case CliSymbolAsciiEsc:
cli->esc_mode = true;
break;
case CliSymbolAsciiCR: case CliSymbolAsciiCR:
if(mstring_size(cli->line) == 0) { if(mstring_size(cli->line) == 0) {
cli_write_eol(cli); cli_write_eol(cli);
@ -200,6 +225,7 @@ void cli_handle_char(Cli* cli, uint8_t c) {
default: default:
break; break;
} }
}
cli_flush(cli); cli_flush(cli);
} }