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:
45
src/pager.rs
Normal file
45
src/pager.rs
Normal 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),
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user