@@ -48,100 +48,71 @@ impl OutputType {
4848 wrapping_mode: WrappingMode,
4949 pager_from_config: Option<&str>,
5050 ) -> Result<Self> {
51− use std::env;
52− use std::ffi::OsString;
53− use std::path::PathBuf;
51+ use crate::pager::{self, PagerKind, PagerSource};
5452 use std::process::{Command, Stdio};
5553
56− let mut replace_arguments_to_less = false;
54+ let pager_opt =
55+ pager::get_pager(pager_from_config).chain_err(|| "Could not parse pager command.")?;
5756
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,
57+ let pager = match pager_opt {
58+ Some(pager) => pager,
59+ None => return Ok(OutputType::stdout()),
7160 };
7261
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;
62+ if pager.kind == PagerKind::Bat {
63+ return Err(ErrorKind::InvalidPagerValueBat.into());
7764 }
7865
79− let pager = pager_from_config
80− .or(pager_from_env)
81− .unwrap_or_else(|| String::from("less"));
82−
83− let pagerflags =
84− shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
85−
86− match pagerflags.split_first() {
87− Some((pager_name, args)) => {
88− let pager_path = PathBuf::from(pager_name);
66+ let mut p = Command::new(pager.bin);
67+ let args = pager.args;
68+
69+ if pager.kind == PagerKind::Less {
70+ // less needs to be called with the '-R' option in order to properly interpret the
71+ // ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
72+ // therefore need to overwrite the arguments and add '-R'.
73+ //
74+ // We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
75+ // or bats '--pager' command line option.
76+ let replace_arguments_to_less = pager.source == PagerSource::EnvVarPager;
77+
78+ if args.is_empty() || replace_arguments_to_less {
79+ p.arg("--RAW-CONTROL-CHARS");
80+ if single_screen_action == SingleScreenAction::Quit {
81+ p.arg("--quit-if-one-screen");
82+ }
8983
90− if pager_path.file_stem() == Some(&OsString::from("bat")) {
91− return Err(ErrorKind::InvalidPagerValueBat.into());
84+ if wrapping_mode == WrappingMode::NoWrapping(true) {
85+ p.arg("--chop-long-lines");
9286 }
9387
94− let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
95−
96− let mut process = if is_less {
97− let mut p = Command::new(&pager_path);
98− if args.is_empty() || replace_arguments_to_less {
99− p.arg("--RAW-CONTROL-CHARS");
100− if single_screen_action == SingleScreenAction::Quit {
101− p.arg("--quit-if-one-screen");
102− }
103−
104− if wrapping_mode == WrappingMode::NoWrapping(true) {
105− p.arg("--chop-long-lines");
106− }
107−
108− // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
109− // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
110− //
111− // See: http://www.greenwoodsoftware.com/less/news.530.html
112− //
113− // For newer versions (530 or 558 on Windows), we omit '--no-init' as it
114− // is not needed anymore.
115− match retrieve_less_version() {
116− None => {
117− p.arg("--no-init");
118− }
119− Some(version)
120− if (version < 530 || (cfg!(windows) && version < 558)) =>
121− {
122− p.arg("--no-init");
123− }
124− _ => {}
125− }
126− } else {
127− p.args(args);
88+ // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
89+ // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
90+ //
91+ // See: http://www.greenwoodsoftware.com/less/news.530.html
92+ //
93+ // For newer versions (530 or 558 on Windows), we omit '--no-init' as it
94+ // is not needed anymore.
95+ match retrieve_less_version() {
96+ None => {
97+ p.arg("--no-init");
98+ }
99+ Some(version) if (version < 530 || (cfg!(windows) && version < 558)) => {
100+ p.arg("--no-init");
128101 }
129− p.env("LESSCHARSET", "UTF-8");
130− p
131− } else {
132− let mut p = Command::new(&pager_path);
133− p.args(args);
134− p
135− };
136−
137− Ok(process
138− .stdin(Stdio::piped())
139− .spawn()
140− .map(OutputType::Pager)
141− .unwrap_or_else(|_| OutputType::stdout()))
102+ _ => {}
103+ }
104+ } else {
105+ p.args(args);
142106 }
143− None => Ok(OutputType::stdout()),
144− }
107+ p.env("LESSCHARSET", "UTF-8");
108+ } else {
109+ p.args(args);
110+ };
111+
112+ Ok(p.stdin(Stdio::piped())
113+ .spawn()
114+ .map(OutputType::Pager)
115+ .unwrap_or_else(|_| OutputType::stdout()))
145116 }
146117
147118 pub(crate) fn stdout() -> Self {