6
6
//! - `format` (and `quiet`)
7
7
//! - `logfile`
8
8
9
- use std:: { fs:: File , time:: Duration } ;
9
+ use std:: { fs:: File , io :: Write , time:: Duration } ;
10
10
11
- use termcolor:: { Ansi , Color , ColorChoice , ColorSpec , NoColor , StandardStream , WriteColor } ;
11
+ use anstream:: AutoStream ;
12
+ use anstyle:: { AnsiColor , Color , Style } ;
12
13
13
14
use crate :: {
14
15
Arguments , ColorSetting , Conclusion , Failed , FormatSetting , Measurement , Outcome , TestInfo ,
15
16
Trial ,
16
17
} ;
17
18
18
19
pub ( crate ) struct Printer {
19
- out : Box < dyn WriteColor > ,
20
+ out : Box < dyn Write > ,
20
21
format : FormatSetting ,
21
22
name_width : usize ,
22
23
kind_width : usize ,
@@ -29,20 +30,20 @@ impl Printer {
29
30
let color_arg = args. color . unwrap_or ( ColorSetting :: Auto ) ;
30
31
31
32
// Determine target of all output
32
- let out = if let Some ( logfile) = & args. logfile {
33
+ let out: Box < dyn Write > = if let Some ( logfile) = & args. logfile {
33
34
let f = File :: create ( logfile) . expect ( "failed to create logfile" ) ;
34
35
if color_arg == ColorSetting :: Always {
35
- Box :: new ( Ansi :: new ( f) ) as Box < dyn WriteColor >
36
+ Box :: new ( AutoStream :: always ( f) )
36
37
} else {
37
- Box :: new ( NoColor :: new ( f) )
38
+ Box :: new ( AutoStream :: never ( f) )
38
39
}
39
40
} else {
40
41
let choice = match color_arg {
41
- ColorSetting :: Auto => ColorChoice :: Auto ,
42
- ColorSetting :: Always => ColorChoice :: Always ,
43
- ColorSetting :: Never => ColorChoice :: Never ,
42
+ ColorSetting :: Auto => anstream :: ColorChoice :: Auto ,
43
+ ColorSetting :: Always => anstream :: ColorChoice :: Always ,
44
+ ColorSetting :: Never => anstream :: ColorChoice :: Never ,
44
45
} ;
45
- Box :: new ( StandardStream :: stdout ( choice) )
46
+ Box :: new ( AutoStream :: new ( std :: io :: stdout ( ) , choice) )
46
47
} ;
47
48
48
49
// Determine correct format
@@ -160,9 +161,8 @@ impl Printer {
160
161
}
161
162
} ;
162
163
163
- self . out . set_color ( & color_of_outcome ( outcome) ) . unwrap ( ) ;
164
- write ! ( self . out, "{}" , c) . unwrap ( ) ;
165
- self . out . reset ( ) . unwrap ( ) ;
164
+ let style = color_of_outcome ( outcome) ;
165
+ write ! ( self . out, "{style}{}{style:#}" , c) . unwrap ( ) ;
166
166
}
167
167
FormatSetting :: Json => {
168
168
if let Outcome :: Measured ( Measurement { avg, variance } ) = outcome {
@@ -319,9 +319,8 @@ impl Printer {
319
319
Outcome :: Measured { .. } => "bench" ,
320
320
} ;
321
321
322
- self . out . set_color ( & color_of_outcome ( outcome) ) . unwrap ( ) ;
323
- write ! ( self . out, "{}" , s) . unwrap ( ) ;
324
- self . out . reset ( ) . unwrap ( ) ;
322
+ let style = color_of_outcome ( outcome) ;
323
+ write ! ( self . out, "{style}{}{style:#}" , s) . unwrap ( ) ;
325
324
326
325
if let Outcome :: Measured ( Measurement { avg, variance } ) = outcome {
327
326
write ! (
@@ -347,14 +346,12 @@ pub fn fmt_with_thousand_sep(mut v: u64) -> String {
347
346
}
348
347
349
348
/// Returns the `ColorSpec` associated with the given outcome.
350
- fn color_of_outcome ( outcome : & Outcome ) -> ColorSpec {
351
- let mut out = ColorSpec :: new ( ) ;
349
+ fn color_of_outcome ( outcome : & Outcome ) -> Style {
352
350
let color = match outcome {
353
- Outcome :: Passed => Color :: Green ,
354
- Outcome :: Failed { .. } => Color :: Red ,
355
- Outcome :: Ignored => Color :: Yellow ,
356
- Outcome :: Measured { .. } => Color :: Cyan ,
351
+ Outcome :: Passed => AnsiColor :: Green ,
352
+ Outcome :: Failed { .. } => AnsiColor :: Red ,
353
+ Outcome :: Ignored => AnsiColor :: Yellow ,
354
+ Outcome :: Measured { .. } => AnsiColor :: Cyan ,
357
355
} ;
358
- out. set_fg ( Some ( color) ) ;
359
- out
356
+ Style :: new ( ) . fg_color ( Some ( Color :: Ansi ( color) ) )
360
357
}
0 commit comments