From 154186a58deefd4b837677a5f27000c1a3beae73 Mon Sep 17 00:00:00 2001 From: Park Juhyung Date: Sun, 21 Oct 2018 19:52:29 +0900 Subject: [PATCH] Add --pager option --- src/app.rs | 4 ++++ src/clap_app.rs | 19 ++++++++++++++++--- src/controller.rs | 2 +- src/output.rs | 14 ++++++++------ 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/app.rs b/src/app.rs index 06a0051f..126c718c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -70,6 +70,9 @@ pub struct Config<'a> { /// File extension/name mappings pub syntax_mapping: SyntaxMapping, + + /// Pager option + pub pager: Option<&'a str>, } fn is_truecolor_terminal() -> bool { @@ -228,6 +231,7 @@ impl App { ), output_components, syntax_mapping, + pager: self.matches.value_of("pager"), }) } diff --git a/src/clap_app.rs b/src/clap_app.rs index 25ed5517..7127d178 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -204,12 +204,25 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .long_help( "Specify when to use the pager. To control which pager \ is used, set the PAGER or BAT_PAGER environment \ - variables (the latter takes precedence). The default \ - pager is 'less'. To disable the pager permanently, set \ - BAT_PAGER to an empty string. \ + variables (the latter takes precedence) or set --pager option.\ + The default pager is 'less'. To disable the pager permanently,\ + set BAT_PAGER to an empty string. \ Possible values: *auto*, never, always.", ), ) + .arg( + Arg::with_name("pager") + .long("pager") + .overrides_with("pager") + .takes_value(true) + .value_name("pager-command") + .hidden_short_help(true) + .help("Set pager") + .long_help( + "Set which pager is used. This option will overwrite \ + PAGER or BAT_PAGER environment variables.", + ), + ) .arg( Arg::with_name("wrap") .long("wrap") diff --git a/src/controller.rs b/src/controller.rs index bf8cef13..371ec484 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -19,7 +19,7 @@ impl<'b> Controller<'b> { } pub fn run(&self) -> Result { - let mut output_type = OutputType::from_mode(self.config.paging_mode)?; + let mut output_type = OutputType::from_mode(self.config.paging_mode, self.config.pager)?; let writer = output_type.handle()?; let mut no_errors: bool = true; diff --git a/src/output.rs b/src/output.rs index 43d0669e..66913abf 100644 --- a/src/output.rs +++ b/src/output.rs @@ -15,19 +15,21 @@ pub enum OutputType { } impl OutputType { - pub fn from_mode(mode: PagingMode) -> Result { + pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result { use self::PagingMode::*; Ok(match mode { - Always => OutputType::try_pager(false)?, - QuitIfOneScreen => OutputType::try_pager(true)?, + Always => OutputType::try_pager(false, pager)?, + QuitIfOneScreen => OutputType::try_pager(true, pager)?, _ => OutputType::stdout(), }) } /// Try to launch the pager. Fall back to stdout in case of errors. - fn try_pager(quit_if_one_screen: bool) -> Result { - let pager = env::var("BAT_PAGER") - .or_else(|_| env::var("PAGER")) + fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result { + let pager_from_env = env::var("BAT_PAGER") + .or_else(|_| env::var("PAGER")); + let pager = pager_from_config.map(|p| p.to_string()) + .or(pager_from_env.ok()) .unwrap_or(String::from("less")); let pagerflags = shell_words::split(&pager)