on master help sign in
Simplify and polish pager.rs and related code
dcfe883Martin Nordholts
CHANGELOG.md+1−1
@@ -10,7 +10,7 @@
10- Only print themes hint in interactive mode (`bat --list-themes`), see #1439 (@rsteube)
10- Only print themes hint in interactive mode (`bat --list-themes`), see #1439 (@rsteube)
11- Make ./tests/syntax-tests/regression_test.sh work on recent versions of macOS, see #1443 (@Enselic)
11- Make ./tests/syntax-tests/regression_test.sh work on recent versions of macOS, see #1443 (@Enselic)
12- VimL syntax highlighting fix, see #1450 (@esensar)
12- 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)
14
14
15## Other
15## Other
16
16
src/output.rs+2−1
@@ -52,6 +52,7 @@ impl OutputType {
52 use std::path::PathBuf;
52 use std::path::PathBuf;
53 use std::process::{Command, Stdio};
53 use std::process::{Command, Stdio};
54 use crate::pager::*;
54 use crate::pager::*;
55+ use crate::bat_warning;
55
56
56 let Pager { pager, source } = get_pager(pager_from_config);
57 let Pager { pager, source } = get_pager(pager_from_config);
57
58
@@ -67,7 +68,7 @@ impl OutputType {
67 }
68 }
68
69
69 if pager_path.file_stem() == Some(&OsString::from("most")) && source == PagerSource::PagerEnvVar {
70 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);
71 return Ok(OutputType::stdout());
72 return Ok(OutputType::stdout());
72 }
73 }
73
74
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.
1#[derive(Debug, PartialEq)]
2#[derive(Debug, PartialEq)]
2pub enum PagerSource {
3pub enum PagerSource {
4+ /// From --config
5+ Config,
6+
3 /// From the env var BAT_PAGER
7 /// From the env var BAT_PAGER
4 BatPagerEnvVar,
8 BatPagerEnvVar,
5
9
6 /// From the env var PAGER
10 /// From the env var PAGER
7 PagerEnvVar,
11 PagerEnvVar,
8
12
9− /// From --config
10− Config,
11−
12 /// No pager was specified, default is used
13 /// No pager was specified, default is used
13 Default,
14 Default,
14}
15}
15
16
17+/// A pager such as 'less', and from where we got it.
16pub struct Pager {
18pub struct Pager {
17 pub pager: String,
19 pub pager: String,
18 pub source: PagerSource,
20 pub source: PagerSource,
19}
21}
20
22
21impl Pager {
23impl Pager {
22− fn new(
23− pager: &str,
24− source: PagerSource
24+ fn new(pager: &str, source: PagerSource) -> Pager {
25− ) -> Pager {
26 Pager {
25 Pager {
27 pager: String::from(pager),
26 pager: String::from(pager),
28 source,
27 source,
@@ -30,16 +29,16 @@ impl Pager {
30 }
29 }
31}
30}
32
31
33−pub fn get_pager(
32+/// Returns what pager to use, after looking at both config and environment variables.
34− pager_from_config: Option<&str>,
33+pub fn get_pager(pager_from_config: Option<&str>) -> Pager {
35−) -> Pager {
34+ match (
36− if pager_from_config.is_some() {
35+ pager_from_config,
37− return Pager::new(pager_from_config.unwrap(), PagerSource::Config);
38− } else {
39− return match (std::env::var("BAT_PAGER"), std::env::var("PAGER")) {
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),
40+ (_, Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
41− (_, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
41+ (_, _, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
42− _ => Pager::new("less", PagerSource::Default),
42+ _ => Pager::new("less", PagerSource::Default),
43− };
44 }
43 }
45}
44}
tests/integration_tests.rs+2−2
@@ -423,7 +423,7 @@ fn pager_most() {
423 .arg("test.txt")
423 .arg("test.txt")
424 .assert()
424 .assert()
425 .success()
425 .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())
427 .stdout(predicate::eq("hello world\n").normalize());
427 .stdout(predicate::eq("hello world\n").normalize());
428}
428}
429
429
@@ -435,7 +435,7 @@ fn pager_most_with_arg() {
435 .arg("test.txt")
435 .arg("test.txt")
436 .assert()
436 .assert()
437 .success()
437 .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())
439 .stdout(predicate::eq("hello world\n").normalize());
439 .stdout(predicate::eq("hello world\n").normalize());
440}
440}
441
441