From abd69f2debc4e292e555708222a6d2f161b6c32a Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 23 Oct 2021 21:41:26 +0900 Subject: [PATCH 1/3] Add `bat::PrettyPrinter::clear_highlights` (fixes #1919) --- src/pretty_printer.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index 28644513..674f2929 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -229,6 +229,14 @@ impl<'a> PrettyPrinter<'a> { self } + /// Clear highlighted lines added by [`PrettyPrinter::highlight`] and + /// [`PrettyPrinter::highlight_range`]. This is useful when reusing a + /// `PrettyPrinter` instance for multiple different ranges. + pub fn clear_highlights(&mut self) -> &mut Self { + self.highlighted_lines.clear(); + self + } + /// Specify the highlighting theme pub fn theme(&mut self, theme: impl AsRef) -> &mut Self { self.config.theme = theme.as_ref().to_owned(); From 3d7817d662446ed03a95dfb404770a4ff8df135c Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 5 Sep 2022 21:17:08 +0900 Subject: [PATCH 2/3] Clear `highlighted_lines` when calling `PrettyPrinter::print` --- src/pretty_printer.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index 674f2929..0d68ba40 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -229,14 +229,6 @@ impl<'a> PrettyPrinter<'a> { self } - /// Clear highlighted lines added by [`PrettyPrinter::highlight`] and - /// [`PrettyPrinter::highlight_range`]. This is useful when reusing a - /// `PrettyPrinter` instance for multiple different ranges. - pub fn clear_highlights(&mut self) -> &mut Self { - self.highlighted_lines.clear(); - self - } - /// Specify the highlighting theme pub fn theme(&mut self, theme: impl AsRef) -> &mut Self { self.config.theme = theme.as_ref().to_owned(); @@ -271,8 +263,8 @@ 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.config.highlighted_lines = - HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone())); + let highlight_lines = std::mem::take(&mut self.highlighted_lines); + self.config.highlighted_lines = HighlightedLineRanges(LineRanges::from(highlight_lines)); self.config.term_width = self .term_width .unwrap_or_else(|| Term::stdout().size().1 as usize); From 6a0ab0095aabd32acde1fff4cd54cf6582f6624e Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 6 Sep 2022 09:32:06 +0200 Subject: [PATCH 3/3] Add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c113df4..1b5c3c1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ ## `bat` as a library - Make `bat::PrettyPrinter::syntaxes()` iterate over new `bat::Syntax` struct instead of `&syntect::parsing::SyntaxReference`. See #2222 (@Enselic) +- Clear highlights after printing, see #1919 and #1920 (@rhysd) # v0.21.0