summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEthan P <eth-p+git@hidden.email>2020-12-16 18:10:29 -0800
committereinfachIrgendwer0815 <85333734+einfachIrgendwer0815@users.noreply.github.com>2024-02-24 13:51:00 +0100
commitc36ed3281694375a5ee85b86c2c710073c1195dd (patch)
tree86a551438c5520a5b354cb905b5824e668ea3c76
parente1a3fc5529d55abc73aa5fb18368d22f9b9bb71c (diff)
Add --squeeze/-s option
Co-authored-by: einfachIrgendwer0815 <85333734+einfachIrgendwer0815@users.noreply.github.com>
-rw-r--r--src/bin/bat/app.rs5
-rw-r--r--src/bin/bat/clap_app.rs8
-rw-r--r--src/config.rs3
-rw-r--r--src/printer.rs14
4 files changed, 30 insertions, 0 deletions
diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs
index 8843d53b..58f683cd 100644
--- a/src/bin/bat/app.rs
+++ b/src/bin/bat/app.rs
@@ -290,6 +290,11 @@ impl App {
#[cfg(feature = "lessopen")]
use_lessopen: self.matches.get_flag("lessopen"),
set_terminal_title: self.matches.get_flag("set-terminal-title"),
+ squeeze_lines: if self.matches.get_flag("squeeze") {
+ 1
+ } else {
+ 0
+ },
})
}
diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs
index d3cb9276..4bcee8d6 100644
--- a/src/bin/bat/clap_app.rs
+++ b/src/bin/bat/clap_app.rs
@@ -388,6 +388,14 @@ pub fn build_app(interactive_output: bool) -> Command {
.long_help("Display a list of supported themes for syntax highlighting."),
)
.arg(
+ Arg::new("squeeze")
+ .long("squeeze")
+ .short('s')
+ .action(ArgAction::SetTrue)
+ .help("Squeeze consecutive empty lines.")
+ .long_help("Squeeze consecutive empty lines into a single empty line.")
+ )
+ .arg(
Arg::new("style")
.long("style")
.value_name("components")
diff --git a/src/config.rs b/src/config.rs
index c5cc2abd..ee0d5bc1 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -97,6 +97,9 @@ pub struct Config<'a> {
// Weather or not to set terminal title when using a pager
pub set_terminal_title: bool,
+
+ /// The maximum number of consecutive empty lines to display
+ pub squeeze_lines: usize,
}
#[cfg(all(feature = "minimal-application", feature = "paging"))]
diff --git a/src/printer.rs b/src/printer.rs
index 8fd6bc8e..636c0800 100644
--- a/src/printer.rs
+++ b/src/printer.rs
@@ -187,6 +187,7 @@ pub(crate) struct InteractivePrinter<'a> {
pub line_changes: &'a Option<LineChanges>,
highlighter_from_set: Option<HighlighterFromSet<'a>>,
background_color_highlight: Option<Color>,
+ consecutive_empty_lines: usize,
}
impl<'a> InteractivePrinter<'a> {
@@ -272,6 +273,7 @@ impl<'a> InteractivePrinter<'a> {
line_changes,
highlighter_from_set,
background_color_highlight,
+ consecutive_empty_lines: 0,
})
}
@@ -577,6 +579,18 @@ impl<'a> Printer for InteractivePrinter<'a> {
return Ok(());
}
+ // Skip squeezed lines.
+ if self.config.squeeze_lines > 0 {
+ if line.trim_end_matches(|c| c == '\r' || c == '\n').is_empty() {
+ self.consecutive_empty_lines += 1;
+ if self.consecutive_empty_lines > self.config.squeeze_lines {
+ return Ok(());
+ }
+ } else {
+ self.consecutive_empty_lines = 0;
+ }
+ }
+
let mut cursor: usize = 0;
let mut cursor_max: usize = self.config.term_width;
let mut cursor_total: usize = 0;