mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 23:02:38 +00:00
comm: use delimiter on "total" line
This commit is contained in:
parent
f268e6e3a5
commit
ccf999473c
3 changed files with 31 additions and 19 deletions
|
@ -32,21 +32,12 @@ mod options {
|
|||
pub const TOTAL: &str = "total";
|
||||
}
|
||||
|
||||
fn mkdelim(col: usize, opts: &ArgMatches) -> String {
|
||||
let mut s = String::new();
|
||||
let delim = match opts.get_one::<String>(options::DELIMITER).unwrap().as_str() {
|
||||
"" => "\0",
|
||||
delim => delim,
|
||||
};
|
||||
|
||||
if col > 1 && !opts.get_flag(options::COLUMN_1) {
|
||||
s.push_str(delim.as_ref());
|
||||
fn column_width(col: &str, opts: &ArgMatches) -> usize {
|
||||
if opts.get_flag(col) {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
}
|
||||
if col > 2 && !opts.get_flag(options::COLUMN_2) {
|
||||
s.push_str(delim.as_ref());
|
||||
}
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
fn ensure_nl(line: &mut String) {
|
||||
|
@ -70,7 +61,16 @@ impl LineReader {
|
|||
}
|
||||
|
||||
fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
||||
let delim: Vec<String> = (0..4).map(|col| mkdelim(col, opts)).collect();
|
||||
let delim = match opts.get_one::<String>(options::DELIMITER).unwrap().as_str() {
|
||||
"" => "\0",
|
||||
delim => delim,
|
||||
};
|
||||
|
||||
let width_col_1 = column_width(options::COLUMN_1, opts);
|
||||
let width_col_2 = column_width(options::COLUMN_2, opts);
|
||||
|
||||
let delim_col_2 = delim.repeat(width_col_1);
|
||||
let delim_col_3 = delim.repeat(width_col_1 + width_col_2);
|
||||
|
||||
let ra = &mut String::new();
|
||||
let mut na = a.read_line(ra);
|
||||
|
@ -98,7 +98,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
|||
Ordering::Less => {
|
||||
if !opts.get_flag(options::COLUMN_1) {
|
||||
ensure_nl(ra);
|
||||
print!("{}{}", delim[1], ra);
|
||||
print!("{ra}");
|
||||
}
|
||||
ra.clear();
|
||||
na = a.read_line(ra);
|
||||
|
@ -107,7 +107,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
|||
Ordering::Greater => {
|
||||
if !opts.get_flag(options::COLUMN_2) {
|
||||
ensure_nl(rb);
|
||||
print!("{}{}", delim[2], rb);
|
||||
print!("{delim_col_2}{rb}");
|
||||
}
|
||||
rb.clear();
|
||||
nb = b.read_line(rb);
|
||||
|
@ -116,7 +116,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
|||
Ordering::Equal => {
|
||||
if !opts.get_flag(options::COLUMN_3) {
|
||||
ensure_nl(ra);
|
||||
print!("{}{}", delim[3], ra);
|
||||
print!("{delim_col_3}{ra}");
|
||||
}
|
||||
ra.clear();
|
||||
rb.clear();
|
||||
|
@ -128,7 +128,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
|||
}
|
||||
|
||||
if opts.get_flag(options::TOTAL) {
|
||||
println!("{total_col_1}\t{total_col_2}\t{total_col_3}\ttotal");
|
||||
println!("{total_col_1}{delim}{total_col_2}{delim}{total_col_3}{delim}total");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -71,6 +71,14 @@ fn total_with_suppressed_regular_output() {
|
|||
.stdout_is_fixture("ab_total_suppressed_regular_output.expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn total_with_output_delimiter() {
|
||||
new_ucmd!()
|
||||
.args(&["--total", "--output-delimiter=word", "a", "b"])
|
||||
.succeeds()
|
||||
.stdout_is_fixture("ab_total_delimiter_word.expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_delimiter() {
|
||||
new_ucmd!()
|
||||
|
|
4
tests/fixtures/comm/ab_total_delimiter_word.expected
vendored
Normal file
4
tests/fixtures/comm/ab_total_delimiter_word.expected
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
a
|
||||
wordb
|
||||
wordwordz
|
||||
1word1word1wordtotal
|
Loading…
Reference in a new issue