on master help sign in
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.

commitf4202361b4726bfceea429b5840c604622932807 authorMartin Nordholts <enselic@gmail.com> dateThu 26 Nov 2020, 22:46 (6 years ago) parent6d98149
3 files changed+57−27
src/lib.rs+2 src/output.rs+10−27 src/pager.rs+45
src/lib.rs+2
@@ -31,6 +31,8 @@ mod less;
3131pub mod line_range;
3232mod output;
3333#[cfg(feature = "paging")]
34+mod pager;
35+#[cfg(feature = "paging")]
3436pub(crate) mod paging;
3537mod preprocessor;
3638mod pretty_printer;
src/output.rs+10−27
@@ -48,37 +48,12 @@ impl OutputType {
4848 wrapping_mode: WrappingMode,
4949 pager_from_config: Option<&str>,
5050 ) -> Result<Self> {
51− use std::env;
5251 use std::ffi::OsString;
5352 use std::path::PathBuf;
5453 use std::process::{Command, Stdio};
54+ use crate::pager::*;
5555
56− let mut replace_arguments_to_less = false;
57−
58− let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
59− (Ok(bat_pager), _) => Some(bat_pager),
60− (_, Ok(pager)) => {
61− // less needs to be called with the '-R' option in order to properly interpret the
62− // ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
63− // therefore need to overwrite the arguments and add '-R'.
64− //
65− // We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
66− // or bats '--pager' command line option.
67− replace_arguments_to_less = true;
68− Some(pager)
69− }
70− _ => None,
71− };
72−
73− let pager_from_config = pager_from_config.map(|p| p.to_string());
74−
75− if pager_from_config.is_some() {
76− replace_arguments_to_less = false;
77− }
78−
79− let pager = pager_from_config
80− .or(pager_from_env)
81− .unwrap_or_else(|| String::from("less"));
56+ let Pager { pager, source } = get_pager(pager_from_config);
8257
8358 let pagerflags =
8459 shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
@@ -94,6 +69,14 @@ impl OutputType {
9469 let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
9570
9671 let mut process = if is_less {
72+ // less needs to be called with the '-R' option in order to properly interpret the
73+ // ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
74+ // therefore need to overwrite the arguments and add '-R'.
75+ //
76+ // We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
77+ // or bats '--pager' command line option.
78+ let replace_arguments_to_less = source == PagerSource::PagerEnvVar;
79+
9780 let mut p = Command::new(&pager_path);
9881 if args.is_empty() || replace_arguments_to_less {
9982 p.arg("--RAW-CONTROL-CHARS");
src/pager.rsnew file+45
@@ -0,0 +1,45 @@
1+#[derive(Debug, PartialEq)]
2+pub enum PagerSource {
3+ /// From the env var BAT_PAGER
4+ BatPagerEnvVar,
5+
6+ /// From the env var PAGER
7+ PagerEnvVar,
8+
9+ /// From --config
10+ Config,
11+
12+ /// No pager was specified, default is used
13+ Default,
14+}
15+
16+pub struct Pager {
17+ pub pager: String,
18+ pub source: PagerSource,
19+}
20+
21+impl Pager {
22+ fn new(
23+ pager: &str,
24+ source: PagerSource
25+ ) -> Pager {
26+ Pager {
27+ pager: String::from(pager),
28+ source,
29+ }
30+ }
31+}
32+
33+pub fn get_pager(
34+ pager_from_config: Option<&str>,
35+) -> Pager {
36+ if pager_from_config.is_some() {
37+ return Pager::new(pager_from_config.unwrap(), PagerSource::Config);
38+ } else {
39+ return match (std::env::var("BAT_PAGER"), std::env::var("PAGER")) {
40+ (Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
41+ (_, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
42+ _ => Pager::new("less", PagerSource::Default),
43+ };
44+ }
45+}