@@ -155,7 +155,7 @@ impl<'a> TtfConsole<'a> {
155155 font_baseline : 0.0 ,
156156 cursor_needs_redraw : true ,
157157 glyph_cache : GlyphCache :: new ( GLYPH_CACHE_SIZE ) ,
158- hidden_cursor : false ,
158+ hidden_cursor : true ,
159159 ansi_parse_state : AnsiParseState :: Normal ,
160160 ansi_params : [ 0 ; MAX_ANSI_PARAMS ] ,
161161 ansi_param_count : 0 ,
@@ -377,7 +377,8 @@ impl<'a> TtfConsole<'a> {
377377 }
378378 }
379379
380- pub fn put_char ( & mut self , ch : char ) {
380+ /// 将字符写入当前光标位置(不移动光标)
381+ fn put_char_impl ( & mut self , ch : char ) {
381382 self . ensure_buffer_capacity ( ) ;
382383
383384 let current_buf_y = ( self . cursor_y + self . scroll_offset_y as u32 ) as usize ;
@@ -615,7 +616,7 @@ impl<'a> TtfConsole<'a> {
615616 spaces_to_add = TAB_SPACES as u32 ;
616617 }
617618 for _ in 0 ..spaces_to_add {
618- self . put_char ( ' ' ) ;
619+ self . put_char_impl ( ' ' ) ;
619620 self . cursor_x += 1 ;
620621 if self . cursor_x >= self . width_chars {
621622 self . cursor_x = 0 ;
@@ -626,7 +627,7 @@ impl<'a> TtfConsole<'a> {
626627 }
627628 }
628629 _ => {
629- self . put_char ( c) ;
630+ self . put_char_impl ( c) ;
630631 self . cursor_x += 1 ;
631632 if self . cursor_x >= self . width_chars {
632633 self . cursor_x = 0 ;
@@ -780,3 +781,124 @@ impl Write for TtfConsole<'_> {
780781 Ok ( ( ) )
781782 }
782783}
784+
785+ // Implement the unified [`Console`] trait
786+ impl < ' a > crate :: output:: console:: Console for TtfConsole < ' a > {
787+ fn clear ( & mut self ) {
788+ self . buffer . fill ( None ) ;
789+ self . cursor_x = 0 ;
790+ self . cursor_y = 0 ;
791+ self . scroll_offset_y = 0 ;
792+ self . cursor_needs_redraw = true ;
793+ self . redraw ( ) ;
794+ }
795+
796+ fn set_fg_color ( & mut self , color : Color ) {
797+ if self . current_color != color {
798+ self . current_color = color;
799+ self . cursor_needs_redraw = true ;
800+ }
801+ }
802+
803+ fn set_bg_color ( & mut self , color : Color ) {
804+ if self . current_bg_color != color {
805+ self . current_bg_color = color;
806+ self . renderer . set_clear_color ( color) ;
807+ self . cursor_needs_redraw = true ;
808+ self . redraw ( ) ;
809+ }
810+ }
811+
812+ fn get_fg_color ( & self ) -> Color {
813+ self . current_color
814+ }
815+
816+ fn get_bg_color ( & self ) -> Color {
817+ self . current_bg_color
818+ }
819+
820+ fn put_char ( & mut self , ch : char ) {
821+ // 使用现有的 handle_normal_char 方法处理字符
822+ // 但对于普通字符,直接调用 put_char 并更新位置
823+ match ch {
824+ '\n' => {
825+ self . cursor_x = 0 ;
826+ self . cursor_y += 1 ;
827+ self . ensure_buffer_capacity ( ) ;
828+ }
829+ '\r' => {
830+ self . cursor_x = 0 ;
831+ }
832+ '\t' => {
833+ let mut spaces_to_add = TAB_SPACES as u32 - ( self . cursor_x % TAB_SPACES as u32 ) ;
834+ if spaces_to_add == 0 {
835+ spaces_to_add = TAB_SPACES as u32 ;
836+ }
837+ for _ in 0 ..spaces_to_add {
838+ self . put_char_impl ( ' ' ) ;
839+ self . cursor_x += 1 ;
840+ if self . cursor_x >= self . width_chars {
841+ self . cursor_x = 0 ;
842+ self . cursor_y += 1 ;
843+ self . ensure_buffer_capacity ( ) ;
844+ break ;
845+ }
846+ }
847+ }
848+ _ => {
849+ self . put_char_impl ( ch) ;
850+ self . cursor_x += 1 ;
851+ if self . cursor_x >= self . width_chars {
852+ self . cursor_x = 0 ;
853+ self . cursor_y += 1 ;
854+ self . ensure_buffer_capacity ( ) ;
855+ }
856+ }
857+ }
858+ self . cursor_needs_redraw = true ;
859+ self . draw_cursor ( ) ;
860+ self . renderer . present ( ) ;
861+ }
862+
863+ fn cursor_up ( & mut self , lines : u32 ) {
864+ self . cursor_y = self . cursor_y . saturating_sub ( lines) ;
865+ self . cursor_needs_redraw = true ;
866+ }
867+
868+ fn cursor_down ( & mut self , lines : u32 ) {
869+ self . cursor_y = ( self . cursor_y + lines) . min ( self . height_chars - 1 ) ;
870+ self . ensure_buffer_capacity ( ) ;
871+ self . cursor_needs_redraw = true ;
872+ }
873+
874+ fn cursor_left ( & mut self , cols : u32 ) {
875+ self . cursor_x = self . cursor_x . saturating_sub ( cols) ;
876+ self . cursor_needs_redraw = true ;
877+ }
878+
879+ fn cursor_right ( & mut self , cols : u32 ) {
880+ self . cursor_x = ( self . cursor_x + cols) . min ( self . width_chars - 1 ) ;
881+ self . cursor_needs_redraw = true ;
882+ }
883+
884+ fn set_cursor_pos ( & mut self , x : u32 , y : u32 ) {
885+ self . cursor_x = x. min ( self . width_chars - 1 ) ;
886+ self . cursor_y = y;
887+ self . ensure_buffer_capacity ( ) ;
888+ self . cursor_needs_redraw = true ;
889+ }
890+
891+ fn get_cursor_pos ( & self ) -> ( u32 , u32 ) {
892+ ( self . cursor_x , self . cursor_y )
893+ }
894+
895+ fn cursor_hide ( & mut self ) {
896+ self . hidden_cursor = true ;
897+ self . cursor_needs_redraw = true ;
898+ }
899+
900+ fn cursor_show ( & mut self ) {
901+ self . hidden_cursor = false ;
902+ self . cursor_needs_redraw = true ;
903+ }
904+ }
0 commit comments