Add check to get windows console type to decide to use colors or not

This commit is contained in:
Guillaume Gomez 2020-07-16 16:36:25 +02:00
parent 54e8216348
commit 57bab5e020
3 changed files with 30 additions and 2 deletions

View file

@ -20,3 +20,6 @@ regex = "1"
[dev-dependencies]
expect-test = "1.0"
[target.'cfg(windows)'.dependencies]
termcolor = "1.0"

View file

@ -298,10 +298,31 @@ fn run_test(
ErrorOutputType::HumanReadable(kind) => {
let (_, color_config) = kind.unzip();
match color_config {
ColorConfig::Never => {}
_ => {
ColorConfig::Never => {
compiler.arg("--color").arg("never");
}
ColorConfig::Always => {
compiler.arg("--color").arg("always");
}
ColorConfig::Auto => {
#[cfg(windows)]
{
// This specific check is because old windows consoles require a connection
// to be able to display colors (and they don't support ANSI), which we
// cannot in here, so in case this is an old windows console, we can't
// display colors.
use crate::termcolor::{ColorChoice, StandardStream, WriteColor};
if StandardStream::stdout(ColorChoice::Auto).is_synchronous() {
compiler.arg("--color").arg("never");
} else {
compiler.arg("--color").arg("always");
}
}
#[cfg(not(windows))]
{
compiler.arg("--color").arg("always");
}
}
}
}
_ => {}

View file

@ -54,6 +54,10 @@ extern crate rustc_target;
extern crate rustc_trait_selection;
extern crate rustc_typeck;
extern crate test as testing;
#[macro_use]
extern crate tracing;
#[cfg(windows)]
extern crate termcolor;
use std::default::Default;
use std::env;