Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

future-incompat-report checks both stdout and stderr for color support #10024

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cargo/core/compiler/future_incompat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ impl OnDiskReports {
};
to_display += &package_report;

let to_display = if config.shell().err_supports_color() {
let shell = config.shell();

let to_display = if shell.err_supports_color() && shell.out_supports_color() {
to_display
} else {
strip_ansi_escapes::strip(&to_display)
Expand Down
26 changes: 18 additions & 8 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ impl Shell {
/// Creates a new shell (color choice and verbosity), defaulting to 'auto' color and verbose
/// output.
pub fn new() -> Shell {
let auto = ColorChoice::CargoAuto.to_termcolor_color_choice();
let auto_clr = ColorChoice::CargoAuto;
Shell {
output: ShellOut::Stream {
stdout: StandardStream::stdout(auto),
stderr: StandardStream::stderr(auto),
stdout: StandardStream::stdout(
auto_clr.to_termcolor_color_choice(atty::Stream::Stdout),
),
stderr: StandardStream::stderr(
auto_clr.to_termcolor_color_choice(atty::Stream::Stderr),
),
color_choice: ColorChoice::CargoAuto,
stderr_tty: atty::is(atty::Stream::Stderr),
},
Expand Down Expand Up @@ -297,9 +301,8 @@ impl Shell {
),
};
*color_choice = cfg;
let choice = cfg.to_termcolor_color_choice();
*stdout = StandardStream::stdout(choice);
*stderr = StandardStream::stderr(choice);
*stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(atty::Stream::Stdout));
*stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(atty::Stream::Stderr));
}
Ok(())
}
Expand All @@ -323,6 +326,13 @@ impl Shell {
}
}

pub fn out_supports_color(&self) -> bool {
match &self.output {
ShellOut::Write(_) => false,
ShellOut::Stream { stdout, .. } => stdout.supports_color(),
}
}

/// Prints a message to stderr and translates ANSI escape code into console colors.
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
if self.needs_clear {
Expand Down Expand Up @@ -432,12 +442,12 @@ impl ShellOut {

impl ColorChoice {
/// Converts our color choice to termcolor's version.
fn to_termcolor_color_choice(self) -> termcolor::ColorChoice {
fn to_termcolor_color_choice(self, stream: atty::Stream) -> termcolor::ColorChoice {
match self {
ColorChoice::Always => termcolor::ColorChoice::Always,
ColorChoice::Never => termcolor::ColorChoice::Never,
ColorChoice::CargoAuto => {
if atty::is(atty::Stream::Stderr) {
if atty::is(stream) {
termcolor::ColorChoice::Auto
} else {
termcolor::ColorChoice::Never
Expand Down