shell: add meta-keys

Added following meta-keys:
Ctrl-B - moves the cursor backward one character
Ctrl-D - deletes the character under the cursor
Ctrl-F - moves the cursor forward one character
Ctrl-K - deletes from the cursor to the end of the line
Alt-F  - moves the cursor forward one word
Alt-B  - moves the cursor backward one word

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
Jakub Rzeszutko 2019-01-11 15:08:55 +01:00 committed by Anas Nashif
commit 63696968eb
4 changed files with 97 additions and 2 deletions

View file

@ -768,7 +768,20 @@ static void tab_handle(const struct shell *shell)
} }
} }
static void metakeys_handle(const struct shell *shell, char data) static void alt_metakeys_handle(const struct shell *shell, char data)
{
/* Optional feature */
if (!IS_ENABLED(CONFIG_SHELL_METAKEYS)) {
return;
}
if (data == SHELL_VT100_ASCII_ALT_B) {
shell_op_cursor_word_move(shell, -1);
} else if (data == SHELL_VT100_ASCII_ALT_F) {
shell_op_cursor_word_move(shell, 1);
}
}
static void ctrl_metakeys_handle(const struct shell *shell, char data)
{ {
/* Optional feature */ /* Optional feature */
if (!IS_ENABLED(CONFIG_SHELL_METAKEYS)) { if (!IS_ENABLED(CONFIG_SHELL_METAKEYS)) {
@ -780,6 +793,10 @@ static void metakeys_handle(const struct shell *shell, char data)
shell_op_cursor_home_move(shell); shell_op_cursor_home_move(shell);
break; break;
case SHELL_VT100_ASCII_CTRL_B: /* CTRL + B */
shell_op_left_arrow(shell);
break;
case SHELL_VT100_ASCII_CTRL_C: /* CTRL + C */ case SHELL_VT100_ASCII_CTRL_C: /* CTRL + C */
shell_op_cursor_end_move(shell); shell_op_cursor_end_move(shell);
if (!shell_cursor_in_empty_line(shell)) { if (!shell_cursor_in_empty_line(shell)) {
@ -789,10 +806,22 @@ static void metakeys_handle(const struct shell *shell, char data)
state_set(shell, SHELL_STATE_ACTIVE); state_set(shell, SHELL_STATE_ACTIVE);
break; break;
case SHELL_VT100_ASCII_CTRL_D: /* CTRL + D */
shell_op_char_delete(shell);
break;
case SHELL_VT100_ASCII_CTRL_E: /* CTRL + E */ case SHELL_VT100_ASCII_CTRL_E: /* CTRL + E */
shell_op_cursor_end_move(shell); shell_op_cursor_end_move(shell);
break; break;
case SHELL_VT100_ASCII_CTRL_F: /* CTRL + F */
shell_op_right_arrow(shell);
break;
case SHELL_VT100_ASCII_CTRL_K: /* CTRL + K */
shell_op_delete_from_cursor(shell);
break;
case SHELL_VT100_ASCII_CTRL_L: /* CTRL + L */ case SHELL_VT100_ASCII_CTRL_L: /* CTRL + L */
SHELL_VT100_CMD(shell, SHELL_VT100_CURSORHOME); SHELL_VT100_CMD(shell, SHELL_VT100_CURSORHOME);
SHELL_VT100_CMD(shell, SHELL_VT100_CLEARSCREEN); SHELL_VT100_CMD(shell, SHELL_VT100_CLEARSCREEN);
@ -921,7 +950,7 @@ static void state_collect(const struct shell *shell)
flag_history_exit_set(shell, true); flag_history_exit_set(shell, true);
shell_op_char_insert(shell, data); shell_op_char_insert(shell, data);
} else { } else {
metakeys_handle(shell, data); ctrl_metakeys_handle(shell, data);
} }
break; break;
} }
@ -932,6 +961,7 @@ static void state_collect(const struct shell *shell)
receive_state_change(shell, receive_state_change(shell,
SHELL_RECEIVE_ESC_SEQ); SHELL_RECEIVE_ESC_SEQ);
} else { } else {
alt_metakeys_handle(shell, data);
receive_state_change(shell, receive_state_change(shell,
SHELL_RECEIVE_DEFAULT); SHELL_RECEIVE_DEFAULT);
} }

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#include <ctype.h>
#include "shell_ops.h" #include "shell_ops.h"
void shell_op_cursor_vert_move(const struct shell *shell, s32_t delta) void shell_op_cursor_vert_move(const struct shell *shell, s32_t delta)
@ -97,6 +98,51 @@ void shell_op_cursor_move(const struct shell *shell, s16_t val)
shell->ctx->cmd_buff_pos = new_pos; shell->ctx->cmd_buff_pos = new_pos;
} }
static u16_t shift_calc(const char *str, u16_t pos, u16_t len, s16_t sign)
{
bool found = false;
u16_t ret = 0;
u16_t idx;
while (1) {
idx = pos + ret * sign;
if (((idx == 0) && (sign < 0)) ||
((idx == len) && (sign > 0))) {
break;
}
if (isalnum((int)str[idx]) != 0) {
found = true;
} else {
if (found) {
break;
}
}
ret++;
}
return ret;
}
void shell_op_cursor_word_move(const struct shell *shell, s16_t val)
{
s16_t shift;
s16_t sign;
if (val < 0) {
val = -val;
sign = -1;
} else {
sign = 1;
}
while (val--) {
shift = shift_calc(shell->ctx->cmd_buff,
shell->ctx->cmd_buff_pos,
shell->ctx->cmd_buff_len, sign);
shell_op_cursor_move(shell, sign * shift);
}
}
void shell_op_word_remove(const struct shell *shell) void shell_op_word_remove(const struct shell *shell)
{ {
char *str = &shell->ctx->cmd_buff[shell->ctx->cmd_buff_pos - 1]; char *str = &shell->ctx->cmd_buff[shell->ctx->cmd_buff_pos - 1];
@ -256,6 +302,14 @@ void shell_op_char_delete(const struct shell *shell)
reprint_from_cursor(shell, --diff, true); reprint_from_cursor(shell, --diff, true);
} }
void shell_op_delete_from_cursor(const struct shell *shell)
{
shell->ctx->cmd_buff_len = shell->ctx->cmd_buff_pos;
shell->ctx->cmd_buff[shell->ctx->cmd_buff_pos] = '\0';
clear_eos(shell);
}
void shell_op_completion_insert(const struct shell *shell, void shell_op_completion_insert(const struct shell *shell,
const char *compl, const char *compl,
u16_t compl_len) u16_t compl_len)

