forked from sharkdp/bat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pager helper with info about where the value comes from
In preparation of fixing issue sharkdp#1063. This is a pure refactoring with no intended functional side effects.
- Loading branch information
Showing
3 changed files
with
57 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}; | ||
} | ||
} |