Skip to content

Commit a8b7155

Browse files
maass-hamburgdpgeorge
authored andcommitted
shared/readline: Handle \r and \n correctly as a newline.
The old code was only handling `\r` and ignoring `\n`, which is a problem for systems that only send `\n` as a newline. This commit handles it better, supporting both characters. Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
1 parent 9721955 commit a8b7155

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

shared/readline/readline.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ typedef struct _readline_t {
101101
int hist_cur;
102102
size_t cursor_pos;
103103
char escape_seq_buf[1];
104+
char last_nl;
104105
#if MICROPY_REPL_AUTO_INDENT
105106
uint8_t auto_indent_state;
106107
#endif
@@ -138,6 +139,17 @@ static size_t cursor_count_word(int forward) {
138139
}
139140
#endif
140141

142+
// Function returns true if newline character shall be processed.
143+
static bool process_nl(int c) {
144+
if ((c == '\r' || c == '\n') && (rl.last_nl == 0 || rl.last_nl == c)) {
145+
rl.last_nl = c;
146+
return true;
147+
}
148+
149+
rl.last_nl = 0;
150+
return false;
151+
}
152+
141153
int readline_process_char(int c) {
142154
size_t last_line_len = rl.line->len;
143155
int redraw_step_back = 0;
@@ -192,7 +204,7 @@ int readline_process_char(int c) {
192204
} else if (c == CHAR_CTRL_W) {
193205
goto backward_kill_word;
194206
#endif
195-
} else if (c == '\r') {
207+
} else if (process_nl(c)) {
196208
// newline
197209
mp_hal_stdout_tx_str("\r\n");
198210
readline_push_history(vstr_null_terminated_str(rl.line) + rl.orig_line_len);

0 commit comments

Comments
 (0)