From 1b3200b2af9cebc86a78f30d585b0f65be0c3091 Mon Sep 17 00:00:00 2001 From: samypr100 <3933065+samypr100@users.noreply.github.com> Date: Tue, 4 Jun 2024 17:00:42 -0400 Subject: [PATCH] feat: support `NO_COLOR` and `FORCE_COLOR` env vars (#3979) ## Summary Closes #3955 Adds explicit support to `NO_COLOR` and `FORCE_COLOR` via GlobalSettings. The order, per specs is now `NO_COLOR` > `FORCE_COLOR` > `color`. This PR is a backup plan pending rust-cli/anstyle#192. ## Test Plan Tested all cases locally for now; I didn't see existing tests for GlobalSettings parsing. --- README.md | 2 ++ crates/uv/src/settings.rs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c4e3b3f4..3f9da0653 100644 --- a/README.md +++ b/README.md @@ -592,6 +592,8 @@ In addition, uv respects the following environment variables: - `MACOSX_DEPLOYMENT_TARGET`: Used with `--python-platform macos` and related variants to set the deployment target (i.e., the minimum supported macOS version). Defaults to `12.0`, the least-recent non-EOL macOS version at time of writing. +- `NO_COLOR`: Disable colors. Takes precedence over `FORCE_COLOR`. See [no-color.org](https://no-color.org). +- `FORCE_COLOR`: Enforce colors regardless of TTY support. See [force-color.org](https://force-color.org). ## Versioning diff --git a/crates/uv/src/settings.rs b/crates/uv/src/settings.rs index 23aaf7f10..45363735a 100644 --- a/crates/uv/src/settings.rs +++ b/crates/uv/src/settings.rs @@ -46,8 +46,20 @@ impl GlobalSettings { Self { quiet: args.quiet, verbose: args.verbose, - color: if args.no_color { + color: if args.no_color + || std::env::var_os("NO_COLOR") + .filter(|v| !v.is_empty()) + .is_some() + { ColorChoice::Never + } else if std::env::var_os("FORCE_COLOR") + .filter(|v| !v.is_empty()) + .is_some() + || std::env::var_os("CLICOLOR_FORCE") + .filter(|v| !v.is_empty()) + .is_some() + { + ColorChoice::Always } else { args.color },