formatter: add .labeled().with_heading() helper

I'm going to add a few callers of .with_heading() outside of ui.rs.
This commit is contained in:
Yuya Nishihara 2024-06-17 15:27:03 +09:00
parent 57022d6f04
commit b9e29b009d
2 changed files with 23 additions and 8 deletions

View file

@ -65,6 +65,11 @@ impl<T, S> LabeledWriter<T, S> {
pub fn new(formatter: T, label: S) -> Self { pub fn new(formatter: T, label: S) -> Self {
LabeledWriter { formatter, label } LabeledWriter { formatter, label }
} }
/// Turns into writer that prints labeled message with the `heading`.
pub fn with_heading<H>(self, heading: H) -> HeadingLabeledWriter<T, S, H> {
HeadingLabeledWriter::new(self, heading)
}
} }
impl<'a, T, S> LabeledWriter<T, S> impl<'a, T, S> LabeledWriter<T, S>
@ -96,9 +101,9 @@ pub struct HeadingLabeledWriter<T, S, H> {
} }
impl<T, S, H> HeadingLabeledWriter<T, S, H> { impl<T, S, H> HeadingLabeledWriter<T, S, H> {
pub fn new(formatter: T, label: S, heading: H) -> Self { pub fn new(writer: LabeledWriter<T, S>, heading: H) -> Self {
HeadingLabeledWriter { HeadingLabeledWriter {
writer: LabeledWriter::new(formatter, label), writer,
heading: Some(heading), heading: Some(heading),
} }
} }
@ -1162,8 +1167,14 @@ mod tests {
let mut output: Vec<u8> = vec![]; let mut output: Vec<u8> = vec![];
let mut formatter: Box<dyn Formatter> = let mut formatter: Box<dyn Formatter> =
Box::new(ColorFormatter::for_config(&mut output, &config, false).unwrap()); Box::new(ColorFormatter::for_config(&mut output, &config, false).unwrap());
HeadingLabeledWriter::new(formatter.as_mut(), "inner", "Should be noop: "); formatter
let mut writer = HeadingLabeledWriter::new(formatter.as_mut(), "inner", "Heading: "); .as_mut()
.labeled("inner")
.with_heading("Should be noop: ");
let mut writer = formatter
.as_mut()
.labeled("inner")
.with_heading("Heading: ");
write!(writer, "Message").unwrap(); write!(writer, "Message").unwrap();
writeln!(writer, " continues").unwrap(); writeln!(writer, " continues").unwrap();
drop(formatter); drop(formatter);
@ -1176,7 +1187,10 @@ mod tests {
fn test_heading_labeled_writer_empty_string() { fn test_heading_labeled_writer_empty_string() {
let mut output: Vec<u8> = vec![]; let mut output: Vec<u8> = vec![];
let mut formatter: Box<dyn Formatter> = Box::new(PlainTextFormatter::new(&mut output)); let mut formatter: Box<dyn Formatter> = 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 // 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. // know if that's guaranteed, but let's record the current behavior.
write!(writer, "").unwrap(); write!(writer, "").unwrap();

View file

@ -424,7 +424,8 @@ impl Ui {
&self, &self,
heading: H, heading: H,
) -> Option<HeadingLabeledWriter<Box<dyn Formatter + '_>, &'static str, H>> { ) -> Option<HeadingLabeledWriter<Box<dyn Formatter + '_>, &'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. /// Writer to print warning with the default "Warning: " heading.
@ -444,7 +445,7 @@ impl Ui {
&self, &self,
heading: H, heading: H,
) -> HeadingLabeledWriter<Box<dyn Formatter + '_>, &'static str, H> { ) -> HeadingLabeledWriter<Box<dyn Formatter + '_>, &'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. /// Writer to print error without the "Error: " heading.
@ -457,7 +458,7 @@ impl Ui {
&self, &self,
heading: H, heading: H,
) -> HeadingLabeledWriter<Box<dyn Formatter + '_>, &'static str, H> { ) -> HeadingLabeledWriter<Box<dyn Formatter + '_>, &'static str, H> {
HeadingLabeledWriter::new(self.stderr_formatter(), "error", heading) self.error_no_heading().with_heading(heading)
} }
/// Waits for the pager exits. /// Waits for the pager exits.