2018-05-21 12:59:42 +00:00
|
|
|
use std::io::{self, Write};
|
2020-03-30 17:36:26 +00:00
|
|
|
#[cfg(feature = "paging")]
|
|
|
|
use std::process::Child;
|
2018-05-21 12:59:42 +00:00
|
|
|
|
2020-04-22 19:45:47 +00:00
|
|
|
use crate::error::*;
|
2020-03-30 17:36:26 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2019-12-23 08:54:18 +00:00
|
|
|
use crate::less::retrieve_less_version;
|
2020-04-22 20:54:33 +00:00
|
|
|
#[cfg(feature = "paging")]
|
|
|
|
use crate::paging::PagingMode;
|
2020-10-07 15:28:25 +00:00
|
|
|
#[cfg(feature = "paging")]
|
|
|
|
use crate::wrapping::WrappingMode;
|
2018-08-22 20:29:12 +00:00
|
|
|
|
2020-10-07 17:50:44 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2020-10-07 22:14:33 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-10-07 17:50:44 +00:00
|
|
|
enum SingleScreenAction {
|
|
|
|
Quit,
|
|
|
|
Nothing,
|
|
|
|
}
|
|
|
|
|
2019-10-15 01:25:53 +00:00
|
|
|
#[derive(Debug)]
|
2018-05-21 12:59:42 +00:00
|
|
|
pub enum OutputType {
|
2020-03-30 17:36:26 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2018-05-21 12:59:42 +00:00
|
|
|
Pager(Child),
|
|
|
|
Stdout(io::Stdout),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OutputType {
|
2020-03-30 20:18:41 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2020-10-11 19:57:12 +00:00
|
|
|
pub fn from_mode(
|
|
|
|
paging_mode: PagingMode,
|
|
|
|
wrapping_mode: WrappingMode,
|
|
|
|
pager: Option<&str>,
|
|
|
|
) -> Result<Self> {
|
2020-03-30 20:18:41 +00:00
|
|
|
use self::PagingMode::*;
|
2020-10-07 15:28:25 +00:00
|
|
|
Ok(match paging_mode {
|
2020-10-07 17:50:44 +00:00
|
|
|
Always => OutputType::try_pager(SingleScreenAction::Nothing, wrapping_mode, pager)?,
|
2020-10-11 19:57:12 +00:00
|
|
|
QuitIfOneScreen => {
|
|
|
|
OutputType::try_pager(SingleScreenAction::Quit, wrapping_mode, pager)?
|
|
|
|
}
|
2018-05-21 12:59:42 +00:00
|
|
|
_ => OutputType::stdout(),
|
2018-10-17 18:21:58 +00:00
|
|
|
})
|
2018-05-21 12:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to launch the pager. Fall back to stdout in case of errors.
|
2020-03-30 17:36:26 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2020-10-07 15:28:25 +00:00
|
|
|
fn try_pager(
|
2020-10-07 17:50:44 +00:00
|
|
|
single_screen_action: SingleScreenAction,
|
|
|
|
wrapping_mode: WrappingMode,
|
2020-10-11 19:57:12 +00:00
|
|
|
pager_from_config: Option<&str>,
|
2020-10-07 15:28:25 +00:00
|
|
|
) -> Result<Self> {
|
2020-12-30 12:25:23 +00:00
|
|
|
use crate::pager::{self, PagerKind, PagerSource};
|
2020-03-30 17:36:26 +00:00
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
2020-12-30 12:25:23 +00:00
|
|
|
let pager_opt =
|
|
|
|
pager::get_pager(pager_from_config).chain_err(|| "Could not parse pager command.")?;
|
2018-07-23 20:49:25 +00:00
|
|
|
|
2020-12-30 12:25:23 +00:00
|
|
|
let pager = match pager_opt {
|
|
|
|
Some(pager) => pager,
|
|
|
|
None => return Ok(OutputType::stdout()),
|
|
|
|
};
|
2018-10-12 15:37:57 +00:00
|
|
|
|
2020-12-30 12:25:23 +00:00
|
|
|
if pager.kind == PagerKind::Bat {
|
|
|
|
return Err(ErrorKind::InvalidPagerValueBat.into());
|
|
|
|
}
|
2018-11-06 18:53:32 +00:00
|
|
|
|
2020-12-30 12:25:23 +00:00
|
|
|
let mut p = Command::new(pager.bin);
|
|
|
|
let args = pager.args;
|
|
|
|
|
|
|
|
if pager.kind == PagerKind::Less {
|
|
|
|
// less needs to be called with the '-R' option in order to properly interpret the
|
|
|
|
// ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
|
|
|
|
// therefore need to overwrite the arguments and add '-R'.
|
|
|
|
//
|
|
|
|
// We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
|
|
|
|
// or bats '--pager' command line option.
|
|
|
|
let replace_arguments_to_less = pager.source == PagerSource::PagerEnvVar;
|
|
|
|
|
|
|
|
if args.is_empty() || replace_arguments_to_less {
|
|
|
|
p.arg("--RAW-CONTROL-CHARS");
|
|
|
|
if single_screen_action == SingleScreenAction::Quit {
|
|
|
|
p.arg("--quit-if-one-screen");
|
2018-11-05 20:54:49 +00:00
|
|
|
}
|
2018-11-05 20:41:51 +00:00
|
|
|
|
2020-12-30 12:25:23 +00:00
|
|
|
if wrapping_mode == WrappingMode::NoWrapping {
|
|
|
|
p.arg("--chop-long-lines");
|
2020-11-27 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 12:25:23 +00:00
|
|
|
// Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
|
|
|
|
// versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
|
|
|
|
//
|
|
|
|
// See: http://www.greenwoodsoftware.com/less/news.530.html
|
|
|
|
//
|
|
|
|
// For newer versions (530 or 558 on Windows), we omit '--no-init' as it
|
|
|
|
// is not needed anymore.
|
|
|
|
match retrieve_less_version() {
|
|
|
|
None => {
|
|
|
|
p.arg("--no-init");
|
|
|
|
}
|
|
|
|
Some(version) if (version < 530 || (cfg!(windows) && version < 558)) => {
|
|
|
|
p.arg("--no-init");
|
2018-10-17 18:31:22 +00:00
|
|
|
}
|
2020-12-30 12:25:23 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.args(args);
|
2018-10-17 18:31:22 +00:00
|
|
|
}
|
2020-12-30 12:25:23 +00:00
|
|
|
p.env("LESSCHARSET", "UTF-8");
|
|
|
|
} else {
|
|
|
|
p.args(args);
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(p.stdin(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.map(OutputType::Pager)
|
|
|
|
.unwrap_or_else(|_| OutputType::stdout()))
|
2018-05-21 12:59:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 20:18:41 +00:00
|
|
|
pub(crate) fn stdout() -> Self {
|
2018-05-21 12:59:42 +00:00
|
|
|
OutputType::Stdout(io::stdout())
|
|
|
|
}
|
|
|
|
|
2020-04-25 10:25:25 +00:00
|
|
|
#[cfg(feature = "paging")]
|
|
|
|
pub(crate) fn is_pager(&self) -> bool {
|
2020-11-15 12:29:09 +00:00
|
|
|
if let OutputType::Pager(_) = self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-04-25 10:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "paging"))]
|
|
|
|
pub(crate) fn is_pager(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:14:57 +00:00
|
|
|
pub fn handle(&mut self) -> Result<&mut dyn Write> {
|
2018-05-21 12:59:42 +00:00
|
|
|
Ok(match *self {
|
2020-03-30 17:36:26 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2018-05-21 12:59:42 +00:00
|
|
|
OutputType::Pager(ref mut command) => command
|
|
|
|
.stdin
|
|
|
|
.as_mut()
|
|
|
|
.chain_err(|| "Could not open stdin for pager")?,
|
|
|
|
OutputType::Stdout(ref mut handle) => handle,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 17:36:26 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2018-05-21 12:59:42 +00:00
|
|
|
impl Drop for OutputType {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let OutputType::Pager(ref mut command) = *self {
|
|
|
|
let _ = command.wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|