@@ -7,7 +7,6 @@ use clap::CommandFactory;
77use clap:: FromArgMatches ;
88use std:: alloc;
99use std:: ffi:: OsString ;
10- use std:: io:: IsTerminal ;
1110use std:: path:: PathBuf ;
1211use std:: time:: Duration ;
1312use tedge:: command:: BuildCommand ;
@@ -23,6 +22,11 @@ use tedge_config::unconfigured_logger;
2322use tedge_file_log_plugin:: bin:: TEdgeConfigView ;
2423use tracing:: log;
2524
25+ // Control when to use console colors (`stdout` and `stderr` is a TTY, `NO_COLOR` is not set)
26+ static USE_COLOR : yansi:: Condition = yansi:: Condition :: from ( || {
27+ yansi:: Condition :: stdouterr_are_tty ( ) && yansi:: Condition :: no_color ( )
28+ } ) ;
29+
2630#[ global_allocator]
2731static ALLOCATOR : Cap < alloc:: System > = Cap :: new ( alloc:: System , usize:: MAX ) ;
2832
@@ -35,6 +39,8 @@ async fn main() -> anyhow::Result<()> {
3539 } )
3640 . unwrap_or_else ( |code| std:: process:: exit ( code) ) ;
3741
42+ yansi:: whenever ( USE_COLOR ) ;
43+
3844 match opt {
3945 TEdgeOptMulticall :: Component ( Component :: TedgeMapper ( opt) ) => {
4046 let tedge_config = tedge_config:: TEdgeConfig :: load ( & opt. common . config_dir ) . await ?;
@@ -87,10 +93,6 @@ async fn main() -> anyhow::Result<()> {
8793 . build_command ( & tedge_config)
8894 . with_context ( || "missing configuration parameter" ) ?;
8995
90- if !std:: io:: stdout ( ) . is_terminal ( ) {
91- yansi:: disable ( ) ;
92- }
93-
9496 match cmd. execute ( tedge_config) . await {
9597 Ok ( ( ) ) => Ok ( ( ) ) ,
9698 // If the command already prints its own nicely formatted errors
0 commit comments