on master help sign in
Add padding above headers when no grid
0040fefPrat T
src/controller.rs+4−2
@@ -75,7 +75,7 @@ impl<'b> Controller<'b> {
75 }
75 }
76 };
76 };
77
77
78− for input in inputs.into_iter() {
78+ for (index, input) in inputs.into_iter().enumerate() {
79 match input.open(io::stdin().lock()) {
79 match input.open(io::stdin().lock()) {
80 Err(error) => {
80 Err(error) => {
81 print_error(&error, writer);
81 print_error(&error, writer);
@@ -128,6 +128,7 @@ impl<'b> Controller<'b> {
128 &mut *printer,
128 &mut *printer,
129 writer,
129 writer,
130 &mut opened_input,
130 &mut opened_input,
131+ index != 0,
131 #[cfg(feature = "git")]
132 #[cfg(feature = "git")]
132 &line_changes,
133 &line_changes,
133 );
134 );
@@ -148,10 +149,11 @@ impl<'b> Controller<'b> {
148 printer: &mut dyn Printer,
149 printer: &mut dyn Printer,
149 writer: &mut dyn Write,
150 writer: &mut dyn Write,
150 input: &mut OpenedInput,
151 input: &mut OpenedInput,
152+ add_header_padding: bool,
151 #[cfg(feature = "git")] line_changes: &Option<LineChanges>,
153 #[cfg(feature = "git")] line_changes: &Option<LineChanges>,
152 ) -> Result<()> {
154 ) -> Result<()> {
153 if !input.reader.first_line.is_empty() || self.config.style_components.header() {
155 if !input.reader.first_line.is_empty() || self.config.style_components.header() {
154− printer.print_header(writer, input)?;
156+ printer.print_header(writer, input, add_header_padding)?;
155 }
157 }
156
158
157 if !input.reader.first_line.is_empty() {
159 if !input.reader.first_line.is_empty() {
src/printer.rs+23−3
@@ -33,7 +33,12 @@ use crate::terminal::{as_terminal_escaped, to_ansi_color};
33use crate::wrapping::WrappingMode;
33use crate::wrapping::WrappingMode;
34
34
35pub(crate) trait Printer {
35pub(crate) trait Printer {
36+ fn print_header(
37+ &mut self,
36− fn print_header(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()>;
38+ handle: &mut dyn Write,
39+ input: &OpenedInput,
40+ add_header_padding: bool,
41+ ) -> Result<()>;
37 fn print_footer(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()>;
42 fn print_footer(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()>;
38
43
39 fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>;
44 fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>;
@@ -56,7 +61,12 @@ impl SimplePrinter {
56}
61}
57
62
58impl Printer for SimplePrinter {
63impl Printer for SimplePrinter {
64+ fn print_header(
65+ &mut self,
59− fn print_header(&mut self, _handle: &mut dyn Write, _input: &OpenedInput) -> Result<()> {
66+ _handle: &mut dyn Write,
67+ _input: &OpenedInput,
68+ _add_header_padding: bool,
69+ ) -> Result<()> {
60 Ok(())
70 Ok(())
61 }
71 }
62
72
@@ -219,7 +229,12 @@ impl<'a> InteractivePrinter<'a> {
219}
229}
220
230
221impl<'a> Printer for InteractivePrinter<'a> {
231impl<'a> Printer for InteractivePrinter<'a> {
232+ fn print_header(
233+ &mut self,
222− fn print_header(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()> {
234+ handle: &mut dyn Write,
235+ input: &OpenedInput,
236+ add_header_padding: bool,
237+ ) -> Result<()> {
223 if !self.config.style_components.header() {
238 if !self.config.style_components.header() {
224 if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
239 if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
225 writeln!(
240 writeln!(
@@ -232,6 +247,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
232 )?;
247 )?;
233 } else if self.config.style_components.grid() {
248 } else if self.config.style_components.grid() {
234 self.print_horizontal_line(handle, '┬')?;
249 self.print_horizontal_line(handle, '┬')?;
250+ } else if add_header_padding {
251+ writeln!(handle)?;
235 }
252 }
236 return Ok(());
253 return Ok(());
237 }
254 }
@@ -248,6 +265,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
248 .paint(if self.panel_width > 0 { "│ " } else { "" }),
265 .paint(if self.panel_width > 0 { "│ " } else { "" }),
249 )?;
266 )?;
250 } else {
267 } else {
268+ if add_header_padding {
269+ writeln!(handle)?;
270+ }
251 write!(handle, "{}", " ".repeat(self.panel_width))?;
271 write!(handle, "{}", " ".repeat(self.panel_width))?;
252 }
272 }
253
273
tests/integration_tests.rs+13−1
@@ -611,7 +611,7 @@ fn filename_multiple_ok() {
611 .arg("--file-name=bar")
611 .arg("--file-name=bar")
612 .assert()
612 .assert()
613 .success()
613 .success()
614− .stdout("File: foo\nFile: bar\n")
614+ .stdout("File: foo\n\nFile: bar\n")
615 .stderr("");
615 .stderr("");
616}
616}
617
617
@@ -628,6 +628,18 @@ fn filename_multiple_err() {
628 .failure();
628 .failure();
629}
629}
630
630
631+#[test]
632+fn header_padding() {
633+ bat()
634+ .arg("--decorations=always")
635+ .arg("--style=plain")
636+ .arg("test.txt")
637+ .arg("single-line.txt")
638+ .assert()
639+ .stdout("hello world\n\nSingle Line\n")
640+ .stderr("");
641+}
642+
631#[cfg(target_os = "linux")]
643#[cfg(target_os = "linux")]
632#[test]
644#[test]
633fn file_with_invalid_utf8_filename() {
645fn file_with_invalid_utf8_filename() {