diff --git a/src/cli_util.rs b/src/cli_util.rs index fb6a6fc42..4baffc533 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -1275,8 +1275,7 @@ pub fn parse_args( let matches = app.clone().get_matches_from(&string_args); let args: Args = Args::from_arg_matches(&matches).unwrap(); if let Some(choice) = args.global_args.color { - // Here we assume ui was created for_terminal(). - ui.reset_color_for_terminal(choice); + ui.reset_color(choice); } let command_helper = CommandHelper::new(app, string_args, args.global_args); Ok((command_helper, matches)) diff --git a/src/ui.rs b/src/ui.rs index bfd5d25b8..f2ff6e703 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -66,11 +66,11 @@ fn color_setting(settings: &UserSettings) -> ColorChoice { .unwrap_or_default() } -fn use_color(choice: ColorChoice) -> bool { +fn use_color(choice: ColorChoice, maybe_tty: bool) -> bool { match choice { ColorChoice::Always => true, ColorChoice::Never => false, - ColorChoice::Auto => atty::is(Stream::Stdout), + ColorChoice::Auto => maybe_tty && atty::is(Stream::Stdout), } } @@ -96,7 +96,7 @@ impl<'stdout> Ui<'stdout> { pub fn for_terminal(settings: UserSettings) -> Ui<'static> { let cwd = std::env::current_dir().unwrap(); - let color = use_color(color_setting(&settings)); + let color = use_color(color_setting(&settings), true); let formatter_factory = FormatterFactory::prepare(&settings, color); Ui { cwd, @@ -110,8 +110,9 @@ impl<'stdout> Ui<'stdout> { } /// Reconfigures the underlying outputs with the new color choice. - pub fn reset_color_for_terminal(&mut self, choice: ColorChoice) { - let color = use_color(choice); + pub fn reset_color(&mut self, choice: ColorChoice) { + let maybe_tty = matches!(&self.output_pair, UiOutputPair::Terminal { .. }); + let color = use_color(choice, maybe_tty); if self.formatter_factory.is_color() != color { self.formatter_factory = FormatterFactory::prepare(&self.settings, color); }