From 4521aa2659c2e44516415913d63da0774e1fd1bc Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Thu, 6 May 2021 22:39:39 -0400 Subject: [PATCH] wc: print counts for each file as soon as computed Change the behavior of `wc` to print the counts for a file as soon as it is computed, instead of waiting to compute the counts for all files before writing any output to `stdout`. The new behavior matches the behavior of GNU `wc`. The old behavior looked like this (the word "hello" is entered on `stdin`): $ wc emptyfile.txt - hello 0 0 0 emptyfile.txt 1 1 6 1 1 6 total The new behavior looks like this: $ wc emptyfile.txt - 0 0 0 emptyfile.txt hello 1 1 6 1 1 6 total --- src/uu/wc/src/wc.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/uu/wc/src/wc.rs b/src/uu/wc/src/wc.rs index b323f7261..6e95254ee 100644 --- a/src/uu/wc/src/wc.rs +++ b/src/uu/wc/src/wc.rs @@ -373,7 +373,6 @@ fn wc(inputs: Vec, settings: &Settings) -> Result<(), u32> { let max_width = max_width(&inputs); let mut total_word_count = WordCount::default(); - let mut results = vec![]; let num_inputs = inputs.len(); @@ -384,10 +383,7 @@ fn wc(inputs: Vec, settings: &Settings) -> Result<(), u32> { WordCount::default() }); total_word_count += word_count; - results.push(word_count.with_title(input.to_title())); - } - - for result in &results { + let result = word_count.with_title(input.to_title()); if let Err(err) = print_stats(settings, &result, max_width) { show_warning!( "failed to print result for {}: {}",