Skip to content

Commit

Permalink
lib/mp-readline: Allow overriding implementation of cursor functions
Browse files Browse the repository at this point in the history
Default implementation uses VT100-style sequences which are not implemented
by all terminals out there
  • Loading branch information
stinos committed May 28, 2015
1 parent ec1345a commit 451f58e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/mp-readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ STATIC char *str_dup_maybe(const char *str) {
return s2;
}

STATIC void move_cursor_back(uint pos) {
// By default assume terminal which implements VT100 commands...
#ifndef MICROPY_HAL_HAS_VT100
#define MICROPY_HAL_HAS_VT100 (1)
#endif

// ...and provide the implementation using them
#if MICROPY_HAL_HAS_VT100
STATIC void mp_hal_move_cursor_back(uint pos) {
if (pos <= 4) {
// fast path for most common case of 1 step back
mp_hal_stdout_tx_strn("\b\b\b\b", pos);
Expand All @@ -75,9 +82,10 @@ STATIC void move_cursor_back(uint pos) {
}
}

STATIC void erase_line_from_cursor(void) {
STATIC void mp_hal_erase_line_from_cursor(void) {
mp_hal_stdout_tx_strn("\x1b[K", 3);
}
#endif

typedef struct _readline_t {
vstr_t *line;
Expand Down Expand Up @@ -257,19 +265,19 @@ int readline_process_char(int c) {

// redraw command prompt, efficiently
if (redraw_step_back > 0) {
move_cursor_back(redraw_step_back);
mp_hal_move_cursor_back(redraw_step_back);
rl.cursor_pos -= redraw_step_back;
}
if (redraw_from_cursor) {
if (rl.line->len < last_line_len) {
// erase old chars
// (number of chars to erase: last_line_len - rl.cursor_pos)
erase_line_from_cursor();
mp_hal_erase_line_from_cursor();
}
// draw new chars
mp_hal_stdout_tx_strn(rl.line->buf + rl.cursor_pos, rl.line->len - rl.cursor_pos);
// move cursor forward if needed (already moved forward by length of line, so move it back)
move_cursor_back(rl.line->len - (rl.cursor_pos + redraw_step_forward));
mp_hal_move_cursor_back(rl.line->len - (rl.cursor_pos + redraw_step_forward));
rl.cursor_pos += redraw_step_forward;
} else if (redraw_step_forward > 0) {
// draw over old chars to move cursor forwards
Expand Down

0 comments on commit 451f58e

Please sign in to comment.