1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

wc: rm leading space when printing multiple counts

Remove the leading space from the output of `wc` when printing two or
more types of counts.

Fixes #2173.
This commit is contained in:
Jeffrey Finkelstein 2021-05-05 20:59:37 -04:00 committed by Sylvestre Ledru
parent a885376583
commit 525f71bada
2 changed files with 57 additions and 32 deletions

View file

@ -323,7 +323,12 @@ fn wc(files: Vec<String>, settings: &Settings) -> Result<(), u32> {
error_count += 1; error_count += 1;
WordCount::default() WordCount::default()
}); });
max_width = max(max_width, word_count.bytes.to_string().len() + 1); // Compute the number of digits needed to display the number
// of bytes in the file. Even if the settings indicate that we
// won't *display* the number of bytes, we still use the
// number of digits in the byte count as the width when
// formatting each count as a string for output.
max_width = max(max_width, word_count.bytes.to_string().len());
total_word_count += word_count; total_word_count += word_count;
results.push(word_count.with_title(path)); results.push(word_count.with_title(path));
} }
@ -364,24 +369,54 @@ fn print_stats(
min_width = 0; min_width = 0;
} }
let mut is_first: bool = true;
if settings.show_lines { if settings.show_lines {
write!(stdout_lock, "{:1$}", result.count.lines, min_width)?; if is_first {
write!(stdout_lock, "{:1$}", result.count.lines, min_width)?;
} else {
write!(stdout_lock, " {:1$}", result.count.lines, min_width)?;
}
is_first = false;
} }
if settings.show_words { if settings.show_words {
write!(stdout_lock, "{:1$}", result.count.words, min_width)?; if is_first {
write!(stdout_lock, "{:1$}", result.count.words, min_width)?;
} else {
write!(stdout_lock, " {:1$}", result.count.words, min_width)?;
}
is_first = false;
} }
if settings.show_bytes { if settings.show_bytes {
write!(stdout_lock, "{:1$}", result.count.bytes, min_width)?; if is_first {
write!(stdout_lock, "{:1$}", result.count.bytes, min_width)?;
} else {
write!(stdout_lock, " {:1$}", result.count.bytes, min_width)?;
}
is_first = false;
} }
if settings.show_chars { if settings.show_chars {
write!(stdout_lock, "{:1$}", result.count.chars, min_width)?; if is_first {
write!(stdout_lock, "{:1$}", result.count.chars, min_width)?;
} else {
write!(stdout_lock, " {:1$}", result.count.chars, min_width)?;
}
is_first = false;
} }
if settings.show_max_line_length { if settings.show_max_line_length {
write!( if is_first {
stdout_lock, write!(
"{:1$}", stdout_lock,
result.count.max_line_length, min_width "{:1$}",
)?; result.count.max_line_length, min_width
)?;
} else {
write!(
stdout_lock,
" {:1$}",
result.count.max_line_length, min_width
)?;
}
} }
if result.title == "-" { if result.title == "-" {

View file

@ -33,7 +33,7 @@ fn test_stdin_default() {
new_ucmd!() new_ucmd!()
.pipe_in_fixture("lorem_ipsum.txt") .pipe_in_fixture("lorem_ipsum.txt")
.run() .run()
.stdout_is(" 13 109 772\n"); .stdout_is(" 13 109 772\n");
} }
#[test] #[test]
@ -42,7 +42,7 @@ fn test_utf8() {
.args(&["-lwmcL"]) .args(&["-lwmcL"])
.pipe_in_fixture("UTF_8_test.txt") .pipe_in_fixture("UTF_8_test.txt")
.run() .run()
.stdout_is(" 300 4969 22781 22213 79\n"); .stdout_is(" 300 4969 22781 22213 79\n");
// GNU returns " 300 2086 22219 22781 79" // GNU returns " 300 2086 22219 22781 79"
// TODO: we should fix that to match GNU's behavior // TODO: we should fix that to match GNU's behavior
} }
@ -71,7 +71,7 @@ fn test_stdin_all_counts() {
.args(&["-c", "-m", "-l", "-L", "-w"]) .args(&["-c", "-m", "-l", "-L", "-w"])
.pipe_in_fixture("alice_in_wonderland.txt") .pipe_in_fixture("alice_in_wonderland.txt")
.run() .run()
.stdout_is(" 5 57 302 302 66\n"); .stdout_is(" 5 57 302 302 66\n");
} }
#[test] #[test]
@ -79,7 +79,7 @@ fn test_single_default() {
new_ucmd!() new_ucmd!()
.arg("moby_dick.txt") .arg("moby_dick.txt")
.run() .run()
.stdout_is(" 18 204 1115 moby_dick.txt\n"); .stdout_is(" 18 204 1115 moby_dick.txt\n");
} }
#[test] #[test]
@ -95,7 +95,7 @@ fn test_single_all_counts() {
new_ucmd!() new_ucmd!()
.args(&["-c", "-l", "-L", "-m", "-w", "alice_in_wonderland.txt"]) .args(&["-c", "-l", "-L", "-m", "-w", "alice_in_wonderland.txt"])
.run() .run()
.stdout_is(" 5 57 302 302 66 alice_in_wonderland.txt\n"); .stdout_is(" 5 57 302 302 66 alice_in_wonderland.txt\n");
} }
#[test] #[test]
@ -108,64 +108,54 @@ fn test_multiple_default() {
]) ])
.run() .run()
.stdout_is( .stdout_is(
" 13 109 772 lorem_ipsum.txt\n 18 204 1115 moby_dick.txt\n 5 57 302 \ " 13 109 772 lorem_ipsum.txt\n 18 204 1115 moby_dick.txt\n 5 57 302 \
alice_in_wonderland.txt\n 36 370 2189 total\n", alice_in_wonderland.txt\n 36 370 2189 total\n",
); );
} }
/// Test for an empty file. /// Test for an empty file.
#[test] #[test]
fn test_file_empty() { fn test_file_empty() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!() new_ucmd!()
.args(&["-clmwL", "emptyfile.txt"]) .args(&["-clmwL", "emptyfile.txt"])
.run() .run()
.stdout_is(" 0 0 0 0 0 emptyfile.txt\n"); .stdout_is("0 0 0 0 0 emptyfile.txt\n");
} }
/// Test for an file containing a single non-whitespace character /// Test for an file containing a single non-whitespace character
/// *without* a trailing newline. /// *without* a trailing newline.
#[test] #[test]
fn test_file_single_line_no_trailing_newline() { fn test_file_single_line_no_trailing_newline() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!() new_ucmd!()
.args(&["-clmwL", "notrailingnewline.txt"]) .args(&["-clmwL", "notrailingnewline.txt"])
.run() .run()
.stdout_is(" 1 1 2 2 1 notrailingnewline.txt\n"); .stdout_is("1 1 2 2 1 notrailingnewline.txt\n");
} }
/// Test for a file that has 100 empty lines (that is, the contents of /// Test for a file that has 100 empty lines (that is, the contents of
/// the file are the newline character repeated one hundred times). /// the file are the newline character repeated one hundred times).
#[test] #[test]
fn test_file_many_empty_lines() { fn test_file_many_empty_lines() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!() new_ucmd!()
.args(&["-clmwL", "manyemptylines.txt"]) .args(&["-clmwL", "manyemptylines.txt"])
.run() .run()
.stdout_is(" 100 0 100 100 0 manyemptylines.txt\n"); .stdout_is("100 0 100 100 0 manyemptylines.txt\n");
} }
/// Test for a file that has one long line comprising only spaces. /// Test for a file that has one long line comprising only spaces.
#[test] #[test]
fn test_file_one_long_line_only_spaces() { fn test_file_one_long_line_only_spaces() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!() new_ucmd!()
.args(&["-clmwL", "onelongemptyline.txt"]) .args(&["-clmwL", "onelongemptyline.txt"])
.run() .run()
.stdout_is(" 1 0 10001 10001 10000 onelongemptyline.txt\n"); .stdout_is(" 1 0 10001 10001 10000 onelongemptyline.txt\n");
} }
/// Test for a file that has one long line comprising a single "word". /// Test for a file that has one long line comprising a single "word".
#[test] #[test]
fn test_file_one_long_word() { fn test_file_one_long_word() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!() new_ucmd!()
.args(&["-clmwL", "onelongword.txt"]) .args(&["-clmwL", "onelongword.txt"])
.run() .run()
.stdout_is(" 1 1 10001 10001 10000 onelongword.txt\n"); .stdout_is(" 1 1 10001 10001 10000 onelongword.txt\n");
} }