1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-09-08 06:12:27 +01:00

Add Pager helper with info about where the value comes from

In preparation of fixing issue #1063.
This is a pure refactoring with no intended functional side effects.
This commit is contained in:
Martin Nordholts
2020-11-26 22:46:24 +01:00
parent 6d981498d8
commit f4202361b4
3 changed files with 57 additions and 27 deletions

45
src/pager.rs Normal file
View File

@@ -0,0 +1,45 @@
#[derive(Debug, PartialEq)]
pub enum PagerSource {
/// From the env var BAT_PAGER
BatPagerEnvVar,
/// From the env var PAGER
PagerEnvVar,
/// From --config
Config,
/// No pager was specified, default is used
Default,
}
pub struct Pager {
pub pager: String,
pub source: PagerSource,
}
impl Pager {
fn new(
pager: &str,
source: PagerSource
) -> Pager {
Pager {
pager: String::from(pager),
source,
}
}
}
pub fn get_pager(
pager_from_config: Option<&str>,
) -> Pager {
if pager_from_config.is_some() {
return Pager::new(pager_from_config.unwrap(), PagerSource::Config);
} else {
return match (std::env::var("BAT_PAGER"), std::env::var("PAGER")) {
(Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
(_, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
_ => Pager::new("less", PagerSource::Default),
};
}
}