Skip to content

Commit 67a8da1

Browse files
committed
[Feature][Shell] 增加对 Home、Insert、Delete 和 End 键的支持,改进输入模式处理
1 parent 88d0eb0 commit 67a8da1

File tree

3 files changed

+105
-13
lines changed

3 files changed

+105
-13
lines changed

components/finsh/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ if RT_USING_MSH
4141
help
4242
Enable Ctrl+Backspace to delete words and Ctrl+Arrow to move cursor by word
4343

44+
config FINSH_USING_FUNC_EXT
45+
bool "Enable function extension home end ins del"
46+
default n
47+
help
48+
Enable function extension home end ins del.
49+
4450
config FINSH_USING_SYMTAB
4551
bool "Using symbol table for commands"
4652
default y

components/finsh/shell.c

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ static void finsh_thread_entry(void *parameter)
558558
* down key: 0x1b 0x5b 0x42
559559
* right key:0x1b 0x5b 0x43
560560
* left key: 0x1b 0x5b 0x44
561+
* home : 0x1b 0x5b 0x31 0x7E
562+
* insert : 0x1b 0x5b 0x32 0x7E
563+
* del : 0x1b 0x5b 0x33 0x7E
564+
* end : 0x1b 0x5b 0x34 0x7E
561565
*/
562566
if (ch == 0x1b)
563567
{
@@ -676,6 +680,69 @@ static void finsh_thread_entry(void *parameter)
676680
}
677681
}
678682
#endif /*defined(FINSH_USING_WORD_OPERATION) */
683+
#if defined(FINSH_USING_FUNC_EXT)
684+
else if (ch >= 0x31 && ch <= 0x34) /* home(0x31), insert(0x32), del(0x33), end(0x34) */
685+
{
686+
shell->stat = WAIT_EXT_KEY;
687+
shell->line[shell->line_position + 1] = ch; /* store the key code */
688+
continue;
689+
}
690+
691+
}
692+
else if (shell->stat == WAIT_EXT_KEY)
693+
{
694+
shell->stat = WAIT_NORMAL;
695+
696+
if (ch == 0x7E) /* extended key terminator */
697+
{
698+
rt_uint8_t key_code = shell->line[shell->line_position + 1];
699+
700+
if (key_code == 0x31) /* home key */
701+
{
702+
/* move cursor to beginning of line */
703+
while (shell->line_curpos > 0)
704+
{
705+
rt_kprintf("\b");
706+
shell->line_curpos--;
707+
}
708+
}
709+
else if (key_code == 0x32) /* insert key */
710+
{
711+
/* toggle insert mode */
712+
shell->overwrite_mode = !shell->overwrite_mode;
713+
}
714+
else if (key_code == 0x33) /* del key */
715+
{
716+
/* delete character at current cursor position */
717+
if (shell->line_curpos < shell->line_position)
718+
{
719+
int i;
720+
shell->line_position--;
721+
rt_memmove(&shell->line[shell->line_curpos],
722+
&shell->line[shell->line_curpos + 1],
723+
shell->line_position - shell->line_curpos);
724+
725+
shell->line[shell->line_position] = 0;
726+
727+
rt_kprintf("%s ", &shell->line[shell->line_curpos]);
728+
729+
/* move cursor back to original position */
730+
for (i = shell->line_curpos; i <= shell->line_position; i++)
731+
rt_kprintf("\b");
732+
}
733+
}
734+
else if (key_code == 0x34) /* end key */
735+
{
736+
/* move cursor to end of line */
737+
while (shell->line_curpos < shell->line_position)
738+
{
739+
rt_kprintf("%c", shell->line[shell->line_curpos]);
740+
shell->line_curpos++;
741+
}
742+
}
743+
continue;
744+
}
745+
#endif /*defined(FINSH_USING_FUNC_EXT) */
679746
}
680747

681748
/* received null or error */
@@ -789,28 +856,46 @@ static void finsh_thread_entry(void *parameter)
789856
if (shell->line_curpos < shell->line_position)
790857
{
791858
int i;
859+
#if defined(FINSH_USING_FUNC_EXT)
860+
if (shell->overwrite_mode) /* overwrite mode */
861+
{
862+
/* directly overwrite the character */
863+
shell->line[shell->line_curpos] = ch;
864+
if (shell->echo_mode)
865+
rt_kprintf("%c", ch);
866+
shell->line_curpos++;
867+
}
868+
else /* insert mode */
869+
#endif /*defined(FINSH_USING_FUNC_EXT)*/
870+
{
871+
shell->line_position++;
872+
/* move existing characters to the right */
873+
rt_memmove(&shell->line[shell->line_curpos + 1],
874+
&shell->line[shell->line_curpos],
875+
shell->line_position - shell->line_curpos);
876+
shell->line[shell->line_curpos] = ch;
792877

793-
rt_memmove(&shell->line[shell->line_curpos + 1],
794-
&shell->line[shell->line_curpos],
795-
shell->line_position - shell->line_curpos);
796-
shell->line[shell->line_curpos] = ch;
797-
if (shell->echo_mode)
798-
rt_kprintf("%s", &shell->line[shell->line_curpos]);
799-
800-
/* move the cursor to new position */
801-
for (i = shell->line_curpos; i < shell->line_position; i++)
802-
rt_kprintf("\b");
878+
if (shell->echo_mode)
879+
{
880+
rt_kprintf("%s", &shell->line[shell->line_curpos]);
881+
/* move cursor back to correct position */
882+
for (i = shell->line_curpos + 1; i < shell->line_position; i++)
883+
rt_kprintf("\b");
884+
}
885+
shell->line_curpos++;
886+
}
803887
}
804888
else
805889
{
890+
/* append character at end of line */
806891
shell->line[shell->line_position] = ch;
807892
if (shell->echo_mode)
808893
rt_kprintf("%c", ch);
894+
shell->line_position++;
895+
shell->line_curpos++;
809896
}
810897

811898
ch = 0;
812-
shell->line_position ++;
813-
shell->line_curpos++;
814899
if (shell->line_position >= FINSH_CMD_SIZE)
815900
{
816901
/* clear command line */

components/finsh/shell.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ enum input_stat
5757
WAIT_NORMAL,
5858
WAIT_SPEC_KEY,
5959
WAIT_FUNC_KEY,
60+
WAIT_EXT_KEY,
6061
};
6162
struct finsh_shell
6263
{
@@ -66,7 +67,7 @@ struct finsh_shell
6667

6768
rt_uint8_t echo_mode: 1;
6869
rt_uint8_t prompt_mode: 1;
69-
70+
rt_uint8_t overwrite_mode: 1;
7071
#ifdef FINSH_USING_HISTORY
7172
rt_uint16_t current_history;
7273
rt_uint16_t history_count;

0 commit comments

Comments
 (0)