Add Pager helper with info about where the value comes from

In preparation of fixing issue #1063.
This is a pure refactoring with no intended functional side effects.
This commit is contained in:
Martin Nordholts 2020-11-26 22:46:24 +01:00
parent 6d981498d8
commit f4202361b4
3 changed files with 57 additions and 27 deletions

View file

@ -31,6 +31,8 @@ mod less;
pub mod line_range;
mod output;
#[cfg(feature = "paging")]
mod pager;
#[cfg(feature = "paging")]
pub(crate) mod paging;
mod preprocessor;
mod pretty_printer;

View file

@ -48,37 +48,12 @@ impl OutputType {
wrapping_mode: WrappingMode,
pager_from_config: Option<&str>,
) -> Result<Self> {
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use crate::pager::*;
let mut replace_arguments_to_less = false;
let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
(Ok(bat_pager), _) => Some(bat_pager),
(_, Ok(pager)) => {
// less needs to be called with the '-R' option in order to properly interpret the
// ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
// therefore need to overwrite the arguments and add '-R'.
//
// We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
// or bats '--pager' command line option.
replace_arguments_to_less = true;
Some(pager)
}
_ => None,
};
let pager_from_config = pager_from_config.map(|p| p.to_string());
if pager_from_config.is_some() {
replace_arguments_to_less = false;
}
let pager = pager_from_config
.or(pager_from_env)
.unwrap_or_else(|| String::from("less"));
let Pager { pager, source } = get_pager(pager_from_config);
let pagerflags =
shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
@ -94,6 +69,14 @@ impl OutputType {
let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
let mut process = if is_less {
// less needs to be called with the '-R' option in order to properly interpret the
// ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
// therefore need to overwrite the arguments and add '-R'.
//
// We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
// or bats '--pager' command line option.
let replace_arguments_to_less = source == PagerSource::PagerEnvVar;
let mut p = Command::new(&pager_path);
if args.is_empty() || replace_arguments_to_less {
p.arg("--RAW-CONTROL-CHARS");

45
src/pager.rs Normal file
View file

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