1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27: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;
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;
results.push(word_count.with_title(path));
}
@ -364,24 +369,54 @@ fn print_stats(
min_width = 0;
}
let mut is_first: bool = true;
if settings.show_lines {
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 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 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 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 is_first {
write!(
stdout_lock,
"{:1$}",
result.count.max_line_length, min_width
)?;
} else {
write!(
stdout_lock,
" {:1$}",
result.count.max_line_length, min_width
)?;
}
}
if result.title == "-" {

View file

@ -116,8 +116,6 @@ fn test_multiple_default() {
/// Test for an empty file.
#[test]
fn test_file_empty() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!()
.args(&["-clmwL", "emptyfile.txt"])
.run()
@ -128,8 +126,6 @@ fn test_file_empty() {
/// *without* a trailing newline.
#[test]
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!()
.args(&["-clmwL", "notrailingnewline.txt"])
.run()
@ -140,8 +136,6 @@ fn test_file_single_line_no_trailing_newline() {
/// the file are the newline character repeated one hundred times).
#[test]
fn test_file_many_empty_lines() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!()
.args(&["-clmwL", "manyemptylines.txt"])
.run()
@ -151,8 +145,6 @@ fn test_file_many_empty_lines() {
/// Test for a file that has one long line comprising only spaces.
#[test]
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!()
.args(&["-clmwL", "onelongemptyline.txt"])
.run()
@ -162,8 +154,6 @@ fn test_file_one_long_line_only_spaces() {
/// Test for a file that has one long line comprising a single "word".
#[test]
fn test_file_one_long_word() {
// TODO There is a leading space in the output that should be
// removed; see issue #2173.
new_ucmd!()
.args(&["-clmwL", "onelongword.txt"])
.run()