on master help sign in
Add padding above headers when no grid
commit0040fef2153d70a4d184d5f0b77415e238f2b4e0 authorPrat T <pt2121@users.noreply.github.com> dateMon 11 May 2020, 17:57 (6 years ago) parent1a6e8d2
3 files changed+40−6
src/controller.rs+4−2 src/printer.rs+23−3 tests/integration_tests.rs+13−1
src/controller.rs+4−2
@@ -75,7 +75,7 @@ impl<'b> Controller<'b> {
7575 }
7676 };
7777
78− for input in inputs.into_iter() {
78+ for (index, input) in inputs.into_iter().enumerate() {
7979 match input.open(io::stdin().lock()) {
8080 Err(error) => {
8181 print_error(&error, writer);
@@ -128,6 +128,7 @@ impl<'b> Controller<'b> {
128128 &mut *printer,
129129 writer,
130130 &mut opened_input,
131+ index != 0,
131132 #[cfg(feature = "git")]
132133 &line_changes,
133134 );
@@ -148,10 +149,11 @@ impl<'b> Controller<'b> {
148149 printer: &mut dyn Printer,
149150 writer: &mut dyn Write,
150151 input: &mut OpenedInput,
152+ add_header_padding: bool,
151153 #[cfg(feature = "git")] line_changes: &Option<LineChanges>,
152154 ) -> Result<()> {
153155 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)?;
155157 }
156158
157159 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};
3333use crate::wrapping::WrappingMode;
3434
3535pub(crate) trait Printer {
36− fn print_header(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()>;
36+ fn print_header(
37+ &mut self,
38+ handle: &mut dyn Write,
39+ input: &OpenedInput,
40+ add_header_padding: bool,
41+ ) -> Result<()>;
3742 fn print_footer(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()>;
3843
3944 fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>;
@@ -56,7 +61,12 @@ impl SimplePrinter {
5661}
5762
5863impl Printer for SimplePrinter {
59− fn print_header(&mut self, _handle: &mut dyn Write, _input: &OpenedInput) -> Result<()> {
64+ fn print_header(
65+ &mut self,
66+ _handle: &mut dyn Write,
67+ _input: &OpenedInput,
68+ _add_header_padding: bool,
69+ ) -> Result<()> {
6070 Ok(())
6171 }
6272
@@ -219,7 +229,12 @@ impl<'a> InteractivePrinter<'a> {
219229}
220230
221231impl<'a> Printer for InteractivePrinter<'a> {
222− fn print_header(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()> {
232+ fn print_header(
233+ &mut self,
234+ handle: &mut dyn Write,
235+ input: &OpenedInput,
236+ add_header_padding: bool,
237+ ) -> Result<()> {
223238 if !self.config.style_components.header() {
224239 if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
225240 writeln!(
@@ -232,6 +247,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
232247 )?;
233248 } else if self.config.style_components.grid() {
234249 self.print_horizontal_line(handle, '┬')?;
250+ } else if add_header_padding {
251+ writeln!(handle)?;
235252 }
236253 return Ok(());
237254 }
@@ -248,6 +265,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
248265 .paint(if self.panel_width > 0 { "│ " } else { "" }),
249266 )?;
250267 } else {
268+ if add_header_padding {
269+ writeln!(handle)?;
270+ }
251271 write!(handle, "{}", " ".repeat(self.panel_width))?;
252272 }
253273
tests/integration_tests.rs+13−1
@@ -611,7 +611,7 @@ fn filename_multiple_ok() {
611611 .arg("--file-name=bar")
612612 .assert()
613613 .success()
614− .stdout("File: foo\nFile: bar\n")
614+ .stdout("File: foo\n\nFile: bar\n")
615615 .stderr("");
616616}
617617
@@ -628,6 +628,18 @@ fn filename_multiple_err() {
628628 .failure();
629629}
630630
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+
631643#[cfg(target_os = "linux")]
632644#[test]
633645fn file_with_invalid_utf8_filename() {