Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/uu/more/src/more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use uucore::{display::Quotable, show};

use uucore::translate;

mod parse;

#[derive(Debug)]
enum MoreError {
IsDirectory(PathBuf),
Expand Down Expand Up @@ -152,6 +154,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
print!("\r");
println!("{panic_info}");
}));
let args = parse::preprocess_args(args);
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
let mut options = Options::from(&matches);
if let Some(files) = matches.get_many::<OsString>(options::FILES) {
Expand Down
142 changes: 142 additions & 0 deletions src/uu/more/src/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use std::ffi::OsString;

#[derive(Debug, PartialEq, Eq)]
pub enum ExtraArg {
/// `-number` (also available as `--lines`)
Lines(u16),
/// `+number` (also available as `--from-line`)
FromLine(usize),
/// `+/string` (also available as `--pattern`)
Pattern(String),
}

pub fn parse_extra_arg(src: &str) -> Option<ExtraArg> {
if let Some(rest) = src.strip_prefix("+/") {
if !rest.is_empty() {
return Some(ExtraArg::Pattern(rest.to_string()));
}
} else if let Some(rest) = src.strip_prefix('+') {
if let Ok(n) = rest.parse::<usize>() {
return Some(ExtraArg::FromLine(n));
}
} else if let Some(rest) = src.strip_prefix('-') {
if let Ok(n) = rest.parse::<u16>() {
return Some(ExtraArg::Lines(n));
}
}
None
}

fn expand_extra_arg(result: &mut Vec<OsString>, arg: ExtraArg) {
match arg {
ExtraArg::Lines(n) => {
result.push("--lines".into());
result.push(n.to_string().into());
}
ExtraArg::FromLine(n) => {
result.push("--from-line".into());
result.push(n.to_string().into());
}
ExtraArg::Pattern(p) => {
result.push("--pattern".into());
result.push(p.into());
}
}
}

pub fn preprocess_args(args: impl Iterator<Item = OsString>) -> Vec<OsString> {
let mut result = Vec::new();
for (i, arg) in args.enumerate() {
if let Some(extra) = arg.to_str().and_then(parse_extra_arg) {
if i == 0 {
result.push(uucore::util_name().into());
}
expand_extra_arg(&mut result, extra);
} else {
result.push(arg);
}
}
result
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_minus_number() {
assert_eq!(parse_extra_arg("-10"), Some(ExtraArg::Lines(10)));
assert_eq!(parse_extra_arg("-1"), Some(ExtraArg::Lines(1)));
assert_eq!(parse_extra_arg("-abc"), None);
assert_eq!(parse_extra_arg("-"), None);
}

#[test]
fn test_parse_plus_number() {
assert_eq!(parse_extra_arg("+5"), Some(ExtraArg::FromLine(5)));
assert_eq!(parse_extra_arg("+100"), Some(ExtraArg::FromLine(100)));
assert_eq!(parse_extra_arg("+abc"), None);
}

#[test]
fn test_parse_plus_pattern() {
assert_eq!(
parse_extra_arg("+/foo"),
Some(ExtraArg::Pattern("foo".into()))
);
assert_eq!(
parse_extra_arg("+/hello world"),
Some(ExtraArg::Pattern("hello world".into()))
);
assert_eq!(parse_extra_arg("+/"), None);
}

#[test]
fn test_preprocess_args() {
// Test -number
let args = ["more", "-5"];
let result = preprocess_args(args.iter().map(OsString::from));
assert_eq!(
result,
vec![
OsString::from("more"),
OsString::from("--lines"),
OsString::from("5")
]
);

// Test +number
let args = ["more", "+10"];
let result = preprocess_args(args.iter().map(OsString::from));
assert_eq!(
result,
vec![
OsString::from("more"),
OsString::from("--from-line"),
OsString::from("10")
]
);

// Test +/pattern
let args = ["more", "+/hello"];
let result = preprocess_args(args.iter().map(OsString::from));
assert_eq!(
result,
vec![
OsString::from("more"),
OsString::from("--pattern"),
OsString::from("hello")
]
);

// Test regular args unchanged
let args = ["more", "file.txt"];
let result = preprocess_args(args.iter().map(OsString::from));
assert_eq!(result, args.iter().map(OsString::from).collect::<Vec<_>>());
}
}
31 changes: 18 additions & 13 deletions tests/by-util/test_more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,29 @@ fn test_no_arg() {
#[cfg(unix)]
fn test_valid_arg() {
let args_list: Vec<&[&str]> = vec![
&["-c"],
&["--clean-print"],
&["-d"],
&["--silent"],
&["-l"],
&["--logical"],
&["-e"],
&["--exit-on-eof"],
&["-f"],
&["--no-pause"],
&["-p"],
&["--print-over"],
&["-c"],
&["--clean-print"],
&["-s"],
&["--squeeze"],
&["-u"],
&["--plain"],
&["-n", "10"],
&["--lines", "0"],
&["--number", "0"],
&["-F", "10"],
&["--from-line", "0"],
&["-P", "something"],
&["--pattern", "-1"],
&["-0"],
&["+10"],
&["+0"],
&["+/something"],
&["+/-1"],
];
for args in args_list {
test_alive(args);
Expand Down Expand Up @@ -234,11 +242,8 @@ fn test_squeeze_blank_lines() {
#[test]
#[cfg(unix)]
fn test_pattern_search() {
let (child, controller, output) = run_more_with_pty(
&["-P", "target"],
"test.txt",
"foo\nbar\nbaz\ntarget\nend\n",
);
let (child, controller, output) =
run_more_with_pty(&["+/target"], "test.txt", "foo\nbar\nbaz\ntarget\nend\n");
assert!(output.contains("target"));
assert!(!output.contains("foo"));
quit_more(&controller, child);
Expand All @@ -248,7 +253,7 @@ fn test_pattern_search() {
#[cfg(unix)]
fn test_from_line_option() {
let (child, controller, output) =
run_more_with_pty(&["-F", "2"], "test.txt", "line1\nline2\nline3\nline4\n");
run_more_with_pty(&["+2"], "test.txt", "line1\nline2\nline3\nline4\n");
assert!(output.contains("line2"));
assert!(!output.contains("line1"));
quit_more(&controller, child);
Expand Down
Loading