|
|
@ -81,10 +81,10 @@ STATIC void erase_line_from_cursor(void) { |
|
|
|
|
|
|
|
typedef struct _readline_t { |
|
|
|
vstr_t *line; |
|
|
|
int orig_line_len; |
|
|
|
size_t orig_line_len; |
|
|
|
int escape_seq; |
|
|
|
int hist_cur; |
|
|
|
int cursor_pos; |
|
|
|
size_t cursor_pos; |
|
|
|
char escape_seq_buf[1]; |
|
|
|
const char *prompt; |
|
|
|
} readline_t; |
|
|
@ -92,7 +92,7 @@ typedef struct _readline_t { |
|
|
|
STATIC readline_t rl; |
|
|
|
|
|
|
|
int readline_process_char(int c) { |
|
|
|
int last_line_len = rl.line->len; |
|
|
|
size_t last_line_len = rl.line->len; |
|
|
|
int redraw_step_back = 0; |
|
|
|
bool redraw_from_cursor = false; |
|
|
|
int redraw_step_forward = 0; |
|
|
@ -112,17 +112,7 @@ int readline_process_char(int c) { |
|
|
|
} else if (c == '\r') { |
|
|
|
// newline
|
|
|
|
mp_hal_stdout_tx_str("\r\n"); |
|
|
|
if (rl.line->len > rl.orig_line_len && (MP_STATE_PORT(readline_hist)[0] == NULL || strcmp(MP_STATE_PORT(readline_hist)[0], rl.line->buf + rl.orig_line_len) != 0)) { |
|
|
|
// a line which is not empty and different from the last one
|
|
|
|
// so update the history
|
|
|
|
char *most_recent_hist = str_dup_maybe(vstr_null_terminated_str(rl.line) + rl.orig_line_len); |
|
|
|
if (most_recent_hist != NULL) { |
|
|
|
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) { |
|
|
|
MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1]; |
|
|
|
} |
|
|
|
MP_STATE_PORT(readline_hist)[0] = most_recent_hist; |
|
|
|
} |
|
|
|
} |
|
|
|
readline_push_history(vstr_null_terminated_str(rl.line) + rl.orig_line_len); |
|
|
|
return 0; |
|
|
|
} else if (c == 27) { |
|
|
|
// escape sequence
|
|
|
@ -149,7 +139,7 @@ int readline_process_char(int c) { |
|
|
|
redraw_from_cursor = true; |
|
|
|
} else { |
|
|
|
// one match
|
|
|
|
for (int i = 0; i < compl_len; ++i) { |
|
|
|
for (mp_uint_t i = 0; i < compl_len; ++i) { |
|
|
|
vstr_ins_byte(rl.line, rl.cursor_pos + i, *compl_str++); |
|
|
|
} |
|
|
|
// set redraw parameters
|
|
|
@ -184,7 +174,7 @@ int readline_process_char(int c) { |
|
|
|
rl.escape_seq = ESEQ_NONE; |
|
|
|
if (c == 'A') { |
|
|
|
// up arrow
|
|
|
|
if (rl.hist_cur + 1 < READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) { |
|
|
|
if (rl.hist_cur + 1 < (int)READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) { |
|
|
|
// increase hist num
|
|
|
|
rl.hist_cur += 1; |
|
|
|
// set line to history
|
|
|
@ -312,3 +302,19 @@ int readline(vstr_t *line, const char *prompt) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void readline_push_history(const char *line) { |
|
|
|
if (line[0] != '\0' |
|
|
|
&& (MP_STATE_PORT(readline_hist)[0] == NULL |
|
|
|
|| strcmp(MP_STATE_PORT(readline_hist)[0], line) != 0)) { |
|
|
|
// a line which is not empty and different from the last one
|
|
|
|
// so update the history
|
|
|
|
char *most_recent_hist = str_dup_maybe(line); |
|
|
|
if (most_recent_hist != NULL) { |
|
|
|
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) { |
|
|
|
MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1]; |
|
|
|
} |
|
|
|
MP_STATE_PORT(readline_hist)[0] = most_recent_hist; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|