From e6cdcfd811d4701024aadfb3c0b8935e9d4cf4a7 Mon Sep 17 00:00:00 2001 From: konstin Date: Mon, 9 Dec 2024 10:38:20 +0100 Subject: [PATCH 1/2] Respect user settings for tracing coloring Previously, `-vvv --color never` would still emit ANSI sequences to stderr. Ref https://github.com/astral-sh/uv/issues/9668#issuecomment-2522120211 --- crates/uv-cli/src/lib.rs | 17 +++++++++++++++++ crates/uv/src/lib.rs | 1 + crates/uv/src/logging.rs | 17 ++++++++++------- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 959694f75f93..50b7ab535420 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -296,6 +296,23 @@ pub enum ColorChoice { Never, } +impl ColorChoice { + /// Combine self (higher priority) with an [`anstream::ColorChoice`] (lower priority). + /// + /// This method allows prioritizing the user choice, while using the inferred choice for a + /// stream as default. + pub fn and_colorchoice(self, next: anstream::ColorChoice) -> Self { + match self { + Self::Auto => match next { + anstream::ColorChoice::Auto => Self::Auto, + anstream::ColorChoice::Always | anstream::ColorChoice::AlwaysAnsi => Self::Always, + anstream::ColorChoice::Never => Self::Never, + }, + Self::Always | Self::Never => self, + } + } +} + impl From for anstream::ColorChoice { fn from(value: ColorChoice) -> Self { match value { diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index a27ce472234c..9e8db59e057f 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -219,6 +219,7 @@ async fn run(mut cli: Cli) -> Result { 2.. => logging::Level::ExtraVerbose, }, duration_layer, + globals.color, )?; // Configure the `Printer`, which controls user-facing output in the CLI. diff --git a/crates/uv/src/logging.rs b/crates/uv/src/logging.rs index f446bb9d15b3..d030af780c14 100644 --- a/crates/uv/src/logging.rs +++ b/crates/uv/src/logging.rs @@ -1,7 +1,6 @@ use std::fmt; use std::str::FromStr; -use anstream::ColorChoice; use anyhow::Context; use jiff::Timestamp; use owo_colors::OwoColorize; @@ -19,6 +18,8 @@ use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::{EnvFilter, Layer, Registry}; use tracing_tree::time::Uptime; use tracing_tree::HierarchicalLayer; + +use uv_cli::ColorChoice; #[cfg(feature = "tracing-durations-export")] use uv_static::EnvVars; @@ -117,6 +118,7 @@ where pub(crate) fn setup_logging( level: Level, durations: impl Layer + Send + Sync, + color: ColorChoice, ) -> anyhow::Result<()> { let default_directive = match level { Level::Default => { @@ -140,6 +142,11 @@ pub(crate) fn setup_logging( .from_env() .context("Invalid RUST_LOG directives")?; + let ansi = match color.and_colorchoice(anstream::Stderr::choice(&std::io::stderr())) { + ColorChoice::Always => true, + ColorChoice::Never => false, + ColorChoice::Auto => unreachable!("anstream can't return auto as choice"), + }; match level { Level::Default | Level::Verbose => { // Regardless of the tracing level, show messages without any adornment. @@ -148,12 +155,7 @@ pub(crate) fn setup_logging( display_level: true, show_spans: false, }; - let ansi = match anstream::Stderr::choice(&std::io::stderr()) { - ColorChoice::Always | ColorChoice::AlwaysAnsi => true, - ColorChoice::Never => false, - // We just asked anstream for a choice, that can't be auto - ColorChoice::Auto => unreachable!(), - }; + tracing_subscriber::registry() .with(durations_layer) .with( @@ -174,6 +176,7 @@ pub(crate) fn setup_logging( .with_targets(true) .with_timer(Uptime::default()) .with_writer(std::io::stderr) + .with_ansi(ansi) .with_filter(filter), ) .init(); From 8b694ed2f28afe3fe7bb35c156b7fe991d71c62c Mon Sep 17 00:00:00 2001 From: konstin Date: Mon, 9 Dec 2024 11:13:48 +0100 Subject: [PATCH 2/2] Cippy --- crates/uv-cli/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 50b7ab535420..badb1e522993 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -301,6 +301,7 @@ impl ColorChoice { /// /// This method allows prioritizing the user choice, while using the inferred choice for a /// stream as default. + #[must_use] pub fn and_colorchoice(self, next: anstream::ColorChoice) -> Self { match self { Self::Auto => match next {