2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-15 00:12:57 +00:00
bat/src/output.rs

148 lines
5.1 KiB
Rust
Raw Normal View History

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")]
use crate::less::retrieve_less_version;
2020-04-22 20:54:33 +00:00
#[cfg(feature = "paging")]
use crate::paging::PagingMode;
2018-08-22 20:29:12 +00:00
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")]
2018-10-21 10:52:29 +00:00
pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
2020-03-30 20:18:41 +00:00
use self::PagingMode::*;
2018-10-17 18:21:58 +00:00
Ok(match mode {
2020-03-30 20:18:41 +00:00
Always => OutputType::try_pager(false, pager)?,
QuitIfOneScreen => OutputType::try_pager(true, 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")]
2018-10-21 10:52:29 +00:00
fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
2020-03-30 17:36:26 +00:00
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::{Command, Stdio};
let mut replace_arguments_to_less = false;
let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
(Ok(bat_pager), _) => Some(bat_pager),
(_, Ok(pager)) => {
// 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.
replace_arguments_to_less = true;
Some(pager)
}
_ => None,
};
let pager_from_config = pager_from_config.map(|p| p.to_string());
if pager_from_config.is_some() {
replace_arguments_to_less = false;
}
2018-11-05 20:41:51 +00:00
let pager = pager_from_config
.or(pager_from_env)
2019-03-08 10:46:49 +00:00
.unwrap_or_else(|| String::from("less"));
let pagerflags =
shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
match pagerflags.split_first() {
Some((pager_name, args)) => {
let mut pager_path = PathBuf::from(pager_name);
if pager_path.file_stem() == Some(&OsString::from("bat")) {
pager_path = PathBuf::from("less");
}
2018-11-05 20:41:51 +00:00
let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
let mut process = if is_less {
let mut p = Command::new(&pager_path);
if args.is_empty() || replace_arguments_to_less {
p.arg("--RAW-CONTROL-CHARS");
if quit_if_one_screen {
p.arg("--quit-if-one-screen");
}
// 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");
}
_ => {}
}
} else {
p.args(args);
}
p.env("LESSCHARSET", "UTF-8");
p
} else {
let mut p = Command::new(&pager_path);
p.args(args);
p
};
Ok(process
.stdin(Stdio::piped())
.spawn()
.map(OutputType::Pager)
.unwrap_or_else(|_| OutputType::stdout()))
}
None => Ok(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())
}
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();
}
}
}