View file

@ -162,6 +162,9 @@ void shell_op_left_arrow(const struct shell *shell);
void shell_op_right_arrow(const struct shell *shell); void shell_op_right_arrow(const struct shell *shell);
/* Moves cursor by defined number of words left (val negative) or right. */
void shell_op_cursor_word_move(const struct shell *shell, s16_t val);
/* /*
* Removes the "word" to the left of the cursor: * Removes the "word" to the left of the cursor:
* - if there are spaces at the cursor position, remove all spaces to the left * - if there are spaces at the cursor position, remove all spaces to the left
@ -186,6 +189,8 @@ void shell_op_char_backspace(const struct shell *shell);
void shell_op_char_delete(const struct shell *shell); void shell_op_char_delete(const struct shell *shell);
void shell_op_delete_from_cursor(const struct shell *shell);
void shell_op_completion_insert(const struct shell *shell, void shell_op_completion_insert(const struct shell *shell,
const char *compl, const char *compl,
u16_t compl_len); u16_t compl_len);

View file

@ -11,11 +11,17 @@
#define SHELL_VT100_ASCII_DEL (0x7F) #define SHELL_VT100_ASCII_DEL (0x7F)
#define SHELL_VT100_ASCII_BSPACE (0x08) #define SHELL_VT100_ASCII_BSPACE (0x08)
#define SHELL_VT100_ASCII_CTRL_A (0x01) #define SHELL_VT100_ASCII_CTRL_A (0x01)
#define SHELL_VT100_ASCII_CTRL_B (0x02)
#define SHELL_VT100_ASCII_CTRL_C (0x03) #define SHELL_VT100_ASCII_CTRL_C (0x03)
#define SHELL_VT100_ASCII_CTRL_D (0x04)
#define SHELL_VT100_ASCII_CTRL_E (0x05) #define SHELL_VT100_ASCII_CTRL_E (0x05)
#define SHELL_VT100_ASCII_CTRL_F (0x06)
#define SHELL_VT100_ASCII_CTRL_K (0x0B)
#define SHELL_VT100_ASCII_CTRL_L (0x0C) #define SHELL_VT100_ASCII_CTRL_L (0x0C)
#define SHELL_VT100_ASCII_CTRL_U (0x15) #define SHELL_VT100_ASCII_CTRL_U (0x15)
#define SHELL_VT100_ASCII_CTRL_W (0x17) #define SHELL_VT100_ASCII_CTRL_W (0x17)
#define SHELL_VT100_ASCII_ALT_B (0x62)
#define SHELL_VT100_ASCII_ALT_F (0x66)
#define SHELL_VT100_SETNL \ #define SHELL_VT100_SETNL \
{ \ { \