on master help sign in
Simplify and polish pager.rs and related code
commitdcfe883f4be477dfe2ef3cef254d9446048a3bfe authorMartin Nordholts <enselic@gmail.com> dateMon 28 Dec 2020, 22:29 (6 years ago) parentcc0f8ca
4 files changed+22−22
CHANGELOG.md+1−1 src/output.rs+2−1 src/pager.rs+17−18 tests/integration_tests.rs+2−2
CHANGELOG.md+1−1
@@ -10,7 +10,7 @@
1010- Only print themes hint in interactive mode (`bat --list-themes`), see #1439 (@rsteube)
1111- Make ./tests/syntax-tests/regression_test.sh work on recent versions of macOS, see #1443 (@Enselic)
1212- VimL syntax highlighting fix, see #1450 (@esensar)
13−- Ignore PAGER=most by default with a warning to stderr, but allow override with BAT_PAGER or --config, see #1063 (@Enselic)
13+- Ignore PAGER=most with a warning to stderr, but allow override with BAT_PAGER or --config, see #1063 (@Enselic)
1414
1515## Other
1616
src/output.rs+2−1
@@ -52,6 +52,7 @@ impl OutputType {
5252 use std::path::PathBuf;
5353 use std::process::{Command, Stdio};
5454 use crate::pager::*;
55+ use crate::bat_warning;
5556
5657 let Pager { pager, source } = get_pager(pager_from_config);
5758
@@ -67,7 +68,7 @@ impl OutputType {
6768 }
6869
6970 if pager_path.file_stem() == Some(&OsString::from("most")) && source == PagerSource::PagerEnvVar {
70− eprintln!("WARNING: Ignoring PAGER=\"{}\": Coloring not supported. Override with BAT_PAGER=\"{}\" or --pager \"{}\"", pager, pager, pager);
71+ bat_warning!("Ignoring PAGER=\"{}\": Coloring not supported. Override with BAT_PAGER=\"{}\" or --pager \"{}\"", pager, pager, pager);
7172 return Ok(OutputType::stdout());
7273 }
7374
src/pager.rs+17−18
@@ -1,28 +1,27 @@
1+/// If we use a pager, this enum tells us from where we were told to use it.
12#[derive(Debug, PartialEq)]
23pub enum PagerSource {
4+ /// From --config
5+ Config,
6+
37 /// From the env var BAT_PAGER
48 BatPagerEnvVar,
59
610 /// From the env var PAGER
711 PagerEnvVar,
812
9− /// From --config
10− Config,
11−
1213 /// No pager was specified, default is used
1314 Default,
1415}
1516
17+/// A pager such as 'less', and from where we got it.
1618pub struct Pager {
1719 pub pager: String,
1820 pub source: PagerSource,
1921}
2022
2123impl Pager {
22− fn new(
23− pager: &str,
24− source: PagerSource
25− ) -> Pager {
24+ fn new(pager: &str, source: PagerSource) -> Pager {
2625 Pager {
2726 pager: String::from(pager),
2827 source,
@@ -30,16 +29,16 @@ impl Pager {
3029 }
3130}
3231
33−pub fn get_pager(
34− pager_from_config: Option<&str>,
35−) -> Pager {
36− if pager_from_config.is_some() {
37− return Pager::new(pager_from_config.unwrap(), PagerSource::Config);
38− } else {
39− return match (std::env::var("BAT_PAGER"), std::env::var("PAGER")) {
40− (Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
41− (_, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
42− _ => Pager::new("less", PagerSource::Default),
43− };
32+/// Returns what pager to use, after looking at both config and environment variables.
33+pub fn get_pager(pager_from_config: Option<&str>) -> Pager {
34+ match (
35+ pager_from_config,
36+ std::env::var("BAT_PAGER"),
37+ std::env::var("PAGER"),
38+ ) {
39+ (Some(config), _, _) => Pager::new(config, PagerSource::Config),
40+ (_, Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
41+ (_, _, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
42+ _ => Pager::new("less", PagerSource::Default),
4443 }
4544}
tests/integration_tests.rs+2−2
@@ -423,7 +423,7 @@ fn pager_most() {
423423 .arg("test.txt")
424424 .assert()
425425 .success()
426− .stderr(predicate::eq("WARNING: Ignoring PAGER=\"most\": Coloring not supported. Override with BAT_PAGER=\"most\" or --pager \"most\"\n").normalize())
426+ .stderr(predicate::eq("\x1b[33m[bat warning]\x1b[0m: Ignoring PAGER=\"most\": Coloring not supported. Override with BAT_PAGER=\"most\" or --pager \"most\"\n").normalize())
427427 .stdout(predicate::eq("hello world\n").normalize());
428428}
429429
@@ -435,7 +435,7 @@ fn pager_most_with_arg() {
435435 .arg("test.txt")
436436 .assert()
437437 .success()
438− .stderr(predicate::eq("WARNING: Ignoring PAGER=\"most -w\": Coloring not supported. Override with BAT_PAGER=\"most -w\" or --pager \"most -w\"\n").normalize())
438+ .stderr(predicate::eq("\x1b[33m[bat warning]\x1b[0m: Ignoring PAGER=\"most -w\": Coloring not supported. Override with BAT_PAGER=\"most -w\" or --pager \"most -w\"\n").normalize())
439439 .stdout(predicate::eq("hello world\n").normalize());
440440}
441441