@@ -25,6 +25,68 @@ impl<R: CellRenderer> CellCanvas<R> {
2525 }
2626 }
2727
28+ fn write_ansi_background_color < W : Write > ( w : & mut W , color : Color ) -> fmt:: Result {
29+ match color {
30+ Color :: Black => w. write_str ( "\x1b [40m" ) ,
31+ Color :: Red => w. write_str ( "\x1b [41m" ) ,
32+ Color :: Green => w. write_str ( "\x1b [42m" ) ,
33+ Color :: Yellow => w. write_str ( "\x1b [43m" ) ,
34+ Color :: Blue => w. write_str ( "\x1b [44m" ) ,
35+ Color :: Magenta => w. write_str ( "\x1b [45m" ) ,
36+ Color :: Cyan => w. write_str ( "\x1b [46m" ) ,
37+ Color :: White => w. write_str ( "\x1b [47m" ) ,
38+ Color :: BrightBlack => w. write_str ( "\x1b [100m" ) ,
39+ Color :: BrightRed => w. write_str ( "\x1b [101m" ) ,
40+ Color :: BrightGreen => w. write_str ( "\x1b [102m" ) ,
41+ Color :: BrightYellow => w. write_str ( "\x1b [103m" ) ,
42+ Color :: BrightBlue => w. write_str ( "\x1b [104m" ) ,
43+ Color :: BrightMagenta => w. write_str ( "\x1b [105m" ) ,
44+ Color :: BrightCyan => w. write_str ( "\x1b [106m" ) ,
45+ Color :: BrightWhite => w. write_str ( "\x1b [107m" ) ,
46+ Color :: TrueColor { r, g, b } => write ! ( w, "\x1b [48;2;{};{};{}m" , r, g, b) ,
47+ }
48+ }
49+
50+ fn write_style < W : Write > (
51+ w : & mut W ,
52+ foreground : Option < Color > ,
53+ background : Option < Color > ,
54+ last_foreground : & mut Option < Color > ,
55+ last_background : & mut Option < Color > ,
56+ ) -> fmt:: Result {
57+ if foreground == * last_foreground && background == * last_background {
58+ return Ok ( ( ) ) ;
59+ }
60+
61+ let needs_reset = ( foreground. is_none ( ) && last_foreground. is_some ( ) )
62+ || ( background. is_none ( ) && last_background. is_some ( ) ) ;
63+
64+ if needs_reset {
65+ w. write_str ( "\x1b [0m" ) ?;
66+ if let Some ( bg) = background {
67+ Self :: write_ansi_background_color ( w, bg) ?;
68+ }
69+ if let Some ( fg) = foreground {
70+ Self :: write_ansi_color ( w, fg) ?;
71+ }
72+ } else {
73+ if background != * last_background {
74+ if let Some ( bg) = background {
75+ Self :: write_ansi_background_color ( w, bg) ?;
76+ }
77+ }
78+ if foreground != * last_foreground {
79+ if let Some ( fg) = foreground {
80+ Self :: write_ansi_color ( w, fg) ?;
81+ }
82+ }
83+ }
84+
85+ * last_foreground = foreground;
86+ * last_background = background;
87+ Ok ( ( ) )
88+ }
89+
2890 pub fn render_to < W : Write > (
2991 & self ,
3092 w : & mut W ,
@@ -44,7 +106,8 @@ impl<R: CellRenderer> CellCanvas<R> {
44106 w. write_char ( '\n' ) ?;
45107 }
46108
47- let mut last_color: Option < Color > = None ;
109+ let mut last_foreground: Option < Color > = None ;
110+ let mut last_background: Option < Color > = None ;
48111
49112 for row in 0 ..self . height {
50113 if show_border {
@@ -53,27 +116,28 @@ impl<R: CellRenderer> CellCanvas<R> {
53116
54117 for col in 0 ..self . width {
55118 let idx = self . idx ( col, row) ;
56- let char_to_print = if let Some ( c ) = self . text_layer [ idx ] {
57- c
58- } else {
59- R :: glyph ( self . buffer [ idx] )
60- } ;
61-
62- let current_color = self . colors [ idx ] ;
63- if current_color != last_color {
64- match current_color {
65- Some ( c ) => Self :: write_ansi_color ( w , c ) ? ,
66- None => w . write_str ( " \x1b [0m" ) ? ,
67- }
68- last_color = current_color ;
69- }
119+ let appearance = R :: appearance (
120+ self . buffer [ idx ] ,
121+ self . colors [ idx ] ,
122+ self . background_colors [ idx] ,
123+ self . text_layer [ idx ] ,
124+ ) ;
125+
126+ Self :: write_style (
127+ w ,
128+ appearance . foreground ,
129+ appearance . background ,
130+ & mut last_foreground ,
131+ & mut last_background ,
132+ ) ? ;
70133
71- w. write_char ( char_to_print ) ?;
134+ w. write_char ( appearance . glyph ) ?;
72135 }
73136
74- if last_color . is_some ( ) {
137+ if last_foreground . is_some ( ) || last_background . is_some ( ) {
75138 w. write_str ( "\x1b [0m" ) ?;
76- last_color = None ;
139+ last_foreground = None ;
140+ last_background = None ;
77141 }
78142
79143 if show_border {
0 commit comments