mirror of
https://github.com/sharkdp/bat.git
synced 2024-12-22 06:20:44 +01:00
Add --pager option
This commit is contained in:
parent
b22a9f8fe3
commit
154186a58d
@ -70,6 +70,9 @@ pub struct Config<'a> {
|
|||||||
|
|
||||||
/// File extension/name mappings
|
/// File extension/name mappings
|
||||||
pub syntax_mapping: SyntaxMapping,
|
pub syntax_mapping: SyntaxMapping,
|
||||||
|
|
||||||
|
/// Pager option
|
||||||
|
pub pager: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_truecolor_terminal() -> bool {
|
fn is_truecolor_terminal() -> bool {
|
||||||
@ -228,6 +231,7 @@ impl App {
|
|||||||
),
|
),
|
||||||
output_components,
|
output_components,
|
||||||
syntax_mapping,
|
syntax_mapping,
|
||||||
|
pager: self.matches.value_of("pager"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,12 +204,25 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
|
|||||||
.long_help(
|
.long_help(
|
||||||
"Specify when to use the pager. To control which pager \
|
"Specify when to use the pager. To control which pager \
|
||||||
is used, set the PAGER or BAT_PAGER environment \
|
is used, set the PAGER or BAT_PAGER environment \
|
||||||
variables (the latter takes precedence). The default \
|
variables (the latter takes precedence) or set --pager option.\
|
||||||
pager is 'less'. To disable the pager permanently, set \
|
The default pager is 'less'. To disable the pager permanently,\
|
||||||
BAT_PAGER to an empty string. \
|
set BAT_PAGER to an empty string. \
|
||||||
Possible values: *auto*, never, always.",
|
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(
|
||||||
Arg::with_name("wrap")
|
Arg::with_name("wrap")
|
||||||
.long("wrap")
|
.long("wrap")
|
||||||
|
@ -19,7 +19,7 @@ impl<'b> Controller<'b> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&self) -> Result<bool> {
|
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 writer = output_type.handle()?;
|
||||||
let mut no_errors: bool = true;
|
let mut no_errors: bool = true;
|
||||||
|
|
||||||
|
@ -15,19 +15,21 @@ pub enum OutputType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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::*;
|
use self::PagingMode::*;
|
||||||
Ok(match mode {
|
Ok(match mode {
|
||||||
Always => OutputType::try_pager(false)?,
|
Always => OutputType::try_pager(false, pager)?,
|
||||||
QuitIfOneScreen => OutputType::try_pager(true)?,
|
QuitIfOneScreen => OutputType::try_pager(true, pager)?,
|
||||||
_ => OutputType::stdout(),
|
_ => OutputType::stdout(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to launch the pager. Fall back to stdout in case of errors.
|
/// Try to launch the pager. Fall back to stdout in case of errors.
|
||||||
fn try_pager(quit_if_one_screen: bool) -> Result<Self> {
|
fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
|
||||||
let pager = env::var("BAT_PAGER")
|
let pager_from_env = env::var("BAT_PAGER")
|
||||||
.or_else(|_| env::var("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"));
|
.unwrap_or(String::from("less"));
|
||||||
|
|
||||||
let pagerflags = shell_words::split(&pager)
|
let pagerflags = shell_words::split(&pager)
|
||||||
|
Loading…
Reference in New Issue
Block a user