@@ -123,21 +123,7 @@ pub fn pad_visible(text: &str, width: usize) -> String {
123123}
124124
125125pub fn strip_ansi ( text : & str ) -> String {
126- let mut out = String :: with_capacity ( text. len ( ) ) ;
127- let mut chars = text. chars ( ) . peekable ( ) ;
128- while let Some ( ch) = chars. next ( ) {
129- if ch == '\u{1b}' && chars. peek ( ) == Some ( & '[' ) {
130- chars. next ( ) ;
131- for code in chars. by_ref ( ) {
132- if code. is_ascii_alphabetic ( ) {
133- break ;
134- }
135- }
136- continue ;
137- }
138- out. push ( ch) ;
139- }
140- out
126+ crate :: ui:: strip_ansi ( text)
141127}
142128
143129fn body_lines ( card : & Card , plain : bool , color : bool ) -> Vec < String > {
@@ -256,15 +242,15 @@ fn border(position: &str, width: usize, plain: bool, color: bool) -> String {
256242
257243fn style_border ( text : & str , color : bool ) -> String {
258244 if color {
259- format ! ( " \u{1b} [2m{text} \u{1b} [0m" )
245+ crate :: ui :: ansi_wrap ( "2" , text )
260246 } else {
261247 text. to_string ( )
262248 }
263249}
264250
265251fn style_title ( text : & str , color : bool ) -> String {
266252 if color {
267- format ! ( " \u{1b} [1m{text} \u{1b} [0m" )
253+ crate :: ui :: ansi_wrap ( "1" , text )
268254 } else {
269255 text. to_string ( )
270256 }
@@ -281,7 +267,7 @@ fn style_tone(text: &str, tone: Tone, color: bool) -> String {
281267 Tone :: Bad => "31" ,
282268 Tone :: Dim => "2" ,
283269 } ;
284- format ! ( " \u{1b} [{ code}m{ text} \u{1b} [0m" )
270+ crate :: ui :: ansi_wrap ( code, text)
285271}
286272
287273fn truncate_visible_for_mode ( text : & str , width : usize , plain : bool ) -> String {
@@ -310,18 +296,8 @@ fn truncate_visible_inner(text: &str, width: usize, ellipsis: &str) -> String {
310296 let mut active_style = false ;
311297 let mut chars = text. chars ( ) . peekable ( ) ;
312298 while let Some ( ch) = chars. next ( ) {
313- if ch == '\u{1b}' && chars. peek ( ) == Some ( & '[' ) {
314- let mut sequence = String :: from ( ch) ;
315- if let Some ( bracket) = chars. next ( ) {
316- sequence. push ( bracket) ;
317- }
318- for code in chars. by_ref ( ) {
319- sequence. push ( code) ;
320- if code. is_ascii_alphabetic ( ) {
321- active_style = sequence != "\u{1b} [0m" ;
322- break ;
323- }
324- }
299+ if let Some ( ( sequence, active) ) = crate :: ui:: read_ansi_sequence ( ch, & mut chars) {
300+ active_style = active;
325301 out. push_str ( & sequence) ;
326302 continue ;
327303 }
@@ -333,7 +309,7 @@ fn truncate_visible_inner(text: &str, width: usize, ellipsis: &str) -> String {
333309 }
334310 out. push_str ( ellipsis) ;
335311 if active_style {
336- out. push_str ( " \u{1b} [0m" ) ;
312+ out. push_str ( crate :: ui :: ansi_reset ( ) ) ;
337313 }
338314 out
339315}
0 commit comments