Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for BusyBox less as pager #2162

Merged
merged 4 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions src/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
use std::ffi::OsStr;
use std::process::Command;

pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<usize> {
#[derive(Debug, PartialEq)]
pub enum LessVersion {
Less(usize),
BusyBox,
}

pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<LessVersion> {
let resolved_path = grep_cli::resolve_binary(less_path.as_ref()).ok()?;

let cmd = Command::new(resolved_path).arg("--version").output().ok()?;
parse_less_version(&cmd.stdout)
if cmd.status.success() {
parse_less_version(&cmd.stdout)
} else {
parse_less_version(&cmd.stderr)
}
}

fn parse_less_version(output: &[u8]) -> Option<usize> {
if !output.starts_with(b"less ") {
return None;
fn parse_less_version(output: &[u8]) -> Option<LessVersion> {
match std::str::from_utf8(output) {
Enselic marked this conversation as resolved.
Show resolved Hide resolved
Ok(version) if version.starts_with("less ") => {
let end = (&version[5..]).find(|c: char| !c.is_ascii_digit())? + 5;
Some(LessVersion::Less(version[5..end].parse::<usize>().ok()?))
}
Ok(version) if version.contains("BusyBox ") => Some(LessVersion::BusyBox),
_ => None,
}

let version = std::str::from_utf8(&output[5..]).ok()?;
let end = version.find(|c: char| !c.is_ascii_digit())?;
version[..end].parse::<usize>().ok()
}
Enselic marked this conversation as resolved.
Show resolved Hide resolved

#[test]
Expand All @@ -30,7 +41,7 @@ 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));
assert_eq!(Some(LessVersion::Less(487)), parse_less_version(output));
}

#[test]
Expand All @@ -43,7 +54,7 @@ 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));
assert_eq!(Some(LessVersion::Less(529)), parse_less_version(output));
}

#[test]
Expand All @@ -56,7 +67,7 @@ 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));
assert_eq!(Some(LessVersion::Less(551)), parse_less_version(output));
}

#[test]
Expand All @@ -69,7 +80,7 @@ For information about the terms of redistribution,
see the file named README in the less distribution.
Home page: https://greenwoodsoftware.com/less";

assert_eq!(Some(581), parse_less_version(output));
assert_eq!(Some(LessVersion::Less(581)), parse_less_version(output));
}

#[test]
Expand All @@ -78,3 +89,25 @@ fn test_parse_less_version_wrong_program() {

assert_eq!(None, parse_less_version(output));
}

Enselic marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn test_parse_less_version_busybox() {
let output = b"pkg/less: unrecognized option '--version'
BusyBox v1.35.0 (2022-04-21 10:38:11 EDT) multi-call binary.

Usage: less [-EFIMmNSRh~] [FILE]...

View FILE (or stdin) one screenful at a time

-E Quit once the end of a file is reached
-F Quit if entire file fits on first screen
-I Ignore case in all searches
-M,-m Display status line with line numbers
and percentage through the file
-N Prefix line number to each line
-S Truncate long lines
-R Remove color escape codes in input
-~ Suppress ~s displayed past EOF";

assert_eq!(Some(LessVersion::BusyBox), parse_less_version(output));
}
32 changes: 20 additions & 12 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::process::Child;

use crate::error::*;
#[cfg(feature = "paging")]
use crate::less::retrieve_less_version;
use crate::less::{retrieve_less_version, LessVersion};
#[cfg(feature = "paging")]
use crate::paging::PagingMode;
#[cfg(feature = "paging")]
Expand Down Expand Up @@ -83,31 +83,39 @@ impl OutputType {
let replace_arguments_to_less = pager.source == PagerSource::EnvVarPager;

if args.is_empty() || replace_arguments_to_less {
p.arg("--RAW-CONTROL-CHARS");
Enselic marked this conversation as resolved.
Show resolved Hide resolved
if single_screen_action == SingleScreenAction::Quit {
p.arg("--quit-if-one-screen");
}

if wrapping_mode == WrappingMode::NoWrapping(true) {
p.arg("--chop-long-lines");
}

// 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(&pager.bin) {
let less_version = retrieve_less_version(&pager.bin);
match less_version {
None => {
p.arg("--no-init");
Enselic marked this conversation as resolved.
Show resolved Hide resolved
}
Some(version) if (version < 530 || (cfg!(windows) && version < 558)) => {
Some(LessVersion::Less(version))
if (version < 530 || (cfg!(windows) && version < 558)) =>
{
p.arg("--no-init");
}
Some(LessVersion::BusyBox) => {
// This makes BusyBox less filter out color escapes
p.arg("-R");
}
_ => {}
}
if less_version != Some(LessVersion::BusyBox) {
p.arg("--RAW-CONTROL-CHARS");
if single_screen_action == SingleScreenAction::Quit {
p.arg("--quit-if-one-screen");
}

if wrapping_mode == WrappingMode::NoWrapping(true) {
p.arg("--chop-long-lines");
}
}
} else {
p.args(args);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/syntax-tests/highlighted/Rust/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@
 None => {
 p.arg("--no-init");
 }
 Some(version)
Enselic marked this conversation as resolved.
Show resolved Hide resolved
 Some(LessVersion::Less(version))
 if (version < 530 || (cfg!(windows) && version < 558)) =>
 {
 p.arg("--no-init");
 }
 Some(LessVersion::BusyBox) => {
 p.arg("-R")
 }
 _ => {}
 }
 } else {
Expand Down
5 changes: 4 additions & 1 deletion tests/syntax-tests/source/Rust/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ impl OutputType {
None => {
p.arg("--no-init");
}
Some(version)
Some(LessVersion::Less(version))
if (version < 530 || (cfg!(windows) && version < 558)) =>
{
p.arg("--no-init");
}
Some(LessVersion::BusyBox) => {
p.arg("-R")
}
_ => {}
}
} else {
Expand Down