Skip to content

Commit d2cc298

Browse files
committed
Auto merge of #10024 - dswij:9960, r=ehuss
`future-incompat-report` checks both stdout and stderr for color support Closes #9960
2 parents 3a3a071 + 622b43a commit d2cc298

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/cargo/core/compiler/future_incompat.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ impl OnDiskReports {
218218
};
219219
to_display += &package_report;
220220

221-
let to_display = if config.shell().err_supports_color() {
221+
let shell = config.shell();
222+
223+
let to_display = if shell.err_supports_color() && shell.out_supports_color() {
222224
to_display
223225
} else {
224226
strip_ansi_escapes::strip(&to_display)

src/cargo/core/shell.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ impl Shell {
9696
/// Creates a new shell (color choice and verbosity), defaulting to 'auto' color and verbose
9797
/// output.
9898
pub fn new() -> Shell {
99-
let auto = ColorChoice::CargoAuto.to_termcolor_color_choice();
99+
let auto_clr = ColorChoice::CargoAuto;
100100
Shell {
101101
output: ShellOut::Stream {
102-
stdout: StandardStream::stdout(auto),
103-
stderr: StandardStream::stderr(auto),
102+
stdout: StandardStream::stdout(
103+
auto_clr.to_termcolor_color_choice(atty::Stream::Stdout),
104+
),
105+
stderr: StandardStream::stderr(
106+
auto_clr.to_termcolor_color_choice(atty::Stream::Stderr),
107+
),
104108
color_choice: ColorChoice::CargoAuto,
105109
stderr_tty: atty::is(atty::Stream::Stderr),
106110
},
@@ -297,9 +301,8 @@ impl Shell {
297301
),
298302
};
299303
*color_choice = cfg;
300-
let choice = cfg.to_termcolor_color_choice();
301-
*stdout = StandardStream::stdout(choice);
302-
*stderr = StandardStream::stderr(choice);
304+
*stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(atty::Stream::Stdout));
305+
*stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(atty::Stream::Stderr));
303306
}
304307
Ok(())
305308
}
@@ -323,6 +326,13 @@ impl Shell {
323326
}
324327
}
325328

329+
pub fn out_supports_color(&self) -> bool {
330+
match &self.output {
331+
ShellOut::Write(_) => false,
332+
ShellOut::Stream { stdout, .. } => stdout.supports_color(),
333+
}
334+
}
335+
326336
/// Prints a message to stderr and translates ANSI escape code into console colors.
327337
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
328338
if self.needs_clear {
@@ -432,12 +442,12 @@ impl ShellOut {
432442

433443
impl ColorChoice {
434444
/// Converts our color choice to termcolor's version.
435-
fn to_termcolor_color_choice(self) -> termcolor::ColorChoice {
445+
fn to_termcolor_color_choice(self, stream: atty::Stream) -> termcolor::ColorChoice {
436446
match self {
437447
ColorChoice::Always => termcolor::ColorChoice::Always,
438448
ColorChoice::Never => termcolor::ColorChoice::Never,
439449
ColorChoice::CargoAuto => {
440-
if atty::is(atty::Stream::Stderr) {
450+
if atty::is(stream) {
441451
termcolor::ColorChoice::Auto
442452
} else {
443453
termcolor::ColorChoice::Never

0 commit comments

Comments
 (0)