From 278d22ce8f9c5b51778f70d36088a986f1b6adc8 Mon Sep 17 00:00:00 2001
From: stijn <stinos@zoho.com>
Date: Thu, 28 May 2015 13:35:38 +0200
Subject: [PATCH] lib/mp-readline: Allow overriding implementation of cursor
 functions

Default implementation uses VT100-style sequences which are not implemented
by all terminals out there
---
 lib/mp-readline/readline.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c
index 1334eb91d..7f63fd2d8 100644
--- a/lib/mp-readline/readline.c
+++ b/lib/mp-readline/readline.c
@@ -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);
@@ -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;
@@ -257,19 +265,19 @@ end_key:
 
     // 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
-- 
GitLab