From b93a41a9a3c1af60bdf7f2dcd56d0d5d14aab429 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Sun, 11 Aug 2024 00:26:33 +0900 Subject: [PATCH] Add print_with_writer to PrettyPrint --- src/pretty_printer.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index eb123ea3..aa06a78b 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -279,6 +279,11 @@ impl<'a> PrettyPrinter<'a> { /// If you want to call 'print' multiple times, you have to call the appropriate /// input_* methods again. pub fn print(&mut self) -> Result { + self.print_with_writer(None::<&mut dyn std::fmt::Write>) + } + + /// Pretty-print all specified inputs to a specified writer. + pub fn print_with_writer(&mut self, writer: Option) -> Result { let highlight_lines = std::mem::take(&mut self.highlighted_lines); self.config.highlighted_lines = HighlightedLineRanges(LineRanges::from(highlight_lines)); self.config.term_width = self @@ -315,7 +320,13 @@ impl<'a> PrettyPrinter<'a> { // Run the controller let controller = Controller::new(&self.config, &self.assets); - controller.run(inputs.into_iter().map(|i| i.into()).collect(), None) + + // If writer is provided, pass it to the controller, otherwise pass None + if let Some(mut w) = writer { + controller.run(inputs.into_iter().map(|i| i.into()).collect(), Some(&mut w)) + } else { + controller.run(inputs.into_iter().map(|i| i.into()).collect(), None) + } } }