From 9cbc34cdb925f72fcbd979a48b1210f56ff8361d Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sat, 7 Mar 2026 22:29:01 +0000 Subject: [PATCH] Add NO_COLOR and --no-color support with TTY-aware rendering --- Cargo.lock | 130 +++++++++++++++++++++++++++++++++ crates/bough-cli/Cargo.toml | 1 + crates/bough-cli/src/config.rs | 48 ++++++++++++ crates/bough-cli/src/render.rs | 22 +++++- 4 files changed, 199 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9e32fcc..3953c908 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,6 +120,7 @@ dependencies = [ "facet-yaml", "figue", "miette", + "serial_test", "tempfile", "thiserror", "tracing", @@ -577,6 +578,41 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-executor" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -795,6 +831,15 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.29" @@ -895,6 +940,29 @@ dependencies = [ "supports-color 3.0.2", ] +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec 1.15.1", + "windows-link", +] + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -952,6 +1020,15 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba" +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.12.3" @@ -1031,6 +1108,27 @@ dependencies = [ "hashlink", ] +[[package]] +name = "scc" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46e6f046b7fef48e2660c57ed794263155d713de679057f2d0c169bfc6e756cc" +dependencies = [ + "sdd", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "sdd" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490dcfcbfef26be6800d11870ff2df8774fa6e86d047e3e8c8a76b25655e41ca" + [[package]] name = "semver" version = "1.0.27" @@ -1080,6 +1178,32 @@ dependencies = [ "zmij", ] +[[package]] +name = "serial_test" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "911bd979bf1070a3f3aa7b691a3b3e9968f339ceeec89e08c280a8a22207a32f" +dependencies = [ + "futures-executor", + "futures-util", + "log", + "once_cell", + "parking_lot", + "scc", + "serial_test_derive", +] + +[[package]] +name = "serial_test_derive" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a7d91949b85b0d2fb687445e448b40d322b6b3e4af6b44a29b21d9a5f33e6d9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha2" version = "0.10.9" @@ -1106,6 +1230,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + [[package]] name = "smallvec" version = "1.15.1" diff --git a/crates/bough-cli/Cargo.toml b/crates/bough-cli/Cargo.toml index 0b26f109..e5432209 100644 --- a/crates/bough-cli/Cargo.toml +++ b/crates/bough-cli/Cargo.toml @@ -18,4 +18,5 @@ tracing = "0.1" tracing-subscriber = "0.3" [dev-dependencies] +serial_test = "3" tempfile = "3.26" diff --git a/crates/bough-cli/src/config.rs b/crates/bough-cli/src/config.rs index 65bf3aa4..0183274f 100644 --- a/crates/bough-cli/src/config.rs +++ b/crates/bough-cli/src/config.rs @@ -1,4 +1,5 @@ use std::env; +use std::io::IsTerminal; use std::{collections::HashMap, path::PathBuf}; use facet::Facet; @@ -15,6 +16,9 @@ pub struct Cli { #[facet(args::named, args::short = 'f', default)] pub format: Format, + #[facet(args::named, default)] + no_color: bool, + #[facet(args::subcommand)] pub command: Command, @@ -22,6 +26,19 @@ pub struct Cli { pub config: Config, } +pub fn resolve_color(no_color_flag: bool, is_tty: bool) -> bool { + if no_color_flag || env::var("NO_COLOR").is_ok() { + return false; + } + is_tty +} + +impl Cli { + pub fn color(&self) -> bool { + resolve_color(self.no_color, std::io::stdout().is_terminal()) + } +} + #[derive(Facet, Debug, Clone, Copy, PartialEq, Eq, Default)] #[repr(u8)] pub enum Format { @@ -864,6 +881,37 @@ exclude = [] let root = bough_core::Config::get_base_root_path(&cli.config); assert_eq!(root, dir.path().join("src")); } + + #[test] + fn color_defaults_true_when_tty() { + assert!(resolve_color(false, true)); + } + + #[test] + fn color_defaults_false_when_not_tty() { + assert!(!resolve_color(false, false)); + } + + #[test] + fn no_color_flag_disables_color_even_with_tty() { + assert!(!resolve_color(true, true)); + } + + #[test] + #[serial_test::serial] + fn no_color_env_disables_color() { + unsafe { env::set_var("NO_COLOR", "1") }; + assert!(!resolve_color(false, true)); + unsafe { env::remove_var("NO_COLOR") }; + } + + #[test] + #[serial_test::serial] + fn no_color_env_disables_color_even_without_flag() { + unsafe { env::set_var("NO_COLOR", "1") }; + assert!(!resolve_color(false, true)); + unsafe { env::remove_var("NO_COLOR") }; + } } impl bough_core::Config for Config { diff --git a/crates/bough-cli/src/render.rs b/crates/bough-cli/src/render.rs index d7126355..6bcb84f9 100644 --- a/crates/bough-cli/src/render.rs +++ b/crates/bough-cli/src/render.rs @@ -6,6 +6,23 @@ use facet::Facet; use crate::config::{Cli, Config, Format}; +fn strip_ansi(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut chars = s.chars(); + while let Some(c) = chars.next() { + if c == '\x1b' { + for c in chars.by_ref() { + if c == 'm' { + break; + } + } + } else { + out.push(c); + } + } + out +} + const RESET: &str = "\x1b[0m"; const TITLE: &str = "\x1b[33m"; const PATH: &str = "\x1b[34m"; @@ -20,12 +37,13 @@ pub trait Render { fn json(&self) -> String; fn render(&self, cli: &Cli) -> String { - match cli.format { + let out = match cli.format { Format::Terse => self.terse(), Format::Verbose => self.verbose(), Format::Markdown => self.markdown(), Format::Json => self.json(), - } + }; + if cli.color() { out } else { strip_ansi(&out) } } } -- knotmirror 0.0.1