2020-12-28 22:29:03 +01:00
|
|
|
/// If we use a pager, this enum tells us from where we were told to use it.
|
2020-11-26 22:46:24 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum PagerSource {
|
2020-12-28 22:29:03 +01:00
|
|
|
/// From --config
|
|
|
|
Config,
|
|
|
|
|
2020-11-26 22:46:24 +01:00
|
|
|
/// From the env var BAT_PAGER
|
|
|
|
BatPagerEnvVar,
|
|
|
|
|
|
|
|
/// From the env var PAGER
|
|
|
|
PagerEnvVar,
|
|
|
|
|
|
|
|
/// No pager was specified, default is used
|
|
|
|
Default,
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:29:03 +01:00
|
|
|
/// A pager such as 'less', and from where we got it.
|
2020-11-26 22:46:24 +01:00
|
|
|
pub struct Pager {
|
|
|
|
pub pager: String,
|
|
|
|
pub source: PagerSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pager {
|
2020-12-28 22:29:03 +01:00
|
|
|
fn new(pager: &str, source: PagerSource) -> Pager {
|
2020-11-26 22:46:24 +01:00
|
|
|
Pager {
|
|
|
|
pager: String::from(pager),
|
|
|
|
source,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:29:03 +01:00
|
|
|
/// Returns what pager to use, after looking at both config and environment variables.
|
|
|
|
pub fn get_pager(pager_from_config: Option<&str>) -> Pager {
|
|
|
|
match (
|
|
|
|
pager_from_config,
|
|
|
|
std::env::var("BAT_PAGER"),
|
|
|
|
std::env::var("PAGER"),
|
|
|
|
) {
|
|
|
|
(Some(config), _, _) => Pager::new(config, PagerSource::Config),
|
|
|
|
(_, Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
|
|
|
|
(_, _, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
|
|
|
|
_ => Pager::new("less", PagerSource::Default),
|
2020-11-26 22:46:24 +01:00
|
|
|
}
|
|
|
|
}
|