@@ -8,60 +8,9 @@ use unicode_width::UnicodeWidthStr;
8
8
9
9
use typos_cli:: report:: { Context , Message , Report , Typo } ;
10
10
11
- #[ derive( Copy , Clone , Debug , Default ) ]
12
- pub struct Palette {
13
- error : anstyle:: Style ,
14
- info : anstyle:: Style ,
15
- strong : anstyle:: Style ,
16
- }
17
-
18
- impl Palette {
19
- pub fn colored ( ) -> Self {
20
- Self {
21
- error : anstyle:: AnsiColor :: Red . on_default ( ) ,
22
- info : anstyle:: AnsiColor :: Blue . on_default ( ) ,
23
- strong : anstyle:: Effects :: BOLD . into ( ) ,
24
- }
25
- }
26
-
27
- pub ( crate ) fn error < D : std:: fmt:: Display > ( self , display : D ) -> Styled < D > {
28
- Styled :: new ( display, self . error )
29
- }
30
-
31
- pub ( crate ) fn info < D : std:: fmt:: Display > ( self , display : D ) -> Styled < D > {
32
- Styled :: new ( display, self . info )
33
- }
34
-
35
- pub ( crate ) fn strong < D : std:: fmt:: Display > ( self , display : D ) -> Styled < D > {
36
- Styled :: new ( display, self . strong )
37
- }
38
- }
39
-
40
- #[ derive( Debug ) ]
41
- pub ( crate ) struct Styled < D > {
42
- display : D ,
43
- style : anstyle:: Style ,
44
- }
45
-
46
- impl < D : std:: fmt:: Display > Styled < D > {
47
- pub ( crate ) fn new ( display : D , style : anstyle:: Style ) -> Self {
48
- Self { display, style }
49
- }
50
- }
51
-
52
- impl < D : std:: fmt:: Display > std:: fmt:: Display for Styled < D > {
53
- #[ inline]
54
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
55
- if f. alternate ( ) {
56
- write ! ( f, "{}" , self . style. render( ) ) ?;
57
- self . display . fmt ( f) ?;
58
- write ! ( f, "{}" , self . style. render_reset( ) ) ?;
59
- Ok ( ( ) )
60
- } else {
61
- self . display . fmt ( f)
62
- }
63
- }
64
- }
11
+ const ERROR : anstyle:: Style = anstyle:: AnsiColor :: Red . on_default ( ) ;
12
+ const INFO : anstyle:: Style = anstyle:: AnsiColor :: Blue . on_default ( ) ;
13
+ const STRONG : anstyle:: Style = anstyle:: Style :: new ( ) . effects ( anstyle:: Effects :: BOLD ) ;
65
14
66
15
pub struct MessageStatus < ' r > {
67
16
typos_found : atomic:: AtomicBool ,
@@ -108,18 +57,15 @@ impl Report for PrintSilent {
108
57
}
109
58
}
110
59
111
- pub struct PrintBrief {
112
- pub stdout_palette : Palette ,
113
- pub stderr_palette : Palette ,
114
- }
60
+ pub struct PrintBrief ;
115
61
116
62
impl Report for PrintBrief {
117
63
fn report ( & self , msg : Message ) -> Result < ( ) , std:: io:: Error > {
118
64
match & msg {
119
65
Message :: BinaryFile ( msg) => {
120
66
log:: info!( "{}" , msg) ;
121
67
}
122
- Message :: Typo ( msg) => print_brief_correction ( msg, self . stdout_palette ) ?,
68
+ Message :: Typo ( msg) => print_brief_correction ( msg) ?,
123
69
Message :: FileType ( msg) => {
124
70
writeln ! (
125
71
stdout( ) . lock( ) ,
@@ -143,18 +89,15 @@ impl Report for PrintBrief {
143
89
}
144
90
}
145
91
146
- pub struct PrintLong {
147
- pub stdout_palette : Palette ,
148
- pub stderr_palette : Palette ,
149
- }
92
+ pub struct PrintLong ;
150
93
151
94
impl Report for PrintLong {
152
95
fn report ( & self , msg : Message ) -> Result < ( ) , std:: io:: Error > {
153
96
match & msg {
154
97
Message :: BinaryFile ( msg) => {
155
98
log:: info!( "{}" , msg) ;
156
99
}
157
- Message :: Typo ( msg) => print_long_correction ( msg, self . stdout_palette ) ?,
100
+ Message :: Typo ( msg) => print_long_correction ( msg) ?,
158
101
Message :: FileType ( msg) => {
159
102
writeln ! (
160
103
stdout( ) . lock( ) ,
@@ -178,7 +121,11 @@ impl Report for PrintLong {
178
121
}
179
122
}
180
123
181
- fn print_brief_correction ( msg : & Typo , palette : Palette ) -> Result < ( ) , std:: io:: Error > {
124
+ fn print_brief_correction ( msg : & Typo ) -> Result < ( ) , std:: io:: Error > {
125
+ let info = INFO . render ( ) ;
126
+ let strong = STRONG . render ( ) ;
127
+ let reset = anstyle:: Reset . render ( ) ;
128
+
182
129
let start = String :: from_utf8_lossy ( & msg. buffer [ 0 ..msg. byte_offset ] ) ;
183
130
let column_number =
184
131
unicode_segmentation:: UnicodeSegmentation :: graphemes ( start. as_ref ( ) , true ) . count ( ) + 1 ;
@@ -188,34 +135,32 @@ fn print_brief_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::E
188
135
let divider = ":" ;
189
136
writeln ! (
190
137
stdout( ) . lock( ) ,
191
- "{:#}{:#}{:#}: {:#}" ,
192
- palette. info( context_display( & msg. context) ) ,
193
- palette. info( divider) ,
194
- palette. info( column_number) ,
195
- palette. strong( format_args!( "`{}` is disallowed:" , msg. typo) ) ,
138
+ "{info}{}{divider}{column_number}{reset}: {strong}`{}` is disallowed{reset}" ,
139
+ context_display( & msg. context) ,
140
+ msg. typo,
196
141
) ?;
197
142
}
198
143
typos:: Status :: Corrections ( corrections) => {
199
144
let divider = ":" ;
200
145
writeln ! (
201
146
stdout( ) . lock( ) ,
202
- "{:#}{:#}{:#}: {:#}" ,
203
- palette. info( context_display( & msg. context) ) ,
204
- palette. info( divider) ,
205
- palette. info( column_number) ,
206
- palette. strong( format_args!(
207
- "`{}` -> {}" ,
208
- msg. typo,
209
- itertools:: join( corrections. iter( ) . map( |s| format!( "`{}`" , s) ) , ", " )
210
- ) ) ,
147
+ "{info}{}{divider}{column_number}{reset}: {strong}`{}` -> {}{reset}" ,
148
+ context_display( & msg. context) ,
149
+ msg. typo,
150
+ itertools:: join( corrections. iter( ) . map( |s| format!( "`{}`" , s) ) , ", " )
211
151
) ?;
212
152
}
213
153
}
214
154
215
155
Ok ( ( ) )
216
156
}
217
157
218
- fn print_long_correction ( msg : & Typo , palette : Palette ) -> Result < ( ) , std:: io:: Error > {
158
+ fn print_long_correction ( msg : & Typo ) -> Result < ( ) , std:: io:: Error > {
159
+ let error = ERROR . render ( ) ;
160
+ let info = INFO . render ( ) ;
161
+ let strong = STRONG . render ( ) ;
162
+ let reset = anstyle:: Reset . render ( ) ;
163
+
219
164
let stdout = stdout ( ) ;
220
165
let mut handle = stdout. lock ( ) ;
221
166
@@ -229,53 +174,44 @@ fn print_long_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::Er
229
174
typos:: Status :: Invalid => {
230
175
writeln ! (
231
176
handle,
232
- "{:#}: {:#}" ,
233
- palette. error( "error" ) ,
234
- palette. strong( format_args!( "`{}` is disallowed" , msg. typo) )
177
+ "{error}error{reset}: {strong}`{}` is disallowed{reset}" ,
178
+ msg. typo,
235
179
) ?;
236
180
}
237
181
typos:: Status :: Corrections ( corrections) => {
238
182
writeln ! (
239
183
handle,
240
- "{:#}: {:#}" ,
241
- palette. error( "error" ) ,
242
- palette. strong( format_args!(
243
- "`{}` should be {}" ,
244
- msg. typo,
245
- itertools:: join( corrections. iter( ) . map( |s| format!( "`{}`" , s) ) , ", " )
246
- ) )
184
+ "{error}error{reset}: {strong}`{}` should be {}{reset}" ,
185
+ msg. typo,
186
+ itertools:: join( corrections. iter( ) . map( |s| format!( "`{}`" , s) ) , ", " )
247
187
) ?;
248
188
}
249
189
}
250
190
let divider = ":" ;
251
191
writeln ! (
252
192
handle,
253
- " --> {:#}{:#}{:#}" ,
254
- palette. info( context_display( & msg. context) ) ,
255
- palette. info( divider) ,
256
- palette. info( column_number)
193
+ "{info} --> {reset}{}{divider}{column_number}" ,
194
+ context_display( & msg. context) ,
257
195
) ?;
258
196
259
197
if let Some ( Context :: File ( context) ) = & msg. context {
260
198
let line_num = context. line_num . to_string ( ) ;
261
199
let line_indent: String = itertools:: repeat_n ( " " , line_num. len ( ) ) . collect ( ) ;
200
+ let line = line. trim_end ( ) ;
262
201
263
202
let visible_column = calculate_visible_column_width ( start. as_ref ( ) ) ;
264
203
let visible_len = calculate_visible_column_width ( msg. typo ) ;
265
204
266
205
let hl_indent: String = itertools:: repeat_n ( " " , visible_column) . collect ( ) ;
267
206
let hl: String = itertools:: repeat_n ( "^" , visible_len) . collect ( ) ;
268
207
269
- writeln ! ( handle, "{} |" , line_indent ) ?;
270
- writeln ! ( handle, "{:#} | {}" , palette . info ( line_num ) , line. trim_end ( ) ) ?;
208
+ writeln ! ( handle, "{info}{line_indent} |{reset}" ) ?;
209
+ writeln ! ( handle, "{info}{line_num} |{reset} { line}" ) ?;
271
210
writeln ! (
272
211
handle,
273
- "{} | {}{:#}" ,
274
- line_indent,
275
- hl_indent,
276
- palette. error( hl)
212
+ "{info}{line_indent} |{reset} {hl_indent}{error}{hl}{reset}" ,
277
213
) ?;
278
- writeln ! ( handle, "{} |" , line_indent ) ?;
214
+ writeln ! ( handle, "{info}{line_indent} |{reset}" ) ?;
279
215
}
280
216
281
217
Ok ( ( ) )
0 commit comments