on master help sign in
Do not pass '--no-init' on newer less versions

With this change, we do not pass the `--no-init` option in newer versions of less (530 or higher). This fixes #749

commit67fe8042567254d21c82ecda9f116745e2438fc2 authorsharkdp <davidpeter@web.de> dateMon 23 Dec 2019, 09:54 (7 years ago) parent126729f
3 files changed+63−1
src/less.rs+49 src/lib.rs+1 src/output.rs+13−1
src/less.rsnew file+49
@@ -0,0 +1,49 @@
1+use std::process::Command;
2+
3+pub fn retrieve_less_version() -> Option<usize> {
4+ let cmd = Command::new("less").arg("--version").output().ok()?;
5+ parse_less_version(&cmd.stdout)
6+}
7+
8+fn parse_less_version(output: &[u8]) -> Option<usize> {
9+ if output.starts_with(b"less ") {
10+ let version = std::str::from_utf8(&output[5..]).ok()?;
11+ let end = version.find(' ')?;
12+ version[..end].parse::<usize>().ok()
13+ } else {
14+ None
15+ }
16+}
17+
18+#[test]
19+fn test_parse_less_version_487() {
20+ let output = b"less 487 (GNU regular expressions)
21+Copyright (C) 1984-2016 Mark Nudelman
22+
23+less comes with NO WARRANTY, to the extent permitted by law.
24+For information about the terms of redistribution,
25+see the file named README in the less distribution.
26+Homepage: http://www.greenwoodsoftware.com/less";
27+
28+ assert_eq!(Some(487), parse_less_version(output));
29+}
30+
31+#[test]
32+fn test_parse_less_version_551() {
33+ let output = b"less 551 (PCRE regular expressions)
34+Copyright (C) 1984-2019 Mark Nudelman
35+
36+less comes with NO WARRANTY, to the extent permitted by law.
37+For information about the terms of redistribution,
38+see the file named README in the less distribution.
39+Home page: http://www.greenwoodsoftware.com/less";
40+
41+ assert_eq!(Some(551), parse_less_version(output));
42+}
43+
44+#[test]
45+fn test_parse_less_version_wrong_program() {
46+ let output = b"more from util-linux 2.34";
47+
48+ assert_eq!(None, parse_less_version(output));
49+}
src/lib.rs+1
@@ -24,6 +24,7 @@ mod decorations;
2424mod diff;
2525pub mod dirs;
2626pub mod inputfile;
27+mod less;
2728pub mod line_range;
2829mod output;
2930mod preprocessor;
src/output.rs+13−1
@@ -7,6 +7,7 @@ use std::process::{Child, Command, Stdio};
77use shell_words;
88
99use crate::errors::*;
10+use crate::less::retrieve_less_version;
1011use crate::PagingMode;
1112
1213#[derive(Debug)]
@@ -70,10 +71,21 @@ impl OutputType {
7071 let mut process = if is_less {
7172 let mut p = Command::new(&pager_path);
7273 if args.is_empty() || replace_arguments_to_less {
73− p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]);
74+ p.arg("--RAW-CONTROL-CHARS");
7475 if quit_if_one_screen {
7576 p.arg("--quit-if-one-screen");
7677 }
78+
79+ // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
80+ // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
81+ //
82+ // See: http://www.greenwoodsoftware.com/less/news.530.html
83+ match retrieve_less_version() {
84+ Some(version) if version < 530 => {
85+ p.arg("--no-init");
86+ }
87+ _ => {}
88+ }
7789 } else {
7890 p.args(args);
7991 }