on master help sign in
Fix #1413 Can't use `bat` at all! (Error: Use of bat as a pager 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.

commitdd0d44bbb3608618427153515a71cdc11c041988 authorMartin Nordholts <enselic@gmail.com> dateSun 10 Jan 2021, 16:48 (6 years ago) parent060b998
2 files changed+38−5
src/pager.rs+14−4 tests/integration_tests.rs+24−1
src/pager.rs+14−4
@@ -98,10 +98,20 @@ pub(crate) fn get_pager(config_pager: Option<&str>) -> Result<Option<Pager>, Par
9898 Some((bin, args)) => {
9999 let kind = PagerKind::from_bin(bin);
100100
101− // 'more' and 'most' do not supports colors; automatically use 'less' instead
102− // if the problematic pager came from the generic PAGER env var
103− let no_color_support = kind == PagerKind::More || kind == PagerKind::Most;
104− let use_less_instead = no_color_support && source == PagerSource::EnvVarPager;
101+ let use_less_instead = match source {
102+ PagerSource::EnvVarPager => {
103+ // 'more' and 'most' do not supports colors; automatically use 'less' instead
104+ // if the problematic pager came from the generic PAGER env var
105+ let no_color_support = kind == PagerKind::More || kind == PagerKind::Most;
106+
107+ // If PAGER=bat, silently use 'less' instead to prevent recursion ...
108+ let is_self = kind == PagerKind::Bat;
109+
110+ no_color_support || is_self
111+ },
112+ // Never silently replace with less if bat-specific means to set a pager is used
113+ _ => false
114+ } ;
105115
106116 Ok(Some(if use_less_instead {
107117 let no_args = vec![];
tests/integration_tests.rs+24−1
@@ -499,6 +499,28 @@ fn pager_disable() {
499499 .stdout(predicate::eq("hello world\n").normalize());
500500}
501501
502+#[test]
503+fn env_var_pager_value_bat() {
504+ bat()
505+ .env("PAGER", "bat")
506+ .arg("--paging=always")
507+ .arg("test.txt")
508+ .assert()
509+ .success()
510+ .stdout(predicate::eq("hello world\n").normalize());
511+}
512+
513+#[test]
514+fn env_var_bat_pager_value_bat() {
515+ bat()
516+ .env("BAT_PAGER", "bat")
517+ .arg("--paging=always")
518+ .arg("test.txt")
519+ .assert()
520+ .failure()
521+ .stderr(predicate::str::contains("bat as a pager is disallowed"));
522+}
523+
502524#[test]
503525fn pager_value_bat() {
504526 bat()
@@ -506,7 +528,8 @@ fn pager_value_bat() {
506528 .arg("--paging=always")
507529 .arg("test.txt")
508530 .assert()
509− .failure();
531+ .failure()
532+ .stderr(predicate::str::contains("bat as a pager is disallowed"));
510533}
511534
512535/// We shall use less instead of most if PAGER is used since PAGER