@@ -3,7 +3,7 @@ use std::fmt;
3
3
use std:: io:: { Error , ErrorKind , Write } ;
4
4
use std:: str;
5
5
6
- use unicode_width:: { UnicodeWidthChar , UnicodeWidthStr } ;
6
+ use unicode_width:: UnicodeWidthStr ;
7
7
8
8
use super :: format:: Alignment ;
9
9
@@ -84,54 +84,7 @@ pub fn print_align<T: Write + ?Sized>(
84
84
/// Return the display width of a unicode string.
85
85
/// This functions takes ANSI-escaped color codes into account.
86
86
pub fn display_width ( text : & str ) -> usize {
87
- #[ derive( PartialEq , Eq , Clone , Copy ) ]
88
- enum State {
89
- /// We are not inside any terminal escape.
90
- Normal ,
91
- /// We have just seen a \u{1b}
92
- EscapeChar ,
93
- /// We have just seen a [
94
- OpenBracket ,
95
- /// We just ended the escape by seeing an m
96
- AfterEscape ,
97
- }
98
-
99
- let width = UnicodeWidthStr :: width ( text) ;
100
- let mut state = State :: Normal ;
101
- let mut hidden = 0 ;
102
-
103
- for c in text. chars ( ) {
104
- state = match ( state, c) {
105
- ( State :: Normal , '\u{1b}' ) => State :: EscapeChar ,
106
- ( State :: EscapeChar , '[' ) => State :: OpenBracket ,
107
- ( State :: EscapeChar , _) => State :: Normal ,
108
- ( State :: OpenBracket , 'm' ) => State :: AfterEscape ,
109
- _ => state,
110
- } ;
111
-
112
- // We don't count escape characters as hidden as
113
- // UnicodeWidthStr::width already considers them.
114
- if matches ! ( state, State :: OpenBracket | State :: AfterEscape ) {
115
- // but if we see an escape char *inside* the ANSI escape, we should ignore it.
116
- if UnicodeWidthChar :: width ( c) . unwrap_or ( 0 ) > 0 {
117
- hidden += 1 ;
118
- }
119
- }
120
-
121
- if state == State :: AfterEscape {
122
- state = State :: Normal ;
123
- }
124
- }
125
-
126
- assert ! (
127
- width >= hidden,
128
- "internal error: width {} less than hidden {} on string {:?}" ,
129
- width,
130
- hidden,
131
- text
132
- ) ;
133
-
134
- width - hidden
87
+ UnicodeWidthStr :: width ( strip_ansi_escapes:: strip_str ( text) . as_str ( ) )
135
88
}
136
89
137
90
/// Wrapper struct which will emit the HTML-escaped version of the contained
0 commit comments