From 63c77383ceb4cef1d67b49025380050df74cee0f Mon Sep 17 00:00:00 2001 From: Park Juhyung Date: Thu, 20 Dec 2018 00:17:14 +0900 Subject: [PATCH] Ignore flags from PAGER env var if the program is 'less' --- src/output.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/output.rs b/src/output.rs index 26de8f21..69b34644 100644 --- a/src/output.rs +++ b/src/output.rs @@ -26,7 +26,8 @@ impl OutputType { /// Try to launch the pager. Fall back to stdout in case of errors. fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result { - let pager_from_env = env::var("BAT_PAGER").or_else(|_| env::var("PAGER")); + let pager_from_env = + env::var("BAT_PAGER").or_else(|_| env::var("PAGER").map(add_default_flags_to_less)); let pager = pager_from_config .map(|p| p.to_string()) @@ -87,6 +88,28 @@ impl OutputType { } } +// Since someone could set PAGER to less without arg -R that bat needs, +// ignore flags for less and change flags to -FRX. +// If a user wants an env variable with flags unchanged, he/she should use +// BAT_PAGER. +fn add_default_flags_to_less(pager: String) -> String { + if let Some(pager_name) = shell_words::split(&pager) + .ok() + .as_ref() + .and_then(|flags| flags.first()) + .and_then(|pager_path| { + let path_buf = PathBuf::from(pager_path); + path_buf.file_stem().map(|os_str| os_str.to_os_string()) + }) + { + if pager_name == OsString::from("less") { + return "less -FRX".to_string(); + } + } + + pager.to_string() +} + impl Drop for OutputType { fn drop(&mut self) { if let OutputType::Pager(ref mut command) = *self {