Add --squeeze-limit to specify max number of consecutive empty lines

Co-authored-by: einfachIrgendwer0815 <85333734+einfachIrgendwer0815@users.noreply.github.com>
This commit is contained in:
Ethan P 2020-12-16 18:23:14 -08:00 committed by einfachIrgendwer0815
parent 0c7e5299bf
commit 0e4e10edb6
No known key found for this signature in database
GPG Key ID: 58D55E5F117DA873
5 changed files with 18 additions and 7 deletions

View File

@ -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::<usize>("squeeze-limit")
.map(|limit| limit.to_owned())
.unwrap_or(1),
)
} else {
0
None
},
})
}

View File

@ -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::<usize>().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")

View File

@ -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<usize>,
}
#[cfg(all(feature = "minimal-application", feature = "paging"))]

View File

@ -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<usize>) -> &mut Self {
self.config.squeeze_lines = maximum;
self
}

View File

@ -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 {