on master help sign in
Add --pager option
154186aPark Juhyung
src/app.rs+4
@@ -70,6 +70,9 @@ pub struct Config<'a> {
70
70
71 /// File extension/name mappings
71 /// File extension/name mappings
72 pub syntax_mapping: SyntaxMapping,
72 pub syntax_mapping: SyntaxMapping,
73+
74+ /// Pager option
75+ pub pager: Option<&'a str>,
73}
76}
74
77
75fn is_truecolor_terminal() -> bool {
78fn is_truecolor_terminal() -> bool {
@@ -228,6 +231,7 @@ impl App {
228 ),
231 ),
229 output_components,
232 output_components,
230 syntax_mapping,
233 syntax_mapping,
234+ pager: self.matches.value_of("pager"),
231 })
235 })
232 }
236 }
233
237
src/clap_app.rs+16−3
@@ -204,12 +204,25 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
204 .long_help(
204 .long_help(
205 "Specify when to use the pager. To control which pager \
205 "Specify when to use the pager. To control which pager \
206 is used, set the PAGER or BAT_PAGER environment \
206 is used, set the PAGER or BAT_PAGER environment \
207− variables (the latter takes precedence). The default \
207+ variables (the latter takes precedence) or set --pager option.\
208− pager is 'less'. To disable the pager permanently, set \
208+ The default pager is 'less'. To disable the pager permanently,\
209− BAT_PAGER to an empty string. \
209+ set BAT_PAGER to an empty string. \
210 Possible values: *auto*, never, always.",
210 Possible values: *auto*, never, always.",
211 ),
211 ),
212 )
212 )
213+ .arg(
214+ Arg::with_name("pager")
215+ .long("pager")
216+ .overrides_with("pager")
217+ .takes_value(true)
218+ .value_name("pager-command")
219+ .hidden_short_help(true)
220+ .help("Set pager")
221+ .long_help(
222+ "Set which pager is used. This option will overwrite \
223+ PAGER or BAT_PAGER environment variables.",
224+ ),
225+ )
213 .arg(
226 .arg(
214 Arg::with_name("wrap")
227 Arg::with_name("wrap")
215 .long("wrap")
228 .long("wrap")
src/controller.rs+1−1
@@ -19,7 +19,7 @@ impl<'b> Controller<'b> {
19 }
19 }
20
20
21 pub fn run(&self) -> Result<bool> {
21 pub fn run(&self) -> Result<bool> {
22− let mut output_type = OutputType::from_mode(self.config.paging_mode)?;
22+ let mut output_type = OutputType::from_mode(self.config.paging_mode, self.config.pager)?;
23 let writer = output_type.handle()?;
23 let writer = output_type.handle()?;
24 let mut no_errors: bool = true;
24 let mut no_errors: bool = true;
25
25
src/output.rs+8−6
@@ -15,19 +15,21 @@ pub enum OutputType {
15}
15}
16
16
17impl OutputType {
17impl OutputType {
18− pub fn from_mode(mode: PagingMode) -> Result<Self> {
18+ pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
19 use self::PagingMode::*;
19 use self::PagingMode::*;
20 Ok(match mode {
20 Ok(match mode {
21− Always => OutputType::try_pager(false)?,
21+ Always => OutputType::try_pager(false, pager)?,
22− QuitIfOneScreen => OutputType::try_pager(true)?,
22+ QuitIfOneScreen => OutputType::try_pager(true, pager)?,
23 _ => OutputType::stdout(),
23 _ => OutputType::stdout(),
24 })
24 })
25 }
25 }
26
26
27 /// Try to launch the pager. Fall back to stdout in case of errors.
27 /// Try to launch the pager. Fall back to stdout in case of errors.
28− fn try_pager(quit_if_one_screen: bool) -> Result<Self> {
28+ fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
29− let pager = env::var("BAT_PAGER")
29+ let pager_from_env = env::var("BAT_PAGER")
30− .or_else(|_| env::var("PAGER"))
30+ .or_else(|_| env::var("PAGER"));
31+ let pager = pager_from_config.map(|p| p.to_string())
32+ .or(pager_from_env.ok())
31 .unwrap_or(String::from("less"));
33 .unwrap_or(String::from("less"));
32
34
33 let pagerflags = shell_words::split(&pager)
35 let pagerflags = shell_words::split(&pager)