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