mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
comm: implement --total
This commit is contained in:
parent
dce8a021da
commit
b06485990d
4 changed files with 39 additions and 0 deletions
|
@ -29,6 +29,7 @@ mod options {
|
||||||
pub const DELIMITER_DEFAULT: &str = "\t";
|
pub const DELIMITER_DEFAULT: &str = "\t";
|
||||||
pub const FILE_1: &str = "FILE1";
|
pub const FILE_1: &str = "FILE1";
|
||||||
pub const FILE_2: &str = "FILE2";
|
pub const FILE_2: &str = "FILE2";
|
||||||
|
pub const TOTAL: &str = "total";
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mkdelim(col: usize, opts: &ArgMatches) -> String {
|
fn mkdelim(col: usize, opts: &ArgMatches) -> String {
|
||||||
|
@ -76,6 +77,10 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
||||||
let rb = &mut String::new();
|
let rb = &mut String::new();
|
||||||
let mut nb = b.read_line(rb);
|
let mut nb = b.read_line(rb);
|
||||||
|
|
||||||
|
let mut total_col_1 = 0;
|
||||||
|
let mut total_col_2 = 0;
|
||||||
|
let mut total_col_3 = 0;
|
||||||
|
|
||||||
while na.is_ok() || nb.is_ok() {
|
while na.is_ok() || nb.is_ok() {
|
||||||
let ord = match (na.is_ok(), nb.is_ok()) {
|
let ord = match (na.is_ok(), nb.is_ok()) {
|
||||||
(false, true) => Ordering::Greater,
|
(false, true) => Ordering::Greater,
|
||||||
|
@ -97,6 +102,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
||||||
}
|
}
|
||||||
ra.clear();
|
ra.clear();
|
||||||
na = a.read_line(ra);
|
na = a.read_line(ra);
|
||||||
|
total_col_1 += 1;
|
||||||
}
|
}
|
||||||
Ordering::Greater => {
|
Ordering::Greater => {
|
||||||
if !opts.get_flag(options::COLUMN_2) {
|
if !opts.get_flag(options::COLUMN_2) {
|
||||||
|
@ -105,6 +111,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
||||||
}
|
}
|
||||||
rb.clear();
|
rb.clear();
|
||||||
nb = b.read_line(rb);
|
nb = b.read_line(rb);
|
||||||
|
total_col_2 += 1;
|
||||||
}
|
}
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
if !opts.get_flag(options::COLUMN_3) {
|
if !opts.get_flag(options::COLUMN_3) {
|
||||||
|
@ -115,9 +122,14 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
||||||
rb.clear();
|
rb.clear();
|
||||||
na = a.read_line(ra);
|
na = a.read_line(ra);
|
||||||
nb = b.read_line(rb);
|
nb = b.read_line(rb);
|
||||||
|
total_col_3 += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.get_flag(options::TOTAL) {
|
||||||
|
println!("{total_col_1}\t{total_col_2}\t{total_col_3}\ttotal");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_file(name: &str) -> io::Result<LineReader> {
|
fn open_file(name: &str) -> io::Result<LineReader> {
|
||||||
|
@ -187,4 +199,10 @@ pub fn uu_app() -> Command {
|
||||||
.required(true)
|
.required(true)
|
||||||
.value_hint(clap::ValueHint::FilePath),
|
.value_hint(clap::ValueHint::FilePath),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new(options::TOTAL)
|
||||||
|
.long(options::TOTAL)
|
||||||
|
.help("output a summary")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,22 @@ fn empty_empty() {
|
||||||
.stdout_only_fixture("emptyempty.expected"); // spell-checker:disable-line
|
.stdout_only_fixture("emptyempty.expected"); // spell-checker:disable-line
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn total() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--total", "a", "b"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is_fixture("ab_total.expected");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn total_with_suppressed_regular_output() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--total", "-123", "a", "b"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is_fixture("ab_total_suppressed_regular_output.expected");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn output_delimiter() {
|
fn output_delimiter() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
|
4
tests/fixtures/comm/ab_total.expected
vendored
Normal file
4
tests/fixtures/comm/ab_total.expected
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
a
|
||||||
|
b
|
||||||
|
z
|
||||||
|
1 1 1 total
|
1
tests/fixtures/comm/ab_total_suppressed_regular_output.expected
vendored
Normal file
1
tests/fixtures/comm/ab_total_suppressed_regular_output.expected
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1 1 1 total
|
Loading…
Add table
Add a link
Reference in a new issue