3use std::io::{self, Write};
5use std::process::{Child, Command, Stdio};
18 pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
19 use self::PagingMode::*;
21 Always => OutputType::try_pager(false, pager)?,
22 QuitIfOneScreen => OutputType::try_pager(true, pager)?,
23 _ => OutputType::stdout(),
27 /// Try to launch the pager. Fall back to stdout in case of errors.
28 fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
29 let mut replace_arguments_to_less = false;
31 let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
32 (Ok(bat_pager), _) => Some(bat_pager),
34 // less needs to be called with the '-R' option in order to properly interpret the
35 // ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
36 // therefore need to overwrite the arguments and add '-R'.
38 // We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
39 // or bats '--pager' command line option.
40 replace_arguments_to_less = true;
46 let pager = pager_from_config
47 .map(|p| p.to_string())
49 .unwrap_or(String::from("less"));
52 shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
54 match pagerflags.split_first() {
55 Some((pager_name, args)) => {
56 let mut pager_path = PathBuf::from(pager_name);
58 if pager_path.file_stem() == Some(&OsString::from("bat")) {
59 pager_path = PathBuf::from("less");
62 let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
64 let mut process = if is_less {
65 let mut p = Command::new(&pager_path);
66 if args.is_empty() || replace_arguments_to_less {
67 p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]);
68 if quit_if_one_screen {
69 p.arg("--quit-if-one-screen");
74 p.env("LESSCHARSET", "UTF-8");
77 let mut p = Command::new(&pager_path);
83 .stdin(Stdio::piped())
85 .map(OutputType::Pager)
86 .unwrap_or_else(|_| OutputType::stdout()))
88 None => Ok(OutputType::stdout()),
93 OutputType::Stdout(io::stdout())
96 pub fn handle(&mut self) -> Result<&mut Write> {
98 OutputType::Pager(ref mut command) => command
101 .chain_err(|| "Could not open stdin for pager")?,
102 OutputType::Stdout(ref mut handle) => handle,
107impl Drop for OutputType {
109 if let OutputType::Pager(ref mut command) = *self {
110 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