diff options
author | Keith Hall <keith-hall@users.noreply.github.com> | 2024-11-16 21:52:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-16 21:52:51 +0200 |
commit | e608b331425ca2ce8f8d0bd37e7f90901f91eb99 (patch) | |
tree | 0b9f2c0d03b64509f350d0d46525639ddb12ebe9 | |
parent | 822eff60282245e11f0a89845a04226c5a848965 (diff) | |
parent | 6598442d4146ad6dcbec10f34c79c63e337ba1ba (diff) |
Add print_with_writer to PrettyPrint
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/pretty_printer.rs | 13 |
2 files changed, 13 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fdbbf867..8c0f591b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ - Add `theme::theme` for choosing an appropriate theme based on the terminal's color scheme, see #2896 (@bash) - [BREAKING] Remove `HighlightingAssets::default_theme`. Use `theme::default_theme` instead. +- Add `PrettyPrinter::print_with_writer` for custom output destinations, see #3070 (@kojix2) # v0.24.0 diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index 51c9af80..a70ac021 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -281,6 +281,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 @@ -317,7 +322,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) + } } } |