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
This commit is contained in:
sharkdp 2019-12-23 09:54:18 +01:00 committed by David Peter
parent 126729f87a
commit 67fe804256
3 changed files with 63 additions and 1 deletions

49
src/less.rs Normal file
View File

@ -0,0 +1,49 @@
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_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));
}

View File

@ -24,6 +24,7 @@ mod decorations;
mod diff; mod diff;
pub mod dirs; pub mod dirs;
pub mod inputfile; pub mod inputfile;
mod less;
pub mod line_range; pub mod line_range;
mod output; mod output;
mod preprocessor; mod preprocessor;

View File

@ -7,6 +7,7 @@ use std::process::{Child, Command, Stdio};
use shell_words; use shell_words;
use crate::errors::*; use crate::errors::*;
use crate::less::retrieve_less_version;
use crate::PagingMode; use crate::PagingMode;
#[derive(Debug)] #[derive(Debug)]
@ -70,10 +71,21 @@ impl OutputType {
let mut process = if is_less { let mut process = if is_less {
let mut p = Command::new(&pager_path); let mut p = Command::new(&pager_path);
if args.is_empty() || replace_arguments_to_less { if args.is_empty() || replace_arguments_to_less {
p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]); p.arg("--RAW-CONTROL-CHARS");
if quit_if_one_screen { if quit_if_one_screen {
p.arg("--quit-if-one-screen"); p.arg("--quit-if-one-screen");
} }
// 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
match retrieve_less_version() {
Some(version) if version < 530 => {
p.arg("--no-init");
}
_ => {}
}
} else { } else {
p.args(args); p.args(args);
} }