diff options
| author | Kamal Marhubi <kamal@marhubi.com> | 2016-05-31 15:15:33 +0200 |
|---|---|---|
| committer | Kamal Marhubi <kamal@marhubi.com> | 2016-05-31 15:15:33 +0200 |
| commit | 9759068e6280c8958d8ef769337d015794fb605f (patch) | |
| tree | 85f9fe88e50199cb784021290b9408092f71aac9 /src/rustfmt_diff.rs | |
| parent | 240dba54677c09a455d15f76f8d4f328348ebd99 (diff) | |
print_diff: Don't print color codes if output is not a tty
On unix, `term::stdout()` just reads the `TERM` environment variable to decide what features are available. It does not check if the output file descriptor is in fact a tty. This resulted in printing escape codes when redirecting output.
Diffstat (limited to 'src/rustfmt_diff.rs')
| -rw-r--r-- | src/rustfmt_diff.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index fe521029cb5..d5129bd1bca 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -88,10 +88,28 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F) where F: Fn(u32) -> String { - if let Some(t) = term::stdout() { - print_diff_fancy(diff, get_section_title, t); - } else { - print_diff_basic(diff, get_section_title); + match term::stdout() { + Some(_) if isatty() => print_diff_fancy(diff, get_section_title, term::stdout().unwrap()), + _ => print_diff_basic(diff, get_section_title), + } + + // isatty shamelessly adapted from cargo. + #[cfg(unix)] + fn isatty() -> bool { + extern crate libc; + + unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 } + } + #[cfg(windows)] + fn isatty() -> bool { + extern crate kernel32; + extern crate winapi; + + unsafe { + let handle = kernel32::GetStdHandle(winapi::winbase::STD_OUTPUT_HANDLE); + let mut out = 0; + kernel32::GetConsoleMode(handle, &mut out) != 0 + } } } |
