Skip to content

Commit cad677c

Browse files
committed
feat : coloration for helper command with yansi
1 parent 50fb7ba commit cad677c

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ rand = { version = "0.9.2", features = ["std"] }
2424
serde = { version = "1.0.219", features = ["derive"] }
2525
serde_json = "1.0.143"
2626
humantime = "2"
27+
yansi = "1"
2728

2829
[dev-dependencies]
2930
assert_cmd = "2"

src/main.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clap::{ArgAction, CommandFactory, Parser};
1+
use clap::{ArgAction, ColorChoice, CommandFactory, FromArgMatches, Parser};
2+
use std::io::IsTerminal as _;
23
use std::path::PathBuf;
34

45
mod fs_safemove;
@@ -62,8 +63,28 @@ struct Cli {
6263
}
6364

6465
fn main() -> anyhow::Result<()> {
65-
let cli = Cli::parse();
66+
// Politique couleur:
67+
// - Si NO_COLOR est défini → jamais de couleur
68+
// - Sinon, Auto (TTY uniquement)
69+
let no_color_env = std::env::var_os("NO_COLOR").is_some();
70+
let is_tty = std::io::stdout().is_terminal();
6671

72+
// Config Clap (help colorisée)
73+
let mut cmd = Cli::command();
74+
cmd = cmd.color(if no_color_env {
75+
ColorChoice::Never
76+
} else {
77+
ColorChoice::Auto
78+
});
79+
let mut matches = cmd.get_matches();
80+
let cli = Cli::from_arg_matches_mut(&mut matches)?;
81+
82+
// Config `yansi` (pour nos propres sorties)
83+
if no_color_env || !is_tty {
84+
yansi::disable();
85+
} else {
86+
yansi::enable();
87+
}
6788
// Internal completion endpoint
6889
if !cli.__complete.is_empty() {
6990
let context = cli.__complete[0].as_str();

0 commit comments

Comments
 (0)