From 0e4e10edb6663b9ee73c850b9679b08929501c2f Mon Sep 17 00:00:00 2001 From: Ethan P Date: Wed, 16 Dec 2020 18:23:14 -0800 Subject: [PATCH] Add --squeeze-limit to specify max number of consecutive empty lines Co-authored-by: einfachIrgendwer0815 <85333734+einfachIrgendwer0815@users.noreply.github.com> --- src/bin/bat/app.rs | 9 +++++++-- src/bin/bat/clap_app.rs | 7 +++++++ src/config.rs | 2 +- src/pretty_printer.rs | 3 +-- src/printer.rs | 4 ++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 58f683cd..f462ce8d 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -291,9 +291,14 @@ impl App { 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 + Some( + self.matches + .get_one::("squeeze-limit") + .map(|limit| limit.to_owned()) + .unwrap_or(1), + ) } else { - 0 + None }, }) } diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index 4bcee8d6..0c28ce7d 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -395,6 +395,13 @@ pub fn build_app(interactive_output: bool) -> Command { .help("Squeeze consecutive empty lines.") .long_help("Squeeze consecutive empty lines into a single empty line.") ) + .arg( + Arg::new("squeeze-limit") + .long("squeeze-limit") + .value_parser(|s: &str| s.parse::().map_err(|_| "Requires a non-negative number".to_owned())) + .help("The maximum number of consecutive empty lines.") + .long_help("Set the maximum number of consecutive empty lines to be printed.") + ) .arg( Arg::new("style") .long("style") diff --git a/src/config.rs b/src/config.rs index ee0d5bc1..0298bb2a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -99,7 +99,7 @@ pub struct Config<'a> { pub set_terminal_title: bool, /// The maximum number of consecutive empty lines to display - pub squeeze_lines: usize, + pub squeeze_lines: Option, } #[cfg(all(feature = "minimal-application", feature = "paging"))] diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index 614f0c91..c6203aa9 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -231,8 +231,7 @@ impl<'a> PrettyPrinter<'a> { } /// Specify the maximum number of consecutive empty lines to print. - /// If set to `0`, all empty lines will be shown. - pub fn squeeze_empty_lines(&mut self, maximum: usize) -> &mut Self { + pub fn squeeze_empty_lines(&mut self, maximum: Option) -> &mut Self { self.config.squeeze_lines = maximum; self } diff --git a/src/printer.rs b/src/printer.rs index 636c0800..343f4486 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -580,10 +580,10 @@ impl<'a> Printer for InteractivePrinter<'a> { } // Skip squeezed lines. - if self.config.squeeze_lines > 0 { + if let Some(squeeze_limit) = self.config.squeeze_lines { 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 { + if self.consecutive_empty_lines > squeeze_limit { return Ok(()); } } else {