Skip to content

Commit

Permalink
Auto merge of #10024 - dswij:9960, r=ehuss
Browse files Browse the repository at this point in the history
`future-incompat-report` checks both stdout and stderr for color support

Closes #9960
  • Loading branch information
bors committed Nov 15, 2021
2 parents 3a3a071 + 622b43a commit d2cc298
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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

0 comments on commit d2cc298

Please sign in to comment.