1
0
mirror of https://github.com/sharkdp/bat synced 2026-07-27 17:41:42 +00:00

Simplify and polish pager.rs and related code

This commit is contained in:
Martin Nordholts
2020-12-28 22:29:03 +01:00
parent cc0f8ca813
commit dcfe883f4b
4 changed files with 22 additions and 22 deletions
+17 -18
View File
@@ -1,28 +1,27 @@
/// If we use a pager, this enum tells us from where we were told to use it.
#[derive(Debug, PartialEq)]
pub enum PagerSource {
/// From --config
Config,
/// From the env var BAT_PAGER
BatPagerEnvVar,
/// From the env var PAGER
PagerEnvVar,
/// From --config
Config,
/// No pager was specified, default is used
Default,
}
/// A pager such as 'less', and from where we got it.
pub struct Pager {
pub pager: String,
pub source: PagerSource,
}
impl Pager {
fn new(
pager: &str,
source: PagerSource
) -> Pager {
fn new(pager: &str, source: PagerSource) -> Pager {
Pager {
pager: String::from(pager),
source,
@@ -30,16 +29,16 @@ impl Pager {
}
}
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),
};
/// 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),
}
}