mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
sort: support multiple input files
This commit is contained in:
parent
87455f998a
commit
3bc5a5f769
5 changed files with 48 additions and 20 deletions
|
@ -123,6 +123,7 @@ With no FILE, or when FILE is -, read standard input.", NAME, VERSION);
|
|||
}
|
||||
|
||||
fn exec(files: Vec<String>, settings: &Settings) {
|
||||
let mut lines = Vec::new();
|
||||
for path in &files {
|
||||
let (reader, _) = match open(path) {
|
||||
Some(x) => x,
|
||||
|
@ -130,7 +131,6 @@ fn exec(files: Vec<String>, settings: &Settings) {
|
|||
};
|
||||
|
||||
let buf_reader = BufReader::new(reader);
|
||||
let mut lines = Vec::new();
|
||||
|
||||
for line in buf_reader.lines() {
|
||||
match line {
|
||||
|
@ -140,26 +140,26 @@ fn exec(files: Vec<String>, settings: &Settings) {
|
|||
_ => break
|
||||
}
|
||||
}
|
||||
|
||||
match settings.mode {
|
||||
SortMode::Numeric => lines.sort_by(numeric_compare),
|
||||
SortMode::HumanNumeric => lines.sort_by(human_numeric_size_compare),
|
||||
SortMode::Month => lines.sort_by(month_compare),
|
||||
SortMode::Version => lines.sort_by(version_compare),
|
||||
SortMode::Default => lines.sort()
|
||||
}
|
||||
|
||||
if settings.unique {
|
||||
lines.dedup()
|
||||
}
|
||||
|
||||
let iter = lines.iter();
|
||||
if settings.reverse {
|
||||
print_sorted(iter.rev(), &settings.outfile);
|
||||
} else {
|
||||
print_sorted(iter, &settings.outfile)
|
||||
};
|
||||
}
|
||||
|
||||
match settings.mode {
|
||||
SortMode::Numeric => lines.sort_by(numeric_compare),
|
||||
SortMode::HumanNumeric => lines.sort_by(human_numeric_size_compare),
|
||||
SortMode::Month => lines.sort_by(month_compare),
|
||||
SortMode::Version => lines.sort_by(version_compare),
|
||||
SortMode::Default => lines.sort()
|
||||
}
|
||||
|
||||
if settings.unique {
|
||||
lines.dedup()
|
||||
}
|
||||
|
||||
let iter = lines.iter();
|
||||
if settings.reverse {
|
||||
print_sorted(iter.rev(), &settings.outfile);
|
||||
} else {
|
||||
print_sorted(iter, &settings.outfile)
|
||||
};
|
||||
}
|
||||
|
||||
/// Parse the beginning string into an f64, returning -inf instead of NaN on errors.
|
||||
|
|
9
tests/fixtures/sort/multiple_files.expected
vendored
Normal file
9
tests/fixtures/sort/multiple_files.expected
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
4
tests/fixtures/sort/multiple_files1.txt
vendored
Normal file
4
tests/fixtures/sort/multiple_files1.txt
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
3
|
||||
7
|
||||
2
|
||||
5
|
5
tests/fixtures/sort/multiple_files2.txt
vendored
Normal file
5
tests/fixtures/sort/multiple_files2.txt
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
4
|
||||
8
|
||||
1
|
||||
9
|
||||
6
|
|
@ -52,6 +52,16 @@ fn test_version() {
|
|||
test_helper("version", "-V");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_files() {
|
||||
let (at, mut ucmd) = testing(UTIL_NAME);
|
||||
ucmd.arg("-n");
|
||||
ucmd.arg("multiple_files1.txt");
|
||||
ucmd.arg("multiple_files2.txt");
|
||||
let out = ucmd.run().stdout;
|
||||
assert_eq!(out, at.read("multiple_files.expected"));
|
||||
}
|
||||
|
||||
fn test_helper(file_name: &str, args: &str) {
|
||||
let (at, mut ucmd) = testing(UTIL_NAME);
|
||||
ucmd.arg(args);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue