Skip to content

Commit

Permalink
Fix sharkdp#1413 Can't use bat at all! (Error: Use of bat as a page…
Browse files Browse the repository at this point in the history
…r is disallowed...)

Fixed by implementing the proposal by sharkdp:

* Allow PAGER=bat, but ignore the setting in bat and simply default to
  less. Unless of course, BAT_PAGER or --pager is used to overwrite the
  value of PAGER.

* Disallow the usage of bat within BAT_PAGER and --pager.
  • Loading branch information
Enselic committed Jan 10, 2021
1 parent 149a017 commit b5ab192
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/pager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,20 @@ pub(crate) fn get_pager(config_pager: Option<&str>) -> Result<Option<Pager>, Par
Some((bin, args)) => {
let kind = PagerKind::from_bin(bin);

// 'more' and 'most' do not supports colors; automatically use 'less' instead
// if the problematic pager came from the generic PAGER env var
let no_color_support = kind == PagerKind::More || kind == PagerKind::Most;
let use_less_instead = no_color_support && source == PagerSource::EnvVarPager;
let use_less_instead = match source {
PagerSource::EnvVarPager => {
// 'more' and 'most' do not supports colors; automatically use 'less' instead
// if the problematic pager came from the generic PAGER env var
let no_color_support = kind == PagerKind::More || kind == PagerKind::Most;

// If PAGER=bat, silently use 'less' instead to prevent recursion ...
let is_self = kind == PagerKind::Bat;

no_color_support || is_self
},
// Never silently replace with less if bat-specific means to set a pager is used
_ => false
} ;

Ok(Some(if use_less_instead {
let no_args = vec![];
Expand Down
25 changes: 24 additions & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,37 @@ fn pager_disable() {
.stdout(predicate::eq("hello world\n").normalize());
}

#[test]
fn env_var_pager_value_bat() {
bat()
.env("PAGER", "bat")
.arg("--paging=always")
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("hello world\n").normalize());
}

#[test]
fn env_var_bat_pager_value_bat() {
bat()
.env("BAT_PAGER", "bat")
.arg("--paging=always")
.arg("test.txt")
.assert()
.failure()
.stderr(predicate::str::contains("bat as a pager is disallowed"));
}

#[test]
fn pager_value_bat() {
bat()
.arg("--pager=bat")
.arg("--paging=always")
.arg("test.txt")
.assert()
.failure();
.failure()
.stderr(predicate::str::contains("bat as a pager is disallowed"));
}

/// We shall use less instead of most if PAGER is used since PAGER
Expand Down

0 comments on commit b5ab192

Please sign in to comment.