@@ -48,90 +48,71 @@ impl OutputType {
48 wrapping_mode: WrappingMode,
48 wrapping_mode: WrappingMode,
49 pager_from_config: Option<&str>,
49 pager_from_config: Option<&str>,
50 ) -> Result<Self> {
50 ) -> Result<Self> {
51− use std::ffi::OsString;
51+ use crate::pager::{self, PagerKind, PagerSource};
52− use std::path::PathBuf;
53 use std::process::{Command, Stdio};
52 use std::process::{Command, Stdio};
56− let Pager { pager, source } = get_pager(pager_from_config);
55+ pager::get_pager(pager_from_config).chain_err(|| "Could not parse pager command.")?;
58− let pagerflags =
57+ let pager = match pager_opt {
59− shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
58+ Some(pager) => pager,
59+ None => return Ok(OutputType::stdout()),
61− match pagerflags.split_first() {
62+ if pager.kind == PagerKind::Bat {
62− Some((pager_name, pager_args)) => {
63+ return Err(ErrorKind::InvalidPagerValueBat.into());
63− let mut pager_path = PathBuf::from(pager_name);
64+ }
64− let mut args = pager_args;
65− let empty_args = vec![];
67− if pager_path.file_stem() == Some(&OsString::from("bat")) {
66+ let mut p = Command::new(pager.bin);
68− return Err(ErrorKind::InvalidPagerValueBat.into());
67+ let args = pager.args;
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'.
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;
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");
71− if pager_path.file_stem() == Some(&OsString::from("most")) && source == PagerSource::PagerEnvVar {
84+ if wrapping_mode == WrappingMode::NoWrapping {
72− pager_path = PathBuf::from("less");
85+ p.arg("--chop-long-lines");
76− let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
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'.
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;
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");
94− if wrapping_mode == WrappingMode::NoWrapping {
95− p.arg("--chop-long-lines");
98− // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
88+ // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
99− // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
89+ // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
101− // See: http://www.greenwoodsoftware.com/less/news.530.html
91+ // See: http://www.greenwoodsoftware.com/less/news.530.html
103− // For newer versions (530 or 558 on Windows), we omit '--no-init' as it
93+ // For newer versions (530 or 558 on Windows), we omit '--no-init' as it
104− // is not needed anymore.
94+ // is not needed anymore.
105− match retrieve_less_version() {
95+ match retrieve_less_version() {
106− None => {
96+ None => {
107− p.arg("--no-init");
97+ p.arg("--no-init");
110− if (version < 530 || (cfg!(windows) && version < 558)) =>
99+ Some(version) if (version < 530 || (cfg!(windows) && version < 558)) => {
112− p.arg("--no-init");
100+ p.arg("--no-init");
119− p.env("LESSCHARSET", "UTF-8");
102+ _ => {}
121− } else {
104+ } else {
122− let mut p = Command::new(&pager_path);
123− p.args(args);
105+ p.args(args);
128− .stdin(Stdio::piped())
130− .map(OutputType::Pager)
131− .unwrap_or_else(|_| OutputType::stdout()))
107+ p.env("LESSCHARSET", "UTF-8");
112+ Ok(p.stdin(Stdio::piped())
114+ .map(OutputType::Pager)
133− None => Ok(OutputType::stdout()),
115+ .unwrap_or_else(|_| OutputType::stdout()))
137 pub(crate) fn stdout() -> Self {
118 pub(crate) fn stdout() -> Self {