on master help sign in
Replace less command line arguments only for PAGER
commit6e8fca5bb258a8ff8e59a94e2d65904c03f19cac authorsharkdp <davidpeter@web.de> dateThu 7 Feb 2019, 22:12 (8 years ago) parent63c7738
1 file changed+26−32
src/output.rs+26−32
src/output.rs+26−32
@@ -26,44 +26,60 @@ impl OutputType {
2626
2727 /// Try to launch the pager. Fall back to stdout in case of errors.
2828 fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
29− let pager_from_env =
30− env::var("BAT_PAGER").or_else(|_| env::var("PAGER").map(add_default_flags_to_less));
29+ let mut replace_arguments_to_less = false;
30+
31+ let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
32+ (Ok(bat_pager), _) => Some(bat_pager),
33+ (_, Ok(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'.
37+ //
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;
41+ Some(pager)
42+ }
43+ _ => None,
44+ };
3145
3246 let pager = pager_from_config
3347 .map(|p| p.to_string())
34− .or(pager_from_env.ok())
48+ .or(pager_from_env)
3549 .unwrap_or(String::from("less"));
3650
37− let pagerflags = shell_words::split(&pager)
38− .chain_err(|| "Could not parse (BAT_)PAGER environment variable.")?;
51+ let pagerflags =
52+ shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
3953
4054 match pagerflags.split_first() {
41− Some((pager_name, mut args)) => {
55+ Some((pager_name, args)) => {
4256 let mut pager_path = PathBuf::from(pager_name);
4357
4458 if pager_path.file_stem() == Some(&OsString::from("bat")) {
4559 pager_path = PathBuf::from("less");
46− args = &[];
4760 }
4861
4962 let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
5063
5164 let mut process = if is_less {
5265 let mut p = Command::new(&pager_path);
53− if args.is_empty() {
66+ if args.is_empty() || replace_arguments_to_less {
5467 p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]);
5568 if quit_if_one_screen {
5669 p.arg("--quit-if-one-screen");
5770 }
71+ } else {
72+ p.args(args);
5873 }
5974 p.env("LESSCHARSET", "UTF-8");
6075 p
6176 } else {
62− Command::new(&pager_path)
77+ let mut p = Command::new(&pager_path);
78+ p.args(args);
79+ p
6380 };
6481
6582 Ok(process
66− .args(args)
6783 .stdin(Stdio::piped())
6884 .spawn()
6985 .map(OutputType::Pager)
@@ -88,28 +104,6 @@ impl OutputType {
88104 }
89105}
90106
91−// Since someone could set PAGER to less without arg -R that bat needs,
92−// ignore flags for less and change flags to -FRX.
93−// If a user wants an env variable with flags unchanged, he/she should use
94−// BAT_PAGER.
95−fn add_default_flags_to_less(pager: String) -> String {
96− if let Some(pager_name) = shell_words::split(&pager)
97− .ok()
98− .as_ref()
99− .and_then(|flags| flags.first())
100− .and_then(|pager_path| {
101− let path_buf = PathBuf::from(pager_path);
102− path_buf.file_stem().map(|os_str| os_str.to_os_string())
103− })
104− {
105− if pager_name == OsString::from("less") {
106− return "less -FRX".to_string();
107− }
108− }
109−
110− pager.to_string()
111−}
112−
113107impl Drop for OutputType {
114108 fn drop(&mut self) {
115109 if let OutputType::Pager(ref mut command) = *self {