@@ -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}