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.
This commit is contained in:
Damien George 2014-04-03 23:30:24 +01:00
parent 3269cf2f93
commit 3996611c1b
2 changed files with 59 additions and 35 deletions

View File

@ -31,6 +31,7 @@ int readline(vstr_t *line, const char *prompt) {
stdout_tx_str(prompt); stdout_tx_str(prompt);
int orig_line_len = line->len; int orig_line_len = line->len;
int escape_seq = 0; int escape_seq = 0;
char escape_seq_buf[1] = {0};
int hist_cur = -1; int hist_cur = -1;
int cursor_pos = orig_line_len; int cursor_pos = orig_line_len;
for (;;) { for (;;) {
@ -43,6 +44,12 @@ int readline(vstr_t *line, const char *prompt) {
if (VCP_CHAR_CTRL_A <= c && c <= VCP_CHAR_CTRL_D && vstr_len(line) == orig_line_len) { if (VCP_CHAR_CTRL_A <= c && c <= VCP_CHAR_CTRL_D && vstr_len(line) == orig_line_len) {
// control character with empty line // control character with empty line
return c; return c;
} else if (c == VCP_CHAR_CTRL_A) {
// CTRL-A with non-empty line is go-to-start-of-line
redraw_step_back = cursor_pos - orig_line_len;
} else if (c == VCP_CHAR_CTRL_E) {
// CTRL-E is go-to-end-of-line
redraw_step_forward = line->len - cursor_pos;
} else if (c == '\r') { } else if (c == '\r') {
// newline // newline
stdout_tx_str("\r\n"); stdout_tx_str("\r\n");
@ -80,44 +87,60 @@ int readline(vstr_t *line, const char *prompt) {
escape_seq = 0; escape_seq = 0;
} }
} else if (escape_seq == 2) { } else if (escape_seq == 2) {
escape_seq = 0; if ('0' <= c && c <= '9') {
if (c == 'A') { escape_seq = 3;
// up arrow escape_seq_buf[0] = c;
if (hist_cur + 1 < READLINE_HIST_SIZE && readline_hist[hist_cur + 1] != NULL) { } else {
// increase hist num escape_seq = 0;
hist_cur += 1; if (c == 'A') {
// set line to history // up arrow
line->len = orig_line_len; if (hist_cur + 1 < READLINE_HIST_SIZE && readline_hist[hist_cur + 1] != NULL) {
vstr_add_str(line, readline_hist[hist_cur]); // increase hist num
// set redraw parameters hist_cur += 1;
redraw_step_back = cursor_pos - orig_line_len; // set line to history
redraw_from_cursor = true; line->len = orig_line_len;
redraw_step_forward = line->len - orig_line_len;
}
} else if (c == 'B') {
// down arrow
if (hist_cur >= 0) {
// decrease hist num
hist_cur -= 1;
// set line to history
vstr_cut_tail_bytes(line, line->len - orig_line_len);
if (hist_cur >= 0) {
vstr_add_str(line, readline_hist[hist_cur]); vstr_add_str(line, readline_hist[hist_cur]);
// set redraw parameters
redraw_step_back = cursor_pos - orig_line_len;
redraw_from_cursor = true;
redraw_step_forward = line->len - orig_line_len;
} }
// set redraw parameters } else if (c == 'B') {
// down arrow
if (hist_cur >= 0) {
// decrease hist num
hist_cur -= 1;
// set line to history
vstr_cut_tail_bytes(line, line->len - orig_line_len);
if (hist_cur >= 0) {
vstr_add_str(line, readline_hist[hist_cur]);
}
// set redraw parameters
redraw_step_back = cursor_pos - orig_line_len;
redraw_from_cursor = true;
redraw_step_forward = line->len - orig_line_len;
}
} else if (c == 'C') {
// right arrow
if (cursor_pos < line->len) {
redraw_step_forward = 1;
}
} else if (c == 'D') {
// left arrow
if (cursor_pos > orig_line_len) {
redraw_step_back = 1;
}
}
}
} else if (escape_seq == 3) {
escape_seq = 0;
if (c == '~') {
if (escape_seq_buf[0] == '1' || escape_seq_buf[0] == '7') {
// home key
redraw_step_back = cursor_pos - orig_line_len; redraw_step_back = cursor_pos - orig_line_len;
redraw_from_cursor = true; } else if (escape_seq_buf[0] == '4' || escape_seq_buf[0] == '8') {
redraw_step_forward = line->len - orig_line_len; // end key
} redraw_step_forward = line->len - cursor_pos;
} else if (c == 'C') {
// right arrow
if (cursor_pos < line->len) {
redraw_step_forward = 1;
}
} else if (c == 'D') {
// left arrow
if (cursor_pos > orig_line_len) {
redraw_step_back = 1;
} }
} }
} else { } else {

View File

@ -3,6 +3,7 @@
#define VCP_CHAR_CTRL_B (2) #define VCP_CHAR_CTRL_B (2)
#define VCP_CHAR_CTRL_C (3) #define VCP_CHAR_CTRL_C (3)
#define VCP_CHAR_CTRL_D (4) #define VCP_CHAR_CTRL_D (4)
#define VCP_CHAR_CTRL_E (5)
typedef enum { typedef enum {
USB_DEVICE_MODE_CDC_MSC, USB_DEVICE_MODE_CDC_MSC,