Skip to content

Commit 7713bd9

Browse files
committed
chore: rework --color argument a bit
* Use `value_enum` on arg to automate possible values and Display trait * Add `NO_COLOR` to the arguments to be able to raise a conflict * Extract color eval to its own function
1 parent c8eb288 commit 7713bd9

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

src/main.rs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ mod ws;
1919
use anyhow::{Context, Result};
2020
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
2121
use common::STARTING;
22-
use std::fmt::Display;
2322
use std::io::IsTerminal;
2423
use std::path::PathBuf;
2524
use std::process::ExitCode;
@@ -29,20 +28,7 @@ use tracing_subscriber::prelude::*;
2928
async fn main() -> Result<ExitCode> {
3029
let cli = Trunk::parse();
3130

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);
4632

4733
tracing_subscriber::registry()
4834
// Filter spans based on the RUST_LOG env var.
@@ -75,6 +61,27 @@ async fn main() -> Result<ExitCode> {
7561
})
7662
}
7763

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+
7885
fn eval_logging(cli: &Trunk) -> tracing_subscriber::EnvFilter {
7986
// allow overriding everything with RUST_LOG or --log
8087
if let Some(directives) = &cli.log {
@@ -99,6 +106,7 @@ fn eval_logging(cli: &Trunk) -> tracing_subscriber::EnvFilter {
99106
(1, false) => "error,trunk=debug",
100107
(_, false) => "error,trunk=trace",
101108
};
109+
102110
tracing_subscriber::EnvFilter::new(directives)
103111
}
104112

@@ -125,28 +133,27 @@ struct Trunk {
125133
#[arg(long, global(true), env = "TRUNK_SKIP_VERSION_CHECK")]
126134
pub skip_version_check: bool,
127135

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,
131143
}
132144

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]
136150
Auto,
151+
/// Always enable color
152+
Always,
153+
/// Never enable color
137154
Never,
138155
}
139156

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-
150157
impl Trunk {
151158
#[tracing::instrument(level = "trace", skip(self))]
152159
pub async fn run(self) -> Result<()> {

0 commit comments

Comments
 (0)