Skip to content

Commit 8d944bc

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

File tree

2 files changed

+94
-12
lines changed

2 files changed

+94
-12
lines changed

components/finsh/shell.c

Lines changed: 92 additions & 11 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,66 @@ static void finsh_thread_entry(void *parameter)
676680
}
677681
}
678682
#endif /*defined(FINSH_USING_WORD_OPERATION) */
683+
else if (ch >= 0x31 && ch <= 0x34) /* home(0x31), insert(0x32), del(0x33), end(0x34) */
684+
{
685+
shell->stat = WAIT_EXT_KEY;
686+
shell->line[shell->line_position + 1] = ch; /* store the key code */
687+
continue;
688+
}
689+
}
690+
else if (shell->stat == WAIT_EXT_KEY)
691+
{
692+
shell->stat = WAIT_NORMAL;
693+
694+
if (ch == 0x7E) /* extended key terminator */
695+
{
696+
rt_uint8_t key_code = shell->line[shell->line_position + 1];
697+
698+
if (key_code == 0x31) /* home key */
699+
{
700+
/* move cursor to beginning of line */
701+
while (shell->line_curpos > 0)
702+
{
703+
rt_kprintf("\b");
704+
shell->line_curpos--;
705+
}
706+
}
707+
else if (key_code == 0x32) /* insert key */
708+
{
709+
/* toggle insert mode */
710+
shell->overwrite_mode = !shell->overwrite_mode;
711+
}
712+
else if (key_code == 0x33) /* del key */
713+
{
714+
/* delete character at current cursor position */
715+
if (shell->line_curpos < shell->line_position)
716+
{
717+
int i;
718+
719+
rt_memmove(&shell->line[shell->line_curpos],
720+
&shell->line[shell->line_curpos + 1],
721+
shell->line_position - shell->line_curpos);
722+
shell->line_position--;
723+
shell->line[shell->line_position] = 0;
724+
725+
rt_kprintf("%s ", &shell->line[shell->line_curpos]);
726+
727+
/* move cursor back to original position */
728+
for (i = shell->line_curpos; i <= shell->line_position; i++)
729+
rt_kprintf("\b");
730+
}
731+
}
732+
else if (key_code == 0x34) /* end key */
733+
{
734+
/* move cursor to end of line */
735+
while (shell->line_curpos < shell->line_position)
736+
{
737+
rt_kprintf("%c", shell->line[shell->line_curpos]);
738+
shell->line_curpos++;
739+
}
740+
}
741+
continue;
742+
}
679743
}
680744

681745
/* received null or error */
@@ -790,27 +854,44 @@ static void finsh_thread_entry(void *parameter)
790854
{
791855
int i;
792856

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]);
857+
if (shell->overwrite_mode) /* overwrite mode */
858+
{
859+
/* directly overwrite the character */
860+
shell->line[shell->line_curpos] = ch;
861+
if (shell->echo_mode)
862+
rt_kprintf("%c", ch);
863+
shell->line_curpos++;
864+
}
865+
else /* insert mode */
866+
{
867+
shell->line_position++;
868+
/* move existing characters to the right */
869+
rt_memmove(&shell->line[shell->line_curpos + 1],
870+
&shell->line[shell->line_curpos],
871+
shell->line_position - shell->line_curpos);
872+
shell->line[shell->line_curpos] = ch;
799873

800-
/* move the cursor to new position */
801-
for (i = shell->line_curpos; i < shell->line_position; i++)
802-
rt_kprintf("\b");
874+
if (shell->echo_mode)
875+
{
876+
rt_kprintf("%s", &shell->line[shell->line_curpos]);
877+
/* move cursor back to correct position */
878+
for (i = shell->line_curpos + 1; i < shell->line_position; i++)
879+
rt_kprintf("\b");
880+
}
881+
shell->line_curpos++;
882+
}
803883
}
804884
else
805885
{
886+
/* append character at end of line */
806887
shell->line[shell->line_position] = ch;
807888
if (shell->echo_mode)
808889
rt_kprintf("%c", ch);
890+
shell->line_position++;
891+
shell->line_curpos++;
809892
}
810893

811894
ch = 0;
812-
shell->line_position ++;
813-
shell->line_curpos++;
814895
if (shell->line_position >= FINSH_CMD_SIZE)
815896
{
816897
/* 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)