From cfcfa08a0a7afb3355889b6f1263c594f9d51bdf Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sun, 31 Jan 2016 20:03:14 -0500 Subject: [PATCH] Detect whether or not the terminal supports color. Before v0.4, term used to return `Ok(true)` if something succeeded, `Ok(false)` if the operation was unsupported, and `Err(io::Error)` if there was an IO error. Now, it returns `Ok(())` if the operation succeeds and `Err(term::Error)` if the operation fails. If the operation is unsupported, it returns `Err(term::Error::NotSupported)`. This means that, if `op` is unsupported, `try!(term.op())` will now return an error instead of silently failing (well, return false but that's effectively silent). Fixes #2338 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/cargo/core/shell.rs | 26 ++++++++++++++------------ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a2d750066d9..496c448697e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,7 +25,7 @@ dependencies = [ "semver 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "term 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", @@ -395,7 +395,7 @@ dependencies = [ [[package]] name = "term" -version = "0.4.0" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index ed3e7cb8e97..92067be100c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ regex = "0.1" rustc-serialize = "0.3" semver = "0.2.0" tar = "0.4" -term = "0.4" +term = "0.4.4" time = "0.1" toml = "0.1" url = "0.2" diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 14cc5f38794..f88223db1b3 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -149,21 +149,23 @@ impl Shell { // output to stderr if a tty is present and color output is not possible. match ::term::terminfo::TermInfo::from_env() { Ok(ti) => { + let term = TerminfoTerminal::new_with_terminfo(out, ti); // Color output is possible. - Shell { - terminal: Colored(Box::new(TerminfoTerminal::new_with_terminfo(out, ti))), - config: config + if !term.supports_color() { + Err(term.into_inner()) + } else { + Ok(Shell { + terminal: Colored(Box::new(term)), + config: config + }) } } - _ if config.tty => { - // Color output is expected but not available, fall back to stderr. - Shell { terminal: NoColor(Box::new(io::stderr())), config: config } - } - _ => { - // No color output. - Shell { terminal: NoColor(out), config: config } - } - } + Err(_) => Err(out) + }.unwrap_or_else(|out| Shell { + // If config.tty is set, we should be able to color output but can't. Write to stderr. + terminal: NoColor(if config.tty { Box::new(io::stderr()) } else { out }), + config: config, + }) } pub fn set_color_config(&mut self, color_config: ColorConfig) {