Skip to content

Commit 2acfb7c

Browse files
committed
lib/mp-readline: Export readline_push_history function.
1 parent c754d80 commit 2acfb7c

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

lib/mp-readline/readline.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,18 @@ STATIC void erase_line_from_cursor(void) {
8181

8282
typedef struct _readline_t {
8383
vstr_t *line;
84-
int orig_line_len;
84+
size_t orig_line_len;
8585
int escape_seq;
8686
int hist_cur;
87-
int cursor_pos;
87+
size_t cursor_pos;
8888
char escape_seq_buf[1];
8989
const char *prompt;
9090
} readline_t;
9191

9292
STATIC readline_t rl;
9393

9494
int readline_process_char(int c) {
95-
int last_line_len = rl.line->len;
95+
size_t last_line_len = rl.line->len;
9696
int redraw_step_back = 0;
9797
bool redraw_from_cursor = false;
9898
int redraw_step_forward = 0;
@@ -112,17 +112,7 @@ int readline_process_char(int c) {
112112
} else if (c == '\r') {
113113
// newline
114114
mp_hal_stdout_tx_str("\r\n");
115-
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)) {
116-
// a line which is not empty and different from the last one
117-
// so update the history
118-
char *most_recent_hist = str_dup_maybe(vstr_null_terminated_str(rl.line) + rl.orig_line_len);
119-
if (most_recent_hist != NULL) {
120-
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) {
121-
MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1];
122-
}
123-
MP_STATE_PORT(readline_hist)[0] = most_recent_hist;
124-
}
125-
}
115+
readline_push_history(vstr_null_terminated_str(rl.line) + rl.orig_line_len);
126116
return 0;
127117
} else if (c == 27) {
128118
// escape sequence
@@ -149,7 +139,7 @@ int readline_process_char(int c) {
149139
redraw_from_cursor = true;
150140
} else {
151141
// one match
152-
for (int i = 0; i < compl_len; ++i) {
142+
for (mp_uint_t i = 0; i < compl_len; ++i) {
153143
vstr_ins_byte(rl.line, rl.cursor_pos + i, *compl_str++);
154144
}
155145
// set redraw parameters
@@ -184,7 +174,7 @@ int readline_process_char(int c) {
184174
rl.escape_seq = ESEQ_NONE;
185175
if (c == 'A') {
186176
// up arrow
187-
if (rl.hist_cur + 1 < READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) {
177+
if (rl.hist_cur + 1 < (int)READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) {
188178
// increase hist num
189179
rl.hist_cur += 1;
190180
// set line to history
@@ -312,3 +302,19 @@ int readline(vstr_t *line, const char *prompt) {
312302
}
313303
}
314304
}
305+
306+
void readline_push_history(const char *line) {
307+
if (line[0] != '\0'
308+
&& (MP_STATE_PORT(readline_hist)[0] == NULL
309+
|| strcmp(MP_STATE_PORT(readline_hist)[0], line) != 0)) {
310+
// a line which is not empty and different from the last one
311+
// so update the history
312+
char *most_recent_hist = str_dup_maybe(line);
313+
if (most_recent_hist != NULL) {
314+
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) {
315+
MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1];
316+
}
317+
MP_STATE_PORT(readline_hist)[0] = most_recent_hist;
318+
}
319+
}
320+
}

lib/mp-readline/readline.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
void readline_init0(void);
3434
int readline(vstr_t *line, const char *prompt);
35+
void readline_push_history(const char *line);
3536

3637
void readline_init(vstr_t *line, const char *prompt);
3738
void readline_note_newline(const char *prompt);

0 commit comments

Comments
 (0)