From f4202361b4726bfceea429b5840c604622932807 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Thu, 26 Nov 2020 22:46:24 +0100 Subject: [PATCH] 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. --- src/lib.rs | 2 ++ src/output.rs | 37 ++++++++++--------------------------- src/pager.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 src/pager.rs diff --git a/src/lib.rs b/src/lib.rs index 6d6dd206..3b89159f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/output.rs b/src/output.rs index 689371b9..a9fe865e 100644 --- a/src/output.rs +++ b/src/output.rs @@ -48,37 +48,12 @@ impl OutputType { wrapping_mode: WrappingMode, pager_from_config: Option<&str>, ) -> Result { - 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"); diff --git a/src/pager.rs b/src/pager.rs new file mode 100644 index 00000000..62c01749 --- /dev/null +++ b/src/pager.rs @@ -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), + }; + } +}