2121#include <rthw.h>
2222#include <string.h>
2323#include <stdio.h>
24-
24+ #include "ansi.h"
2525#ifdef RT_USING_FINSH
2626
2727#include "shell.h"
@@ -409,7 +409,7 @@ static rt_bool_t shell_handle_history(struct finsh_shell *shell)
409409 rt_kprintf ("\r" );
410410
411411#else
412- rt_kprintf ("\033[2K\r" );
412+ rt_kprintf (ANSI_CLR_LINE );
413413#endif
414414 rt_kprintf ("%s%s" , FINSH_PROMPT , shell -> line );
415415 return RT_FALSE ;
@@ -693,7 +693,7 @@ static void finsh_thread_entry(void *parameter)
693693 int new_pos = find_prev_word_start (shell -> line , shell -> line_curpos );
694694 if (new_pos != shell -> line_curpos )
695695 {
696- rt_kprintf ("\033[%dD" , shell -> line_curpos - new_pos );
696+ rt_kprintf (ANSI_FMT_CUB , shell -> line_curpos - new_pos );
697697 shell -> line_curpos = new_pos ;
698698 }
699699 continue ;
@@ -703,7 +703,7 @@ static void finsh_thread_entry(void *parameter)
703703 int new_pos = find_next_word_end (shell -> line , shell -> line_curpos , shell -> line_position );
704704 if (new_pos != shell -> line_curpos )
705705 {
706- rt_kprintf ("\033[%dC" , new_pos - shell -> line_curpos );
706+ rt_kprintf (ANSI_FMT_CUF , new_pos - shell -> line_curpos );
707707 shell -> line_curpos = new_pos ;
708708 }
709709 continue ;
@@ -738,11 +738,8 @@ static void finsh_thread_entry(void *parameter)
738738 if (key_code == 0x31 ) /* home key */
739739 {
740740 /* move cursor to beginning of line */
741- while (shell -> line_curpos > 0 )
742- {
743- rt_kprintf ("\b" );
744- shell -> line_curpos -- ;
745- }
741+ rt_kprintf (ANSI_FMT_CUB , shell -> line_curpos );
742+ shell -> line_curpos = 0 ;
746743 }
747744 else if (key_code == 0x32 ) /* insert key */
748745 {
@@ -755,7 +752,6 @@ static void finsh_thread_entry(void *parameter)
755752 if (finsh_shell_check_line (shell ) &&
756753 (shell -> line_curpos < shell -> line_position ))
757754 {
758- int i ;
759755 shell -> line_position -- ;
760756 rt_memmove (& shell -> line [shell -> line_curpos ],
761757 & shell -> line [shell -> line_curpos + 1 ],
@@ -766,18 +762,14 @@ static void finsh_thread_entry(void *parameter)
766762 rt_kprintf ("%s " , & shell -> line [shell -> line_curpos ]);
767763
768764 /* move cursor back to original position */
769- for (i = shell -> line_curpos ; i <= shell -> line_position ; i ++ )
770- rt_kprintf ("\b" );
765+ rt_kprintf (ANSI_FMT_CUB , shell -> line_position - shell -> line_curpos + 1 );
771766 }
772767 }
773768 else if (key_code == 0x34 ) /* end key */
774769 {
775770 /* move cursor to end of line */
776- while (shell -> line_curpos < shell -> line_position )
777- {
778- rt_kprintf ("%c" , shell -> line [shell -> line_curpos ]);
779- shell -> line_curpos ++ ;
780- }
771+ rt_kprintf (ANSI_FMT_CUF , shell -> line_position - shell -> line_curpos );
772+ shell -> line_curpos = shell -> line_position ;
781773 }
782774 continue ;
783775 }
@@ -792,10 +784,8 @@ static void finsh_thread_entry(void *parameter)
792784 /* handle tab key */
793785 else if (ch == '\t' )
794786 {
795- int i ;
796787 /* move the cursor to the beginning of line */
797- for (i = 0 ; i < shell -> line_curpos ; i ++ )
798- rt_kprintf ("\b" );
788+ rt_kprintf (ANSI_FMT_CUB , shell -> line_curpos );
799789
800790 /* auto complete */
801791 shell_auto_complete (& shell -> line [0 ]);
@@ -816,8 +806,6 @@ static void finsh_thread_entry(void *parameter)
816806
817807 if (shell -> line_position > shell -> line_curpos )
818808 {
819- int i ;
820-
821809 rt_memmove (& shell -> line [shell -> line_curpos ],
822810 & shell -> line [shell -> line_curpos + 1 ],
823811 shell -> line_position - shell -> line_curpos );
@@ -826,8 +814,7 @@ static void finsh_thread_entry(void *parameter)
826814 rt_kprintf ("\b%s \b" , & shell -> line [shell -> line_curpos ]);
827815
828816 /* move the cursor to the origin position */
829- for (i = shell -> line_curpos ; i <= shell -> line_position ; i ++ )
830- rt_kprintf ("\b" );
817+ rt_kprintf (ANSI_FMT_CUB , shell -> line_position - shell -> line_curpos + 1 );
831818 }
832819 else
833820 {
@@ -860,15 +847,15 @@ static void finsh_thread_entry(void *parameter)
860847 shell -> line_curpos = start ;
861848
862849 /* Redraw the affected line section */
863- rt_kprintf ("\033[%dD" , del_count );
850+ rt_kprintf (ANSI_FMT_CUB , del_count );
864851 /* Rewrite the remaining content */
865852 rt_kprintf ("%.*s" , shell -> line_position - start , & shell -> line [start ]);
866853 /* Clear trailing artifacts */
867- rt_kprintf ("\033[K" );
854+ rt_kprintf (ANSI_EL_LINE );
868855 if (shell -> line_position > start )
869856 {
870857 /* Reset cursor */
871- rt_kprintf ("\033[%dD" , shell -> line_position - start );
858+ rt_kprintf (ANSI_FMT_CUB , shell -> line_position - start );
872859 }
873860
874861 continue ;
@@ -899,7 +886,6 @@ static void finsh_thread_entry(void *parameter)
899886 /* normal character */
900887 if (shell -> line_curpos < shell -> line_position )
901888 {
902- int i ;
903889#if defined(FINSH_USING_FUNC_EXT )
904890 if (shell -> overwrite_mode ) /* overwrite mode */
905891 {
@@ -923,8 +909,8 @@ static void finsh_thread_entry(void *parameter)
923909 {
924910 rt_kprintf ("%s" , & shell -> line [shell -> line_curpos ]);
925911 /* move cursor back to correct position */
926- for ( i = shell -> line_curpos + 1 ; i < shell -> line_position ; i ++ )
927- rt_kprintf ("\b" );
912+ if ( shell -> line_position > shell -> line_curpos + 1 )
913+ rt_kprintf (ANSI_FMT_CUB , shell -> line_position - shell -> line_curpos - 1 );
928914 }
929915 shell -> line_curpos ++ ;
930916 }
0 commit comments