@@ -558,6 +558,10 @@ static void finsh_thread_entry(void *parameter)
558
558
* down key: 0x1b 0x5b 0x42
559
559
* right key:0x1b 0x5b 0x43
560
560
* 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
561
565
*/
562
566
if (ch == 0x1b )
563
567
{
@@ -676,6 +680,66 @@ static void finsh_thread_entry(void *parameter)
676
680
}
677
681
}
678
682
#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
+ }
679
743
}
680
744
681
745
/* received null or error */
@@ -790,27 +854,44 @@ static void finsh_thread_entry(void *parameter)
790
854
{
791
855
int i ;
792
856
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 ;
799
873
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
+ }
803
883
}
804
884
else
805
885
{
886
+ /* append character at end of line */
806
887
shell -> line [shell -> line_position ] = ch ;
807
888
if (shell -> echo_mode )
808
889
rt_kprintf ("%c" , ch );
890
+ shell -> line_position ++ ;
891
+ shell -> line_curpos ++ ;
809
892
}
810
893
811
894
ch = 0 ;
812
- shell -> line_position ++ ;
813
- shell -> line_curpos ++ ;
814
895
if (shell -> line_position >= FINSH_CMD_SIZE )
815
896
{
816
897
/* clear command line */
0 commit comments