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