diff --git a/cli/src/formatter.rs b/cli/src/formatter.rs index 164e4a201..2d8b97a54 100644 --- a/cli/src/formatter.rs +++ b/cli/src/formatter.rs @@ -65,6 +65,11 @@ impl LabeledWriter { pub fn new(formatter: T, label: S) -> Self { LabeledWriter { formatter, label } } + + /// Turns into writer that prints labeled message with the `heading`. + pub fn with_heading(self, heading: H) -> HeadingLabeledWriter { + HeadingLabeledWriter::new(self, heading) + } } impl<'a, T, S> LabeledWriter @@ -96,9 +101,9 @@ pub struct HeadingLabeledWriter { } impl HeadingLabeledWriter { - pub fn new(formatter: T, label: S, heading: H) -> Self { + pub fn new(writer: LabeledWriter, heading: H) -> Self { HeadingLabeledWriter { - writer: LabeledWriter::new(formatter, label), + writer, heading: Some(heading), } } @@ -1162,8 +1167,14 @@ mod tests { let mut output: Vec = vec![]; let mut formatter: Box = Box::new(ColorFormatter::for_config(&mut output, &config, false).unwrap()); - HeadingLabeledWriter::new(formatter.as_mut(), "inner", "Should be noop: "); - let mut writer = HeadingLabeledWriter::new(formatter.as_mut(), "inner", "Heading: "); + formatter + .as_mut() + .labeled("inner") + .with_heading("Should be noop: "); + let mut writer = formatter + .as_mut() + .labeled("inner") + .with_heading("Heading: "); write!(writer, "Message").unwrap(); writeln!(writer, " continues").unwrap(); drop(formatter); @@ -1176,7 +1187,10 @@ mod tests { fn test_heading_labeled_writer_empty_string() { let mut output: Vec = vec![]; let mut formatter: Box = Box::new(PlainTextFormatter::new(&mut output)); - let mut writer = HeadingLabeledWriter::new(formatter.as_mut(), "inner", "Heading: "); + let mut writer = formatter + .as_mut() + .labeled("inner") + .with_heading("Heading: "); // write_fmt() is called even if the format string is empty. I don't // know if that's guaranteed, but let's record the current behavior. write!(writer, "").unwrap(); diff --git a/cli/src/ui.rs b/cli/src/ui.rs index 72269af41..8b89fadbe 100644 --- a/cli/src/ui.rs +++ b/cli/src/ui.rs @@ -424,7 +424,8 @@ impl Ui { &self, heading: H, ) -> Option, &'static str, H>> { - (!self.quiet).then(|| HeadingLabeledWriter::new(self.stderr_formatter(), "hint", heading)) + self.hint_no_heading() + .map(|writer| writer.with_heading(heading)) } /// Writer to print warning with the default "Warning: " heading. @@ -444,7 +445,7 @@ impl Ui { &self, heading: H, ) -> HeadingLabeledWriter, &'static str, H> { - HeadingLabeledWriter::new(self.stderr_formatter(), "warning", heading) + self.warning_no_heading().with_heading(heading) } /// Writer to print error without the "Error: " heading. @@ -457,7 +458,7 @@ impl Ui { &self, heading: H, ) -> HeadingLabeledWriter, &'static str, H> { - HeadingLabeledWriter::new(self.stderr_formatter(), "error", heading) + self.error_no_heading().with_heading(heading) } /// Waits for the pager exits.