Skip to content

Commit 3996611

Browse files
committed
stmhal: Add home/end cursor support in readline.
Home/end work in picocom and screen (different codes in those 2 programs). Also, CTRL-A (for non-empty liny) and CTRL-E act as home/end.
1 parent 3269cf2 commit 3996611

2 files changed

Lines changed: 59 additions & 35 deletions

File tree

stmhal/readline.c

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int readline(vstr_t *line, const char *prompt) {
3131
stdout_tx_str(prompt);
3232
int orig_line_len = line->len;
3333
int escape_seq = 0;
34+
char escape_seq_buf[1] = {0};
3435
int hist_cur = -1;
3536
int cursor_pos = orig_line_len;
3637
for (;;) {
@@ -43,6 +44,12 @@ int readline(vstr_t *line, const char *prompt) {
4344
if (VCP_CHAR_CTRL_A <= c && c <= VCP_CHAR_CTRL_D && vstr_len(line) == orig_line_len) {
4445
// control character with empty line
4546
return c;
47+
} else if (c == VCP_CHAR_CTRL_A) {
48+
// CTRL-A with non-empty line is go-to-start-of-line
49+
redraw_step_back = cursor_pos - orig_line_len;
50+
} else if (c == VCP_CHAR_CTRL_E) {
51+
// CTRL-E is go-to-end-of-line
52+
redraw_step_forward = line->len - cursor_pos;
4653
} else if (c == '\r') {
4754
// newline
4855
stdout_tx_str("\r\n");
@@ -80,44 +87,60 @@ int readline(vstr_t *line, const char *prompt) {
8087
escape_seq = 0;
8188
}
8289
} else if (escape_seq == 2) {
83-
escape_seq = 0;
84-
if (c == 'A') {
85-
// up arrow
86-
if (hist_cur + 1 < READLINE_HIST_SIZE && readline_hist[hist_cur + 1] != NULL) {
87-
// increase hist num
88-
hist_cur += 1;
89-
// set line to history
90-
line->len = orig_line_len;
91-
vstr_add_str(line, readline_hist[hist_cur]);
92-
// set redraw parameters
93-
redraw_step_back = cursor_pos - orig_line_len;
94-
redraw_from_cursor = true;
95-
redraw_step_forward = line->len - orig_line_len;
96-
}
97-
} else if (c == 'B') {
98-
// down arrow
99-
if (hist_cur >= 0) {
100-
// decrease hist num
101-
hist_cur -= 1;
102-
// set line to history
103-
vstr_cut_tail_bytes(line, line->len - orig_line_len);
104-
if (hist_cur >= 0) {
90+
if ('0' <= c && c <= '9') {
91+
escape_seq = 3;
92+
escape_seq_buf[0] = c;
93+
} else {
94+
escape_seq = 0;
95+
if (c == 'A') {
96+
// up arrow
97+
if (hist_cur + 1 < READLINE_HIST_SIZE && readline_hist[hist_cur + 1] != NULL) {
98+
// increase hist num
99+
hist_cur += 1;
100+
// set line to history
101+
line->len = orig_line_len;
105102
vstr_add_str(line, readline_hist[hist_cur]);
103+
// set redraw parameters
104+
redraw_step_back = cursor_pos - orig_line_len;
105+
redraw_from_cursor = true;
106+
redraw_step_forward = line->len - orig_line_len;
107+
}
108+
} else if (c == 'B') {
109+
// down arrow
110+
if (hist_cur >= 0) {
111+
// decrease hist num
112+
hist_cur -= 1;
113+
// set line to history
114+
vstr_cut_tail_bytes(line, line->len - orig_line_len);
115+
if (hist_cur >= 0) {
116+
vstr_add_str(line, readline_hist[hist_cur]);
117+
}
118+
// set redraw parameters
119+
redraw_step_back = cursor_pos - orig_line_len;
120+
redraw_from_cursor = true;
121+
redraw_step_forward = line->len - orig_line_len;
122+
}
123+
} else if (c == 'C') {
124+
// right arrow
125+
if (cursor_pos < line->len) {
126+
redraw_step_forward = 1;
127+
}
128+
} else if (c == 'D') {
129+
// left arrow
130+
if (cursor_pos > orig_line_len) {
131+
redraw_step_back = 1;
106132
}
107-
// set redraw parameters
108-
redraw_step_back = cursor_pos - orig_line_len;
109-
redraw_from_cursor = true;
110-
redraw_step_forward = line->len - orig_line_len;
111-
}
112-
} else if (c == 'C') {
113-
// right arrow
114-
if (cursor_pos < line->len) {
115-
redraw_step_forward = 1;
116133
}
117-
} else if (c == 'D') {
118-
// left arrow
119-
if (cursor_pos > orig_line_len) {
120-
redraw_step_back = 1;
134+
}
135+
} else if (escape_seq == 3) {
136+
escape_seq = 0;
137+
if (c == '~') {
138+
if (escape_seq_buf[0] == '1' || escape_seq_buf[0] == '7') {
139+
// home key
140+
redraw_step_back = cursor_pos - orig_line_len;
141+
} else if (escape_seq_buf[0] == '4' || escape_seq_buf[0] == '8') {
142+
// end key
143+
redraw_step_forward = line->len - cursor_pos;
121144
}
122145
}
123146
} else {

stmhal/usb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define VCP_CHAR_CTRL_B (2)
44
#define VCP_CHAR_CTRL_C (3)
55
#define VCP_CHAR_CTRL_D (4)
6+
#define VCP_CHAR_CTRL_E (5)
67

78
typedef enum {
89
USB_DEVICE_MODE_CDC_MSC,

0 commit comments

Comments
 (0)