From 5a2d7fa6b540334848c8dfcb018616dc8845e919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20G=C3=BCnzler?= Date: Sat, 7 May 2022 22:54:49 +0200 Subject: [PATCH] always reset colors settings on the output stream after writing (#375) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is according to: https://docs.rs/termcolor/1.1.2/termcolor/#example-using-standardstream Not resetting the color settings on the stream will leak the style to the shell otherwise. This can be observed when listing mailboxes prior to this patch. Signed-off-by: Robert Günzler Co-authored-by: Clément DOUIN --- cli/src/output/print.rs | 6 ++++-- cli/src/ui/table.rs | 10 ++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cli/src/output/print.rs b/cli/src/output/print.rs index a843501..8faf0a4 100644 --- a/cli/src/output/print.rs +++ b/cli/src/output/print.rs @@ -8,12 +8,14 @@ pub trait Print { impl Print for &str { fn print(&self, writer: &mut dyn WriteColor) -> Result<()> { - writeln!(writer, "{}", self).context("cannot write string to writer") + writeln!(writer, "{}", self).context("cannot write string to writer")?; + Ok(writer.reset()?) } } impl Print for String { fn print(&self, writer: &mut dyn WriteColor) -> Result<()> { - self.as_str().print(writer) + self.as_str().print(writer)?; + Ok(writer.reset()?) } } diff --git a/cli/src/ui/table.rs b/cli/src/ui/table.rs index 9d9559a..2a9f22a 100644 --- a/cli/src/ui/table.rs +++ b/cli/src/ui/table.rs @@ -134,14 +134,8 @@ impl Print for Cell { .context(format!(r#"cannot apply colors to cell "{}""#, self.value))?; // Writes the colorized cell to stdout - write!(writer, "{}", self.value) - .context(format!(r#"cannot print cell "{}""#, self.value))?; - - // Resets color after cell - writer - .reset() - .context(format!(r#"cannot reset color in cell "{}""#, self.value))?; - write!(writer, "").context(format!(r#"cannot print cell "{}""#, self.value)) + write!(writer, "{}", self.value).context(format!(r#"cannot print cell "{}""#, self.value))?; + Ok(writer.reset()?) } }