Repository: sharkdp/bat (language: rust)

Issue:
Cannot clear highlights in `bat::PrettyPrinter` instance
<!-- Hey there, thank you for creating an issue! -->

**Describe the bug you encountered:**

Let's say we have the following code:

```rust
use bat::PrettyPrinter;

let targets: (PathBuf, Vec<(usize, usize)>) = vec![];

let mut pp = PrettyPrinter::new();
for (path, ranges) in targets.iter() {
    pp.input_file(path);
    for (start, end) in ranges.iter() {
        pp.highlight_range(*start, *end);
    }

    // This clears inputs, but does not clear highlights
    pp.print().unwrap();
}
```

Since there are multiple files with different highlight ranges, I want to print each files with different ranges. However, currently there is no way to clear highlights in `bat::PrettyPrinter` instance.

Creating `bat::PrettyPrinter` instance for each file is an option. However it is much slower because `bat::PrettyPrinter::new` deserializes binary assets and it is time consuming.

**What did you expect to happen instead?**

I'd like `bat::PrettyPrinter` to provide some API to clear highlights.


**How did you install `bat`?**

<!-- apt-get, homebrew, GitHub release, etc. -->

```toml
[dependencies]
bat = { version = "0.18.3", default-features = false }
```

in my project's `Cargo.toml`.

---

**bat version and environment**

- bat: 0.18.3
- OS: macOS 10.15
- Rust: 1.56
<!--
In order to reproduce your issue, please add some information about the environment
in which you're running bat. To do this, run the full `bat` command that demonstrates
the bug, and attach the `--diagnostic` option:

    bat [other options and arguments…] --diagnostic

Finally, paste the Markdown output here. Please make sure that it does not reveal any
personal information.


If you are running bat 0.17.1 or older (where --diagnostic is not available), please
run the script at

    https://github.com/sharkdp/bat/blob/master/diagnostics/info.sh

(click "Raw" to get the actual source code) and paste the Markdown output here. If you
are on Windows, please let us know your bat version and your Windows version.
-->

Cannot clear highlights in `bat::PrettyPrinter` instance
<!-- Hey there, thank you for creating an issue! -->

**Describe the bug you encountered:**

Let's say we have the following code:

```rust
use bat::PrettyPrinter;

let targets: (PathBuf, Vec<(usize, usize)>) = vec![];

let mut pp = PrettyPrinter::new();
for (path, ranges) in targets.iter() {
    pp.input_file(path);
    for (start, end) in ranges.iter() {
        pp.highlight_range(*start, *end);
    }

    // This clears inputs, but does not clear highlights
    pp.print().unwrap();
}
```

Since there are multiple files with different highlight ranges, I want to print each files with different ranges. However, currently there is no way to clear highlights in `bat::PrettyPrinter` instance.

Creating `bat::PrettyPrinter` instance for each file is an option. However it is much slower because `bat::PrettyPrinter::new` deserializes binary assets and it is time consuming.

**What did you expect to happen instead?**

I'd like `bat::PrettyPrinter` to provide some API to clear highlights.


**How did you install `bat`?**

<!-- apt-get, homebrew, GitHub release, etc. -->

```toml
[dependencies]
bat = { version = "0.18.3", default-features = false }
```

in my project's `Cargo.toml`.

---

**bat version and environment**

- bat: 0.18.3
- OS: macOS 10.15
- Rust: 1.56
<!--
In order to reproduce your issue, please add some information about the environment
in which you're running bat. To do this, run the full `bat` command that demonstrates
the bug, and attach the `--diagnostic` option:

    bat [other options and arguments…] --diagnostic

Finally, paste the Markdown output here. Please make sure that it does not reveal any
personal information.


If you are running bat 0.17.1 or older (where --diagnostic is not available), please
run the script at

    https://github.com/sharkdp/bat/blob/master/diagnostics/info.sh

(click "Raw" to get the actual source code) and paste the Markdown output here. If you
are on Windows, please let us know your bat version and your Windows version.
-->



Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c113df4f0..1b5c3c1d7c 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
diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs
index 286445134f..0d68ba40d9 100644
--- a/src/pretty_printer.rs
+++ b/src/pretty_printer.rs
@@ -263,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<bool> {
-        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);


Test patch: