Skip to content

Commit 278d22c

Browse files
stinospfalcon
authored andcommitted
lib/mp-readline: Allow overriding implementation of cursor functions
Default implementation uses VT100-style sequences which are not implemented by all terminals out there
1 parent 9a522dd commit 278d22c

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

lib/mp-readline/readline.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ STATIC char *str_dup_maybe(const char *str) {
6060
return s2;
6161
}
6262

63-
STATIC void move_cursor_back(uint pos) {
63+
// By default assume terminal which implements VT100 commands...
64+
#ifndef MICROPY_HAL_HAS_VT100
65+
#define MICROPY_HAL_HAS_VT100 (1)
66+
#endif
67+
68+
// ...and provide the implementation using them
69+
#if MICROPY_HAL_HAS_VT100
70+
STATIC void mp_hal_move_cursor_back(uint pos) {
6471
if (pos <= 4) {
6572
// fast path for most common case of 1 step back
6673
mp_hal_stdout_tx_strn("\b\b\b\b", pos);
@@ -75,9 +82,10 @@ STATIC void move_cursor_back(uint pos) {
7582
}
7683
}
7784

78-
STATIC void erase_line_from_cursor(void) {
85+
STATIC void mp_hal_erase_line_from_cursor(void) {
7986
mp_hal_stdout_tx_strn("\x1b[K", 3);
8087
}
88+
#endif
8189

8290
typedef struct _readline_t {
8391
vstr_t *line;
@@ -257,19 +265,19 @@ int readline_process_char(int c) {
257265

258266
// redraw command prompt, efficiently
259267
if (redraw_step_back > 0) {
260-
move_cursor_back(redraw_step_back);
268+
mp_hal_move_cursor_back(redraw_step_back);
261269
rl.cursor_pos -= redraw_step_back;
262270
}
263271
if (redraw_from_cursor) {
264272
if (rl.line->len < last_line_len) {
265273
// erase old chars
266274
// (number of chars to erase: last_line_len - rl.cursor_pos)
267-
erase_line_from_cursor();
275+
mp_hal_erase_line_from_cursor();
268276
}
269277
// draw new chars
270278
mp_hal_stdout_tx_strn(rl.line->buf + rl.cursor_pos, rl.line->len - rl.cursor_pos);
271279
// move cursor forward if needed (already moved forward by length of line, so move it back)
272-
move_cursor_back(rl.line->len - (rl.cursor_pos + redraw_step_forward));
280+
mp_hal_move_cursor_back(rl.line->len - (rl.cursor_pos + redraw_step_forward));
273281
rl.cursor_pos += redraw_step_forward;
274282
} else if (redraw_step_forward > 0) {
275283
// draw over old chars to move cursor forwards

0 commit comments

Comments
 (0)