Commit 2acfb7c0 authored by Damien George's avatar Damien George
Browse files

lib/mp-readline: Export readline_push_history function.

parent c754d801
...@@ -81,10 +81,10 @@ STATIC void erase_line_from_cursor(void) { ...@@ -81,10 +81,10 @@ STATIC void erase_line_from_cursor(void) {
typedef struct _readline_t { typedef struct _readline_t {
vstr_t *line; vstr_t *line;
int orig_line_len; size_t orig_line_len;
int escape_seq; int escape_seq;
int hist_cur; int hist_cur;
int cursor_pos; size_t cursor_pos;
char escape_seq_buf[1]; char escape_seq_buf[1];
const char *prompt; const char *prompt;
} readline_t; } readline_t;
...@@ -92,7 +92,7 @@ typedef struct _readline_t { ...@@ -92,7 +92,7 @@ typedef struct _readline_t {
STATIC readline_t rl; STATIC readline_t rl;
int readline_process_char(int c) { 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; int redraw_step_back = 0;
bool redraw_from_cursor = false; bool redraw_from_cursor = false;
int redraw_step_forward = 0; int redraw_step_forward = 0;
...@@ -112,17 +112,7 @@ int readline_process_char(int c) { ...@@ -112,17 +112,7 @@ int readline_process_char(int c) {
} else if (c == '\r') { } else if (c == '\r') {
// newline // newline
mp_hal_stdout_tx_str("\r\n"); 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)) { readline_push_history(vstr_null_terminated_str(rl.line) + rl.orig_line_len);
// 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;
}
}
return 0; return 0;
} else if (c == 27) { } else if (c == 27) {
// escape sequence // escape sequence
...@@ -149,7 +139,7 @@ int readline_process_char(int c) { ...@@ -149,7 +139,7 @@ int readline_process_char(int c) {
redraw_from_cursor = true; redraw_from_cursor = true;
} else { } else {
// one match // 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++); vstr_ins_byte(rl.line, rl.cursor_pos + i, *compl_str++);
} }
// set redraw parameters // set redraw parameters
...@@ -184,7 +174,7 @@ int readline_process_char(int c) { ...@@ -184,7 +174,7 @@ int readline_process_char(int c) {
rl.escape_seq = ESEQ_NONE; rl.escape_seq = ESEQ_NONE;
if (c == 'A') { if (c == 'A') {
// up arrow // 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 // increase hist num
rl.hist_cur += 1; rl.hist_cur += 1;
// set line to history // set line to history
...@@ -312,3 +302,19 @@ int readline(vstr_t *line, const char *prompt) { ...@@ -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;
}
}
}
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
void readline_init0(void); void readline_init0(void);
int readline(vstr_t *line, const char *prompt); int readline(vstr_t *line, const char *prompt);
void readline_push_history(const char *line);
void readline_init(vstr_t *line, const char *prompt); void readline_init(vstr_t *line, const char *prompt);
void readline_note_newline(const char *prompt); void readline_note_newline(const char *prompt);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment