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:
parent
a885376583
commit
525f71bada
2 changed files with 57 additions and 32 deletions
|
@ -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 {
|
||||||
|
if is_first {
|
||||||
|
write!(stdout_lock, "{:1$}", result.count.lines, min_width)?;
|
||||||
|
} else {
|
||||||
write!(stdout_lock, " {:1$}", result.count.lines, min_width)?;
|
write!(stdout_lock, " {:1$}", result.count.lines, min_width)?;
|
||||||
}
|
}
|
||||||
|
is_first = false;
|
||||||
|
}
|
||||||
if settings.show_words {
|
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)?;
|
write!(stdout_lock, " {:1$}", result.count.words, min_width)?;
|
||||||
}
|
}
|
||||||
|
is_first = false;
|
||||||
|
}
|
||||||
if settings.show_bytes {
|
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)?;
|
write!(stdout_lock, " {:1$}", result.count.bytes, min_width)?;
|
||||||
}
|
}
|
||||||
|
is_first = false;
|
||||||
|
}
|
||||||
if settings.show_chars {
|
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)?;
|
write!(stdout_lock, " {:1$}", result.count.chars, min_width)?;
|
||||||
}
|
}
|
||||||
|
is_first = false;
|
||||||
|
}
|
||||||
if settings.show_max_line_length {
|
if settings.show_max_line_length {
|
||||||
|
if is_first {
|
||||||
write!(
|
write!(
|
||||||
stdout_lock,
|
stdout_lock,
|
||||||
"{:1$}",
|
"{:1$}",
|
||||||
result.count.max_line_length, min_width
|
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 == "-" {
|
||||||
|
|
|
@ -116,8 +116,6 @@ fn test_multiple_default() {
|
||||||
/// 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()
|
||||||
|
@ -128,8 +126,6 @@ fn test_file_empty() {
|
||||||
/// *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()
|
||||||
|
@ -140,8 +136,6 @@ fn test_file_single_line_no_trailing_newline() {
|
||||||
/// 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()
|
||||||
|
@ -151,8 +145,6 @@ fn test_file_many_empty_lines() {
|
||||||
/// 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()
|
||||||
|
@ -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 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()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue