comm: use delimiter on "total" line

This commit is contained in:
Daniel Hofstetter 2023-01-31 10:08:11 +01:00 committed by Sylvestre Ledru
parent f268e6e3a5
commit ccf999473c
3 changed files with 31 additions and 19 deletions

View file

@ -32,21 +32,12 @@ mod options {
pub const TOTAL: &str = "total"; pub const TOTAL: &str = "total";
} }
fn mkdelim(col: usize, opts: &ArgMatches) -> String { fn column_width(col: &str, opts: &ArgMatches) -> usize {
let mut s = String::new(); if opts.get_flag(col) {
let delim = match opts.get_one::<String>(options::DELIMITER).unwrap().as_str() { 0
"" => "\0", } else {
delim => delim, 1
};
if col > 1 && !opts.get_flag(options::COLUMN_1) {
s.push_str(delim.as_ref());
} }
if col > 2 && !opts.get_flag(options::COLUMN_2) {
s.push_str(delim.as_ref());
}
s
} }
fn ensure_nl(line: &mut String) { fn ensure_nl(line: &mut String) {
@ -70,7 +61,16 @@ impl LineReader {
} }
fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) { 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 ra = &mut String::new();
let mut na = a.read_line(ra); let mut na = a.read_line(ra);
@ -98,7 +98,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
Ordering::Less => { Ordering::Less => {
if !opts.get_flag(options::COLUMN_1) { if !opts.get_flag(options::COLUMN_1) {
ensure_nl(ra); ensure_nl(ra);
print!("{}{}", delim[1], ra); print!("{ra}");
} }
ra.clear(); ra.clear();
na = a.read_line(ra); na = a.read_line(ra);
@ -107,7 +107,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
Ordering::Greater => { Ordering::Greater => {
if !opts.get_flag(options::COLUMN_2) { if !opts.get_flag(options::COLUMN_2) {
ensure_nl(rb); ensure_nl(rb);
print!("{}{}", delim[2], rb); print!("{delim_col_2}{rb}");
} }
rb.clear(); rb.clear();
nb = b.read_line(rb); nb = b.read_line(rb);
@ -116,7 +116,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
Ordering::Equal => { Ordering::Equal => {
if !opts.get_flag(options::COLUMN_3) { if !opts.get_flag(options::COLUMN_3) {
ensure_nl(ra); ensure_nl(ra);
print!("{}{}", delim[3], ra); print!("{delim_col_3}{ra}");
} }
ra.clear(); ra.clear();
rb.clear(); rb.clear();
@ -128,7 +128,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
} }
if opts.get_flag(options::TOTAL) { 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");
} }
} }

View file

@ -71,6 +71,14 @@ fn total_with_suppressed_regular_output() {
.stdout_is_fixture("ab_total_suppressed_regular_output.expected"); .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] #[test]
fn output_delimiter() { fn output_delimiter() {
new_ucmd!() new_ucmd!()

View file

@ -0,0 +1,4 @@
a
wordb
wordwordz
1word1word1wordtotal