Skip to content

Commit

Permalink
Detect whether or not the terminal supports color.
Browse files Browse the repository at this point in the history
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 rust-lang#2338
  • Loading branch information
Stebalien committed Feb 1, 2016
1 parent fd463d6 commit cfcfa08
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 14 additions & 12 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cfcfa08

Please sign in to comment.