Skip to content

Commit

Permalink
Add print_with_writer to PrettyPrint
Browse files Browse the repository at this point in the history
  • Loading branch information
kojix2 committed Aug 14, 2024
1 parent fc95468 commit afeba06
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,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<bool> {
self.print_with_writer(None::<&mut dyn std::fmt::Write>)
}

/// Pretty-print all specified inputs to a specified writer.
pub fn print_with_writer<W: std::fmt::Write>(&mut self, writer: Option<W>) -> Result<bool> {
let highlight_lines = std::mem::take(&mut self.highlighted_lines);
self.config.highlighted_lines = HighlightedLineRanges(LineRanges::from(highlight_lines));
self.config.term_width = self
Expand Down Expand Up @@ -300,7 +305,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)
}
}
}

Expand Down

0 comments on commit afeba06

Please sign in to comment.