diff options
author | Dan Aloni <dan@kernelim.com> | 2020-05-06 12:40:03 +0300 |
---|---|---|
committer | Dan Aloni <dan@kernelim.com> | 2020-05-06 12:42:28 +0300 |
commit | 37ac0b62cfde260594be3933ca6071544503c533 (patch) | |
tree | c1398cba43292ed1aa0d190cca8d56f1de246df5 /src/bat | |
parent | efdf475205fefed022a0ede65581d17092f0c03a (diff) |
Import `less --no-init` avoidance from `bat`
Diffstat (limited to 'src/bat')
-rw-r--r-- | src/bat/less.rs | 62 | ||||
-rw-r--r-- | src/bat/mod.rs | 1 | ||||
-rw-r--r-- | src/bat/output.rs | 23 |
3 files changed, 85 insertions, 1 deletions
diff --git a/src/bat/less.rs b/src/bat/less.rs new file mode 100644 index 00000000..f7429333 --- /dev/null +++ b/src/bat/less.rs @@ -0,0 +1,62 @@ +use std::process::Command; + +pub fn retrieve_less_version() -> Option<usize> { + let cmd = Command::new("less").arg("--version").output().ok()?; + parse_less_version(&cmd.stdout) +} + +fn parse_less_version(output: &[u8]) -> Option<usize> { + if output.starts_with(b"less ") { + let version = std::str::from_utf8(&output[5..]).ok()?; + let end = version.find(' ')?; + version[..end].parse::<usize>().ok() + } else { + None + } +} + +#[test] +fn test_parse_less_version_487() { + let output = b"less 487 (GNU regular expressions) +Copyright (C) 1984-2016 Mark Nudelman + +less comes with NO WARRANTY, to the extent permitted by law. +For information about the terms of redistribution, +see the file named README in the less distribution. +Homepage: http://www.greenwoodsoftware.com/less"; + + assert_eq!(Some(487), parse_less_version(output)); +} + +#[test] +fn test_parse_less_version_529() { + let output = b"less 529 (Spencer V8 regular expressions) +Copyright (C) 1984-2017 Mark Nudelman + +less comes with NO WARRANTY, to the extent permitted by law. +For information about the terms of redistribution, +see the file named README in the less distribution. +Homepage: http://www.greenwoodsoftware.com/less"; + + assert_eq!(Some(529), parse_less_version(output)); +} + +#[test] +fn test_parse_less_version_551() { + let output = b"less 551 (PCRE regular expressions) +Copyright (C) 1984-2019 Mark Nudelman + +less comes with NO WARRANTY, to the extent permitted by law. +For information about the terms of redistribution, +see the file named README in the less distribution. +Home page: http://www.greenwoodsoftware.com/less"; + + assert_eq!(Some(551), parse_less_version(output)); +} + +#[test] +fn test_parse_less_version_wrong_program() { + let output = b"more from util-linux 2.34"; + + assert_eq!(None, parse_less_version(output)); +} diff --git a/src/bat/mod.rs b/src/bat/mod.rs index 04f6dbf7..362ba777 100644 --- a/src/bat/mod.rs +++ b/src/bat/mod.rs @@ -2,3 +2,4 @@ pub mod assets; pub mod dirs; pub mod output; pub mod terminal; +mod less; diff --git a/src/bat/output.rs b/src/bat/output.rs index 193128f9..d23f5e80 100644 --- a/src/bat/output.rs +++ b/src/bat/output.rs @@ -9,6 +9,7 @@ use std::process::{Child, Command, Stdio}; use shell_words; use crate::env; +use super::less::retrieve_less_version; #[derive(Debug, Clone, Copy, PartialEq)] #[allow(dead_code)] @@ -79,7 +80,27 @@ impl OutputType { let mut process = if is_less { let mut p = Command::new(&pager_path); if args.is_empty() || replace_arguments_to_less { - p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]); + p.args(vec!["--RAW-CONTROL-CHARS"]); + + // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older + // versions of 'less'. Unfortunately, it also breaks mouse-wheel support. + // + // See: http://www.greenwoodsoftware.com/less/news.530.html + // + // For newer versions (530 or 558 on Windows), we omit '--no-init' as it + // is not needed anymore. + match retrieve_less_version() { + None => { + p.arg("--no-init"); + } + Some(version) + if (version < 530 || (cfg!(windows) && version < 558)) => + { + p.arg("--no-init"); + } + _ => {} + } + if quit_if_one_screen { p.arg("--quit-if-one-screen"); } |