@@ -19,7 +19,6 @@ mod ws;
19
19
use anyhow:: { Context , Result } ;
20
20
use clap:: { ArgAction , Parser , Subcommand , ValueEnum } ;
21
21
use common:: STARTING ;
22
- use std:: fmt:: Display ;
23
22
use std:: io:: IsTerminal ;
24
23
use std:: path:: PathBuf ;
25
24
use std:: process:: ExitCode ;
@@ -29,20 +28,7 @@ use tracing_subscriber::prelude::*;
29
28
async fn main ( ) -> Result < ExitCode > {
30
29
let cli = Trunk :: parse ( ) ;
31
30
32
- let colored = match cli. color {
33
- TrunkColorArgs :: Always => true ,
34
- TrunkColorArgs :: Never => false ,
35
- TrunkColorArgs :: Auto => {
36
- std:: io:: stdout ( ) . is_terminal ( ) && std:: env:: var_os ( "NO_COLOR" ) . is_none ( )
37
- }
38
- } ;
39
-
40
- #[ cfg( windows) ]
41
- if colored {
42
- if let Err ( err) = ansi_term:: enable_ansi_support ( ) {
43
- eprintln ! ( "error enabling ANSI support: {:?}" , err) ;
44
- }
45
- }
31
+ let colored = init_color ( & cli) ;
46
32
47
33
tracing_subscriber:: registry ( )
48
34
// Filter spans based on the RUST_LOG env var.
@@ -75,6 +61,27 @@ async fn main() -> Result<ExitCode> {
75
61
} )
76
62
}
77
63
64
+ fn init_color ( cli : & Trunk ) -> bool {
65
+ if cli. no_color {
66
+ return false ;
67
+ }
68
+
69
+ let colored = match cli. color {
70
+ ColorMode :: Always => true ,
71
+ ColorMode :: Never => false ,
72
+ ColorMode :: Auto => std:: io:: stdout ( ) . is_terminal ( ) ,
73
+ } ;
74
+
75
+ #[ cfg( windows) ]
76
+ if colored {
77
+ if let Err ( err) = ansi_term:: enable_ansi_support ( ) {
78
+ eprintln ! ( "error enabling ANSI support: {:?}" , err) ;
79
+ }
80
+ }
81
+
82
+ colored
83
+ }
84
+
78
85
fn eval_logging ( cli : & Trunk ) -> tracing_subscriber:: EnvFilter {
79
86
// allow overriding everything with RUST_LOG or --log
80
87
if let Some ( directives) = & cli. log {
@@ -99,6 +106,7 @@ fn eval_logging(cli: &Trunk) -> tracing_subscriber::EnvFilter {
99
106
( 1 , false ) => "error,trunk=debug" ,
100
107
( _, false ) => "error,trunk=trace" ,
101
108
} ;
109
+
102
110
tracing_subscriber:: EnvFilter :: new ( directives)
103
111
}
104
112
@@ -125,28 +133,27 @@ struct Trunk {
125
133
#[ arg( long, global( true ) , env = "TRUNK_SKIP_VERSION_CHECK" ) ]
126
134
pub skip_version_check : bool ,
127
135
128
- /// Arg to specify ansi colored output for tracing logs
129
- #[ arg( long, default_value_t = TrunkColorArgs :: Auto ) ]
130
- pub color : TrunkColorArgs ,
136
+ /// Color mode
137
+ #[ arg( long, env = "TRUNK_COLOR" , global( true ) , value_enum, conflicts_with = "no_color" , default_value_t = ColorMode :: Auto ) ]
138
+ pub color : ColorMode ,
139
+
140
+ /// Support for `NO_COLOR` environment variable
141
+ #[ arg( long, env = "NO_COLOR" , global( true ) ) ]
142
+ pub no_color : bool ,
131
143
}
132
144
133
- #[ derive( ValueEnum , Clone ) ]
134
- enum TrunkColorArgs {
135
- Always ,
145
+ #[ derive( Clone , Debug , Default , ValueEnum ) ]
146
+ #[ value( rename_all = "lower" ) ]
147
+ enum ColorMode {
148
+ /// Enable color when running on a TTY
149
+ #[ default]
136
150
Auto ,
151
+ /// Always enable color
152
+ Always ,
153
+ /// Never enable color
137
154
Never ,
138
155
}
139
156
140
- impl Display for TrunkColorArgs {
141
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
142
- match self {
143
- TrunkColorArgs :: Always => write ! ( f, "always" ) ,
144
- TrunkColorArgs :: Auto => write ! ( f, "auto" ) ,
145
- TrunkColorArgs :: Never => write ! ( f, "never" ) ,
146
- }
147
- }
148
- }
149
-
150
157
impl Trunk {
151
158
#[ tracing:: instrument( level = "trace" , skip( self ) ) ]
152
159
pub async fn run ( self ) -> Result < ( ) > {
0 commit comments