Skip to content

Commit 838dcd8

Browse files
committed
fix(check): Subject check to RUSTUP_TERM_COLOR
`rustup check` was using `console::style` which will do its own "can I style" checks, overriding `RUSTUP_TERM_COLOR`. See https://docs.rs/console/0.16.1/src/console/utils.rs.html#696-702
1 parent 9d0818f commit 838dcd8

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

src/cli/rustup_mode.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use std::{
1111
};
1212

1313
use anstream::ColorChoice;
14+
use anstyle::{AnsiColor, Style};
1415
use anyhow::{Context, Error, Result, anyhow};
1516
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum, builder::PossibleValue};
1617
use clap_cargo::style::{CONTEXT, HEADER};
1718
use clap_complete::Shell;
18-
use console::style;
1919
use futures_util::stream::StreamExt;
2020
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
2121
use itertools::Itertools;
@@ -819,6 +819,30 @@ async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<ExitCode> {
819819
Some(_) | None => channels_len,
820820
};
821821

822+
let bold = if use_colors {
823+
Style::new().bold()
824+
} else {
825+
Style::new()
826+
};
827+
828+
let error = if use_colors {
829+
AnsiColor::Red.on_default().bold()
830+
} else {
831+
Style::new()
832+
};
833+
834+
let good = if use_colors {
835+
AnsiColor::Green.on_default().bold()
836+
} else {
837+
Style::new()
838+
};
839+
840+
let warn = if use_colors {
841+
AnsiColor::Yellow.on_default().bold()
842+
} else {
843+
Style::new()
844+
};
845+
822846
// Ensure that `.buffered()` is never called with 0 as this will cause a hang.
823847
// See: https://github.com/rust-lang/futures-rs/pull/1194#discussion_r209501774
824848
if channels_len > 0 {
@@ -842,40 +866,25 @@ async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<ExitCode> {
842866
let dist_version = distributable.show_dist_version().await?;
843867
let mut update_a = false;
844868

845-
let mut styled_name = style(format!("{name} - "));
846-
if use_colors {
847-
styled_name = styled_name.bold();
848-
}
869+
let styled_name = format!("{bold}{name} - {bold:#}");
849870
let message = match (current_version, dist_version) {
850871
(None, None) => {
851-
let mut m = style("Cannot identify installed or update versions");
852-
if use_colors {
853-
m = m.red().bold();
854-
}
855-
format!("{styled_name}{m}")
872+
let m = "Cannot identify installed or update versions";
873+
format!("{styled_name}{error}{m}{error:#}")
856874
}
857875
(Some(cv), None) => {
858-
let mut m = style("Up to date");
859-
if use_colors {
860-
m = m.green().bold();
861-
}
862-
format!("{styled_name}{m} : {cv}")
876+
let m = "Up to date";
877+
format!("{styled_name}{good}{m}{good:#} : {cv}")
863878
}
864879
(Some(cv), Some(dv)) => {
865-
let mut m = style("Update available");
866-
if use_colors {
867-
m = m.yellow().bold();
868-
}
880+
let m = "Update available";
869881
update_a = true;
870-
format!("{styled_name}{m} : {cv} -> {dv}")
882+
format!("{styled_name}{warn}{m}{warn:#} : {cv} -> {dv}")
871883
}
872884
(None, Some(dv)) => {
873-
let mut m = style("Update available");
874-
if use_colors {
875-
m = m.yellow().bold();
876-
}
885+
let m = "Update available";
877886
update_a = true;
878-
format!("{styled_name}{m} : (Unknown version) -> {dv}")
887+
format!("{styled_name}{warn}{m}{warn:#} : (Unknown version) -> {dv}")
879888
}
880889
};
881890
pb.set_style(ProgressStyle::with_template(message.as_str()).unwrap());

tests/suite/cli_rustup_ui/rustup_check_updates_none.stdout.term.svg

Lines changed: 5 additions & 3 deletions
Loading

tests/suite/cli_rustup_ui/rustup_check_updates_some.stdout.term.svg

Lines changed: 5 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)