always reset colors settings on the output stream after writing (#375)

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 <r@gnzler.io>

Co-authored-by: Clément DOUIN <soywod@users.noreply.github.com>
This commit is contained in:
Robert Günzler 2022-05-07 22:54:49 +02:00 committed by GitHub
parent 4d91a5d74e
commit 5a2d7fa6b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 10 deletions

View file

@ -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()?)
}
}

View file

@ -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()?)
}
}