Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -s/--squeeze and --squeeze-limit. #1441

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

## Features

- `bat --squeeze`/`bat -s` will now squeeze consecutive empty lines, see #1441 (@eth-p)
- `bat --squeeze-limit` to set the maximum number of empty consecutive when using `--squeeze`, see #1441 (@eth-p)

## Bugfixes

## Other
Expand All @@ -13,6 +16,8 @@

## `bat` as a library

- `PrettyPrinter::squeeze_empty_lines` to support line squeezing for bat as a library, see #1441 (@eth-p)

## Packaging

- `bat` is now available on snapstore with package name called `batcat`, see #1401 (@purveshpatel511)
Expand Down
10 changes: 10 additions & 0 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ impl App {
.map(LineRanges::from)
.map(HighlightedLineRanges)
.unwrap_or_default(),
squeeze_lines: if self.matches.is_present("squeeze") {
Some(
self.matches
.value_of("squeeze-limit")
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(1),
)
} else {
None
},
})
}

Expand Down
15 changes: 15 additions & 0 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,21 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.help("Display all supported highlighting themes.")
.long_help("Display a list of supported themes for syntax highlighting."),
)
.arg(
Arg::with_name("squeeze")
.long("squeeze")
.short("s")
.help("Squeeze consecutive empty lines.")
.long_help("Squeeze consecutive empty lines into a single empty line.")
)
.arg(
Arg::with_name("squeeze-limit")
.long("squeeze-limit")
.takes_value(true)
.validator(|s| s.parse::<usize>().map(|_| ()).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::with_name("style")
.long("style")
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ pub struct Config<'a> {

/// Ranges of lines which should be highlighted with a special background color
pub highlighted_lines: HighlightedLineRanges,

/// The maximum number of consecutive empty lines to display
Copy link
Owner

@sharkdp sharkdp Dec 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we expand this a bit to explain what happens if it is set to None? Also, maybe an example wouldn't hurt? My understanding would be that Some(3) would mean that

1 something
2 like
3 
4 
5 
6 this

would be printed unchanged, while

1 something
2 like
3
4
5
6
7 this

would be squeezed (to one empty line or to three empty lines?).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To three empty lines.

I had a hard time trying to describe how it worked in only a few comments. The example would definitely be a good idea, thanks!

pub squeeze_lines: Option<usize>,
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions src/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ impl<'a> PrettyPrinter<'a> {
self
}

/// Specify the maximum number of consecutive empty lines to print.
pub fn squeeze_empty_lines(&mut self, maximum: Option<usize>) -> &mut Self {
self.config.squeeze_lines = maximum;
self
}

/// Specify the highlighting theme
pub fn theme(&mut self, theme: impl AsRef<str>) -> &mut Self {
self.config.theme = theme.as_ref().to_owned();
Expand Down
15 changes: 15 additions & 0 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub(crate) struct InteractivePrinter<'a> {
highlighter: Option<HighlightLines<'a>>,
syntax_set: &'a SyntaxSet,
background_color_highlight: Option<Color>,
consecutive_empty_lines: usize,
}

impl<'a> InteractivePrinter<'a> {
Expand Down Expand Up @@ -197,6 +198,7 @@ impl<'a> InteractivePrinter<'a> {
highlighter,
syntax_set: &assets.syntax_set,
background_color_highlight,
consecutive_empty_lines: 0,
})
}

Expand Down Expand Up @@ -395,6 +397,19 @@ impl<'a> Printer for InteractivePrinter<'a> {
highlighter.highlight(line.as_ref(), self.syntax_set)
};

// Skip squeezed lines.
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 > squeeze_limit {
return Ok(());
}
} else {
self.consecutive_empty_lines = 0;
}
}

// Skip lines that are out of range.
if out_of_range {
return Ok(());
}
Expand Down