1616use std:: borrow:: Cow ;
1717use std:: fmt:: { Display , Write } ;
1818
19- use nu_ansi_term:: { Color , Style } ;
20-
2119use crate :: matcher_support:: edit_distance;
2220
2321/// Environment variable controlling the usage of ansi color in difference
@@ -154,26 +152,34 @@ fn compress_common_lines(common_lines: Vec<&str>) -> String {
154152 truncated_lines
155153}
156154
155+ // Use ANSI code to enable styling on the summary lines.
156+ //
157+ // See https://en.wikipedia.org/wiki/ANSI_escape_code.
157158struct LineStyle {
158- ansi : Style ,
159+ ansi_prefix : & ' static str ,
160+ ansi_suffix : & ' static str ,
159161 header : char ,
160162}
161163
162164impl LineStyle {
165+ // Font in red and bold
163166 fn extra_actual_style ( ) -> Self {
164- Self { ansi : Style :: new ( ) . fg ( Color :: Red ) . bold ( ) , header : '-' }
167+ Self { ansi_prefix : " \x1B [1;31m" , ansi_suffix : " \x1B [0m" , header : '-' }
165168 }
166169
170+ // Font in green and bold
167171 fn extra_expected_style ( ) -> Self {
168- Self { ansi : Style :: new ( ) . fg ( Color :: Green ) . bold ( ) , header : '+' }
172+ Self { ansi_prefix : " \x1B [1;32m" , ansi_suffix : " \x1B [0m" , header : '+' }
169173 }
170174
175+ // Font in italic
171176 fn comment_style ( ) -> Self {
172- Self { ansi : Style :: new ( ) . italic ( ) , header : ' ' }
177+ Self { ansi_prefix : " \x1B [3m" , ansi_suffix : " \x1B [0m" , header : ' ' }
173178 }
174179
180+ // No ansi styling
175181 fn unchanged_style ( ) -> Self {
176- Self { ansi : Style :: new ( ) , header : ' ' }
182+ Self { ansi_prefix : "" , ansi_suffix : "" , header : ' ' }
177183 }
178184
179185 fn style ( self , line : & str ) -> StyledLine < ' _ > {
@@ -192,10 +198,7 @@ impl<'a> Display for StyledLine<'a> {
192198 write ! (
193199 f,
194200 "{}{}{}{}" ,
195- self . style. header,
196- self . style. ansi. prefix( ) ,
197- self . line,
198- self . style. ansi. suffix( )
201+ self . style. header, self . style. ansi_prefix, self . line, self . style. ansi_suffix
199202 )
200203 } else {
201204 write ! ( f, "{}{}" , self . style. header, self . line)
0 commit comments