summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPrat T <pt2121@users.noreply.github.com>2020-05-11 17:57:51 -0700
committerDavid Peter <sharkdp@users.noreply.github.com>2020-05-12 16:37:29 +0200
commit0040fef2153d70a4d184d5f0b77415e238f2b4e0 (patch)
tree6573bbcd294e890aad4654aa384adbaaa7c75452
parent1a6e8d297ffbfa2e82a20daa9872bac73e9f5005 (diff)
Add padding above headers when no grid
-rw-r--r--src/controller.rs6
-rw-r--r--src/printer.rs26
-rw-r--r--tests/integration_tests.rs14
3 files changed, 40 insertions, 6 deletions
diff --git a/src/controller.rs b/src/controller.rs
index 87286092..a7f2f3c0 100644
--- a/src/controller.rs
+++ b/src/controller.rs
@@ -75,7 +75,7 @@ impl<'b> Controller<'b> {
}
};
- for input in inputs.into_iter() {
+ for (index, input) in inputs.into_iter().enumerate() {
match input.open(io::stdin().lock()) {
Err(error) => {
print_error(&error, writer);
@@ -128,6 +128,7 @@ impl<'b> Controller<'b> {
&mut *printer,
writer,
&mut opened_input,
+ index != 0,
#[cfg(feature = "git")]
&line_changes,
);
@@ -148,10 +149,11 @@ impl<'b> Controller<'b> {
printer: &mut dyn Printer,
writer: &mut dyn Write,
input: &mut OpenedInput,
+ add_header_padding: bool,
#[cfg(feature = "git")] line_changes: &Option<LineChanges>,
) -> Result<()> {
if !input.reader.first_line.is_empty() || self.config.style_components.header() {
- printer.print_header(writer, input)?;
+ printer.print_header(writer, input, add_header_padding)?;
}
if !input.reader.first_line.is_empty() {
diff --git a/src/printer.rs b/src/printer.rs
index bddf6d89..85dc9093 100644
--- a/src/printer.rs
+++ b/src/printer.rs
@@ -33,7 +33,12 @@ use crate::terminal::{as_terminal_escaped, to_ansi_color};
use crate::wrapping::WrappingMode;
pub(crate) trait Printer {
- fn print_header(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()>;
+ fn print_header(
+ &mut self,
+ handle: &mut dyn Write,
+ input: &OpenedInput,
+ add_header_padding: bool,
+ ) -> Result<()>;
fn print_footer(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()>;
fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>;
@@ -56,7 +61,12 @@ impl SimplePrinter {
}
impl Printer for SimplePrinter {
- fn print_header(&mut self, _handle: &mut dyn Write, _input: &OpenedInput) -> Result<()> {
+ fn print_header(
+ &mut self,
+ _handle: &mut dyn Write,
+ _input: &OpenedInput,
+ _add_header_padding: bool,
+ ) -> Result<()> {
Ok(())
}
@@ -219,7 +229,12 @@ impl<'a> InteractivePrinter<'a> {
}
impl<'a> Printer for InteractivePrinter<'a> {
- fn print_header(&mut self, handle: &mut dyn Write, input: &OpenedInput) -> Result<()> {
+ fn print_header(
+ &mut self,
+ handle: &mut dyn Write,
+ input: &OpenedInput,
+ add_header_padding: bool,
+ ) -> Result<()> {
if !self.config.style_components.header() {
if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
writeln!(
@@ -232,6 +247,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
)?;
} else if self.config.style_components.grid() {
self.print_horizontal_line(handle, '┬')?;
+ } else if add_header_padding {
+ writeln!(handle)?;
}
return Ok(());
}
@@ -248,6 +265,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
.paint(if self.panel_width > 0 { "│ " } else { "" }),
)?;
} else {
+ if add_header_padding {
+ writeln!(handle)?;
+ }
write!(handle, "{}", " ".repeat(self.panel_width))?;
}
diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs
index 89c15832..c8d3d37f 100644
--- a/tests/integration_tests.rs
+++ b/tests/integration_tests.rs
@@ -611,7 +611,7 @@ fn filename_multiple_ok() {
.arg("--file-name=bar")
.assert()
.success()
- .stdout("File: foo\nFile: bar\n")
+ .stdout("File: foo\n\nFile: bar\n")
.stderr("");
}
@@ -628,6 +628,18 @@ fn filename_multiple_err() {
.failure();
}
+#[test]
+fn header_padding() {
+ bat()
+ .arg("--decorations=always")
+ .arg("--style=plain")
+ .arg("test.txt")
+ .arg("single-line.txt")
+ .assert()
+ .stdout("hello world\n\nSingle Line\n")
+ .stderr("");
+}
+
#[cfg(target_os = "linux")]
#[test]
fn file_with_invalid_utf8_filename() {