readline: Allow linefeeds to be used to end lines

Currently readline ignores linefeeds which works well for \r\n (and \r)
data but has problems with linefeed seperations (which, amoung other
things, is used by Gadgetbridge's BangleJS driver.

Add support for executing commands on a linefeed whilst ensuring that
we continue to treat \r\n as a single line.

Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
wasp-os
Daniel Thompson 2020-07-19 20:41:39 +01:00
parent df61f43d56
commit 33252220c7
1 changed files with 9 additions and 2 deletions

View File

@ -42,7 +42,7 @@
#define READLINE_HIST_SIZE (MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist)))
enum { ESEQ_NONE, ESEQ_ESC, ESEQ_ESC_BRACKET, ESEQ_ESC_BRACKET_DIGIT, ESEQ_ESC_O };
enum { ESEQ_NONE, ESEQ_ESC, ESEQ_ESC_BRACKET, ESEQ_ESC_BRACKET_DIGIT, ESEQ_ESC_O, ESEQ_CR };
void readline_init0(void) {
memset(MP_STATE_PORT(readline_hist), 0, READLINE_HIST_SIZE * sizeof(const char*));
@ -103,6 +103,8 @@ int readline_process_char(int c) {
int redraw_step_back = 0;
bool redraw_from_cursor = false;
int redraw_step_forward = 0;
if (rl.escape_seq == ESEQ_CR && c != '\n')
rl.escape_seq = ESEQ_NONE;
if (rl.escape_seq == ESEQ_NONE) {
if (CHAR_CTRL_A <= c && c <= CHAR_CTRL_E && vstr_len(rl.line) == rl.orig_line_len) {
// control character with empty line
@ -148,7 +150,12 @@ int readline_process_char(int c) {
redraw_step_back = rl.cursor_pos - rl.orig_line_len;
redraw_from_cursor = true;
#endif
} else if (c == '\r') {
} else if (c == '\r' || c == '\n') {
// Treat carriage return as a special escape sequence so that we
// can skip a subsequent linefeed (if there is one)
if (c == '\r')
rl.escape_seq = ESEQ_CR;
// newline
mp_hal_stdout_tx_str("\r\n");
readline_push_history(vstr_null_terminated_str(rl.line) + rl.orig_line_len);