Ignore flags from PAGER env var if the program is 'less'

pull/500/head
Park Juhyung 6 years ago committed by David Peter
parent 4df22e617f
commit 63c77383ce

@ -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<Self> {
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 {

Loading…
Cancel
Save