Add --pager option

This commit is contained in:
Park Juhyung 2018-10-21 19:52:29 +09:00 committed by David Peter
parent b22a9f8fe3
commit 154186a58d
4 changed files with 29 additions and 10 deletions

View File

@ -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"),
})
}

View File

@ -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")

View File

@ -19,7 +19,7 @@ impl<'b> Controller<'b> {
}
pub fn run(&self) -> Result<bool> {
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;

View File

@ -15,19 +15,21 @@ pub enum OutputType {
}
impl OutputType {
pub fn from_mode(mode: PagingMode) -> Result<Self> {
pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
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<Self> {
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<Self> {
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)