Skip to content

Commit 57bab5e

Browse files
Add check to get windows console type to decide to use colors or not
1 parent 54e8216 commit 57bab5e

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/librustdoc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ regex = "1"
2020

2121
[dev-dependencies]
2222
expect-test = "1.0"
23+
24+
[target.'cfg(windows)'.dependencies]
25+
termcolor = "1.0"

src/librustdoc/doctest.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,31 @@ fn run_test(
298298
ErrorOutputType::HumanReadable(kind) => {
299299
let (_, color_config) = kind.unzip();
300300
match color_config {
301-
ColorConfig::Never => {}
302-
_ => {
301+
ColorConfig::Never => {
302+
compiler.arg("--color").arg("never");
303+
}
304+
ColorConfig::Always => {
303305
compiler.arg("--color").arg("always");
304306
}
307+
ColorConfig::Auto => {
308+
#[cfg(windows)]
309+
{
310+
// This specific check is because old windows consoles require a connection
311+
// to be able to display colors (and they don't support ANSI), which we
312+
// cannot in here, so in case this is an old windows console, we can't
313+
// display colors.
314+
use crate::termcolor::{ColorChoice, StandardStream, WriteColor};
315+
if StandardStream::stdout(ColorChoice::Auto).is_synchronous() {
316+
compiler.arg("--color").arg("never");
317+
} else {
318+
compiler.arg("--color").arg("always");
319+
}
320+
}
321+
#[cfg(not(windows))]
322+
{
323+
compiler.arg("--color").arg("always");
324+
}
325+
}
305326
}
306327
}
307328
_ => {}

src/librustdoc/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ extern crate rustc_target;
5454
extern crate rustc_trait_selection;
5555
extern crate rustc_typeck;
5656
extern crate test as testing;
57+
#[macro_use]
58+
extern crate tracing;
59+
#[cfg(windows)]
60+
extern crate termcolor;
5761

5862
use std::default::Default;
5963
use std::env;

0 commit comments

Comments
 (0)