Repository: sharkdp/bat (language: rust)

Issue:
`PrettyPrinter::header` does not display a header
<!--

Hey there, thank you for reporting a bug!

Please note that the following bugs have already been reported:

* dpkg: error processing archive /some/path/some-program.deb (--unpack):
  trying to overwrite '/usr/.crates2.json'

  See https://github.com/sharkdp/bat/issues/938

-->

### **What steps will reproduce the bug?**

Compile and run the following program:

```rust
fn main() {
    bat::PrettyPrinter::new()
        .input_file("Cargo.toml")
        .header(true)
        .print()
        .unwrap();
}
```

### **What happens?**

The file is printed without a header.

### **What did you expect to happen instead?**

The file would be printed with a header containing the filename.

### **How did you install `bat`?**

Via cargo:

```toml
[dependencies]
bat = "0.22.1"
```

---

It looks like the issue is due to the fact that there exist both "header filename" and "header filesize" style components. `PrettyPrinter::header` adds `StyleComponent::Header` to the list of components:

https://github.com/sharkdp/bat/blob/f215d07366dac50aa7068e9c3f8d5f46b09b4c1a/src/pretty_printer.rs#L279-L281

But when the components are checked, `bat` only looks for `StyleComponent::HeaderFilename` or `StyleComponent::HeaderFilesize`:

https://github.com/sharkdp/bat/blob/f215d07366dac50aa7068e9c3f8d5f46b09b4c1a/src/style.rs#L109-L119

I'm not sure what the best way to tackle this would be. Should we also check for `StyleComponent::Header` in `StyleComponents::header`? Should the `PrettyPrinter::header` function push in `StyleComponent::HeaderFilename`? Should `PrettyPrinter` expose a `header_filename(bool)` function?


Discussion / hints:
Oh, interesting. Thank you for reporting this. This is also broken in the official examples:
```
▶ cargo run --quiet --example yaml
─────┬─────────────────────────────────────────────────────────────────────────────────────
   1 │ ---
   2 │ name: Anne Mustermann
   3 │ height: 1.76
   4 │ adult: true
   5 │ children:
   6 │   - name: Max Mustermann
   7 │     height: 1.32
   8 │     adult: false
   9 │     children: []
─────┴─────────────────────────────────────────────────────────────────────────────────────
```

This should actually show a header with a filename.



> I'm not sure what the best way to tackle this would be. Should we also check for `StyleComponent::Header` in `StyleComponents::header`? Should the `PrettyPrinter::header` function push in `StyleComponent::HeaderFilename`? Should `PrettyPrinter` expose a `header_filename(bool)` function?

I think for now `.header(true)` should be an alias for `.header_filename(…)`, to keep backwards-compatibility (which was already broken by this bug, but okay). And then maybe we can actually deprecate `.header` in the future?

@Enselic @keith-hall what do you think?
> I think for now .header(true) should be an alias for .header_filename(…), to keep backwards-compatibility (which was already broken by this bug, but okay). And then maybe we can actually deprecate .header in the future?

Sounds good to me 👍 

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f72307086..21d900a148 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -33,6 +33,8 @@
 
 ## `bat` as a library
 
+- `PrettyPrinter::header` correctly displays a header with the filename, see #2378 and #2406 (@cstyles)
+
 
 # v0.22.1
 
diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs
index bc89d4fc4e..02a7a519f5 100644
--- a/src/pretty_printer.rs
+++ b/src/pretty_printer.rs
@@ -19,7 +19,7 @@ use crate::paging::PagingMode;
 
 #[derive(Default)]
 struct ActiveStyleComponents {
-    header: bool,
+    header_filename: bool,
     #[cfg(feature = "git")]
     vcs_modification_markers: bool,
     grid: bool,
@@ -140,7 +140,7 @@ impl<'a> PrettyPrinter<'a> {
 
     /// Whether to show a header with the file name
     pub fn header(&mut self, yes: bool) -> &mut Self {
-        self.active_style_components.header = yes;
+        self.active_style_components.header_filename = yes;
         self
     }
 
@@ -277,8 +277,10 @@ impl<'a> PrettyPrinter<'a> {
         if self.active_style_components.rule {
             self.config.style_components.insert(StyleComponent::Rule);
         }
-        if self.active_style_components.header {
-            self.config.style_components.insert(StyleComponent::Header);
+        if self.active_style_components.header_filename {
+            self.config
+                .style_components
+                .insert(StyleComponent::HeaderFilename);
         }
         if self.active_style_components.line_numbers {
             self.config


Test patch: