Skip to content

Commit c8eb288

Browse files
seblyngctron
authored andcommitted
feat: add --color arg to clap struct
1 parent 792c372 commit c8eb288

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/main.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ mod watch;
1717
mod ws;
1818

1919
use anyhow::{Context, Result};
20-
use clap::{ArgAction, Parser, Subcommand};
20+
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
2121
use common::STARTING;
22+
use std::fmt::Display;
2223
use std::io::IsTerminal;
2324
use std::path::PathBuf;
2425
use std::process::ExitCode;
@@ -28,7 +29,13 @@ use tracing_subscriber::prelude::*;
2829
async fn main() -> Result<ExitCode> {
2930
let cli = Trunk::parse();
3031

31-
let colored = std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none();
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+
};
3239

3340
#[cfg(windows)]
3441
if colored {
@@ -117,6 +124,27 @@ struct Trunk {
117124
/// Skip the version check
118125
#[arg(long, global(true), env = "TRUNK_SKIP_VERSION_CHECK")]
119126
pub skip_version_check: bool,
127+
128+
/// Arg to specify ansi colored output for tracing logs
129+
#[arg(long, default_value_t = TrunkColorArgs::Auto)]
130+
pub color: TrunkColorArgs,
131+
}
132+
133+
#[derive(ValueEnum, Clone)]
134+
enum TrunkColorArgs {
135+
Always,
136+
Auto,
137+
Never,
138+
}
139+
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+
}
120148
}
121149

122150
impl Trunk {

0 commit comments

Comments
 (0)