Skip to content

Commit

Permalink
Merge pull request #3108 from einfachIrgendwer0815/fix/2731_plain_ove…
Browse files Browse the repository at this point in the history
…rride

Fix override of --plain and --paging
  • Loading branch information
keith-hall authored Oct 31, 2024
2 parents 2be3a14 + 3b71837 commit ce7a3d9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Fix handling of inputs with combined ANSI color and attribute sequences, see #2185 and #2856 (@eth-p)
- Fix panel width when line 10000 wraps, see #2854 (@eth-p)
- Fix compile issue of `time` dependency caused by standard library regression #3045 (@cyqsimon)
- Fix override behavior of --plain and --paging, see issue #2731 and PR #3108 (@einfachIrgendwer0815)

## Other

Expand Down
22 changes: 20 additions & 2 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,30 @@ impl App {
pub fn config(&self, inputs: &[Input]) -> Result<Config> {
let style_components = self.style_components()?;

let extra_plain = self.matches.get_count("plain") > 1;
let plain_last_index = self
.matches
.indices_of("plain")
.and_then(Iterator::max)
.unwrap_or_default();
let paging_last_index = self
.matches
.indices_of("paging")
.and_then(Iterator::max)
.unwrap_or_default();

let paging_mode = match self.matches.get_one::<String>("paging").map(|s| s.as_str()) {
Some("always") => PagingMode::Always,
Some("always") => {
// Disable paging if the second -p (or -pp) is specified after --paging=always
if extra_plain && plain_last_index > paging_last_index {
PagingMode::Never
} else {
PagingMode::Always
}
}
Some("never") => PagingMode::Never,
Some("auto") | None => {
// If we have -pp as an option when in auto mode, the pager should be disabled.
let extra_plain = self.matches.get_count("plain") > 1;
if extra_plain || self.matches.get_flag("no-paging") {
PagingMode::Never
} else if inputs.iter().any(Input::is_stdin) {
Expand Down
2 changes: 0 additions & 2 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ pub fn build_app(interactive_output: bool) -> Command {
Arg::new("plain")
.overrides_with("plain")
.overrides_with("number")
.overrides_with("paging")
.short('p')
.long("plain")
.action(ArgAction::Count)
Expand Down Expand Up @@ -322,7 +321,6 @@ pub fn build_app(interactive_output: bool) -> Command {
.long("paging")
.overrides_with("paging")
.overrides_with("no-paging")
.overrides_with("plain")
.value_name("when")
.value_parser(["auto", "never", "always"])
.default_value("auto")
Expand Down
25 changes: 25 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,31 @@ fn enable_pager_if_pp_flag_comes_before_paging() {
.stdout(predicate::eq("pager-output\n").normalize());
}

#[test]
fn paging_does_not_override_simple_plain() {
bat()
.env("PAGER", "echo pager-output")
.arg("--decorations=always")
.arg("--plain")
.arg("--paging=never")
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("hello world\n"));
}

#[test]
fn simple_plain_does_not_override_paging() {
bat()
.env("PAGER", "echo pager-output")
.arg("--paging=always")
.arg("--plain")
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("pager-output\n"));
}

#[test]
fn pager_failed_to_parse() {
bat()
Expand Down

0 comments on commit ce7a3d9

Please sign in to comment.