Detect whether or not the terminal supports color.
authorSteven Allen <steven@stebalien.com>
Mon, 1 Feb 2016 01:03:14 +0000 (20:03 -0500)
committerSteven Allen <steven@stebalien.com>
Mon, 1 Feb 2016 21:06:01 +0000 (16:06 -0500)
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
Cargo.toml
src/cargo/core/shell.rs

index a2d750066d9dd87b60df38c78b07c74b27ed941c..496c448697e5b78a567b3f30d001217b73d7dd80 100644 (file)
@@ -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)",
index ed3e7cb8e973e9de29b0a1f452bb3c6b3a8f74fb..92067be100c375f9bb0fa7df59ba4cf2a35e9562 100644 (file)
@@ -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"
index 14cc5f38794d44b6e83dfdc078224ebc8e835723..f88223db1b3b67ad2376d4d6d8a307f197e04645 100644 (file)
@@ -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) {