Skip to content

Commit

Permalink
term2: Swallow unsupported errors out of term::Error
Browse files Browse the repository at this point in the history
In order that we don't explode horribly on `TERM=dumb` or similar,
we need to ensure that we cleanly swallow errors from the term
crate which signify a less-than-ideal terminal.

Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
  • Loading branch information
kinnison committed Apr 10, 2019
1 parent 41ac62c commit 2c8cbbb
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/cli/term2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,24 @@ impl<T: Instantiable + Isatty + io::Write> io::Write for Terminal<T> {
}
}

macro_rules! swallow_unsupported {
( $call:expr ) => {{
use term::Error::*;
match $call {
Ok(()) | Err(ColorOutOfRange) | Err(NotSupported) => Ok(()),
Err(e) => Err(e),
}
}};
}

impl<T: Instantiable + Isatty + io::Write> Terminal<T> {
pub fn fg(&mut self, color: color::Color) -> Result<(), term::Error> {
if !T::isatty() {
return Ok(());
}

if let Some(ref mut t) = self.0 {
t.fg(color)
swallow_unsupported!(t.fg(color))
} else {
Ok(())
}
Expand All @@ -268,8 +278,8 @@ impl<T: Instantiable + Isatty + io::Write> Terminal<T> {
if let Err(e) = t.attr(attr) {
// If `attr` is not supported, try to emulate it
match attr {
Attr::Bold => t.fg(color::BRIGHT_WHITE),
_ => Err(e),
Attr::Bold => swallow_unsupported!(t.fg(color::BRIGHT_WHITE)),
_ => swallow_unsupported!(Err(e)),
}
} else {
Ok(())
Expand All @@ -285,7 +295,7 @@ impl<T: Instantiable + Isatty + io::Write> Terminal<T> {
}

if let Some(ref mut t) = self.0 {
t.reset()
swallow_unsupported!(t.reset())
} else {
Ok(())
}
Expand Down

0 comments on commit 2c8cbbb

Please sign in to comment.