From 04cfd2a98e6a384ef10ca336f31672b017be308a Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Wed, 10 Apr 2019 13:07:17 +0100 Subject: [PATCH] term2: Swallow unsupported errors out of term::Error 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 --- src/cli/term2.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cli/term2.rs b/src/cli/term2.rs index 3e638445633..8ea9d1dddc4 100644 --- a/src/cli/term2.rs +++ b/src/cli/term2.rs @@ -246,6 +246,18 @@ impl io::Write for Terminal { } } +macro_rules! swallow_unsupported { + ( $call:expr ) => { + match $call { + Ok(()) => Ok(()), + Err(e) => match e { + term::Error::ColorOutOfRange | term::Error::NotSupported => Ok(()), + _ => Err(e), + }, + } + }; +} + impl Terminal { pub fn fg(&mut self, color: color::Color) -> Result<(), term::Error> { if !T::isatty() { @@ -253,7 +265,7 @@ impl Terminal { } if let Some(ref mut t) = self.0 { - t.fg(color) + swallow_unsupported!(t.fg(color)) } else { Ok(()) } @@ -268,8 +280,8 @@ impl Terminal { 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(()) @@ -285,7 +297,7 @@ impl Terminal { } if let Some(ref mut t) = self.0 { - t.reset() + swallow_unsupported!(t.reset()) } else { Ok(()) }