1use std::io::{self, Write};
2#[cfg(feature = "paging")]
3use std::process::Child;
6#[cfg(feature = "paging")]
7use crate::less::retrieve_less_version;
8#[cfg(feature = "paging")]
9use crate::paging::PagingMode;
10#[cfg(feature = "paging")]
11use crate::wrapping::WrappingMode;
13#[cfg(feature = "paging")]
14#[derive(Debug, PartialEq)]
15enum SingleScreenAction {
22 #[cfg(feature = "paging")]
28 #[cfg(feature = "paging")]
30 paging_mode: PagingMode,
31 wrapping_mode: WrappingMode,
34 use self::PagingMode::*;
35 Ok(match paging_mode {
36 Always => OutputType::try_pager(SingleScreenAction::Nothing, wrapping_mode, pager)?,
38 OutputType::try_pager(SingleScreenAction::Quit, wrapping_mode, pager)?
40 _ => OutputType::stdout(),
44 /// Try to launch the pager. Fall back to stdout in case of errors.
45 #[cfg(feature = "paging")]
47 single_screen_action: SingleScreenAction,
48 wrapping_mode: WrappingMode,
49 pager_from_config: Option<&str>,
51 use crate::pager::{self, PagerKind, PagerSource};
52 use std::process::{Command, Stdio};
55 pager::get_pager(pager_from_config).chain_err(|| "Could not parse pager command.")?;
57 let pager = match pager_opt {
59 None => return Ok(OutputType::stdout()),
62 if pager.kind == PagerKind::Bat {
63 return Err(ErrorKind::InvalidPagerValueBat.into());
66 let mut p = Command::new(pager.bin);
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::EnvVarPager;
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");
84 if wrapping_mode == WrappingMode::NoWrapping(true) {
85 p.arg("--chop-long-lines");
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.
91 // See: http://www.greenwoodsoftware.com/less/news.530.html
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() {
99 Some(version) if (version < 530 || (cfg!(windows) && version < 558)) => {
107 p.env("LESSCHARSET", "UTF-8");
112 Ok(p.stdin(Stdio::piped())
114 .map(OutputType::Pager)
115 .unwrap_or_else(|_| OutputType::stdout()))
118 pub(crate) fn stdout() -> Self {
119 OutputType::Stdout(io::stdout())
122 #[cfg(feature = "paging")]
123 pub(crate) fn is_pager(&self) -> bool {
124 matches!(self, OutputType::Pager(_))
127 #[cfg(not(feature = "paging"))]
128 pub(crate) fn is_pager(&self) -> bool {
132 pub fn handle(&mut self) -> Result<&mut dyn Write> {
134 #[cfg(feature = "paging")]
135 OutputType::Pager(ref mut command) => command
138 .chain_err(|| "Could not open stdin for pager")?,
139 OutputType::Stdout(ref mut handle) => handle,
144#[cfg(feature = "paging")]
145impl Drop for OutputType {
147 if let OutputType::Pager(ref mut command) = *self {
148 let _ = command.wait();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list