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 04cfd2a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/cli/term2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,26 @@ impl<T: Instantiable + Isatty + io::Write> io::Write for Terminal<T> {
}
}

macro_rules! swallow_unsupported {
( $call:expr ) => {
match $call {
Ok(()) => Ok(()),
Err(e) => match e {
term::Error::ColorOutOfRange | term::Error::NotSupported => Ok(()),
_ => 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 +280,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 +297,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 04cfd2a

Please sign in to comment.