From cb4e57f0ec5a218c87846c8b520466dae3a8016f Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Mon, 15 Nov 2021 00:03:10 -0700 Subject: [PATCH] Keep behaviour of sorting results for fast responses even with interactive terminal --- src/walk.rs | 127 ++++++++++++++++++++-------------------------------- 1 file changed, 49 insertions(+), 78 deletions(-) diff --git a/src/walk.rs b/src/walk.rs index e0c52c232..16f59b3e9 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -47,7 +47,6 @@ pub enum WorkerResult { /// Maximum size of the output buffer before flushing results to the console pub const MAX_BUFFER_LENGTH: usize = 1000; - /// Recursively scan the given search path for files / pathnames matching the pattern. /// /// If the `--exec` argument was supplied, this will create a thread pool for executing @@ -235,97 +234,69 @@ fn spawn_receiver( let mut stdout = io::BufWriter::new(stdout); let mut num_results = 0; - if config.interactive_terminal { - let mut buffer = Vec::with_capacity(MAX_BUFFER_LENGTH); - for worker_result in rx { - match worker_result { - WorkerResult::Entry(path) => { - if config.quiet { - return ExitCode::HasResults(true); - } + let is_interactive = config.interactive_terminal; + let mut buffer = Vec::with_capacity(MAX_BUFFER_LENGTH); + for worker_result in rx { + match worker_result { + WorkerResult::Entry(path) => { + if config.quiet { + return ExitCode::HasResults(true); + } - match mode { - ReceiverMode::Buffering => { - buffer.push(path); - - // Have we reached the maximum buffer size or maximum buffering time? - if buffer.len() > MAX_BUFFER_LENGTH - || time::Instant::now() - start > max_buffer_time - { - // Flush the buffer - for path in &buffer { - output::print_entry( - &mut stdout, - path, - &config, - &wants_to_quit, - ); - } - buffer.clear(); - let r = stdout.flush(); - if r.is_err() { - // Probably a broken pipe. Exit gracefully. - process::exit(ExitCode::GeneralError.into()); - } - // Start streaming - mode = ReceiverMode::Streaming; + match mode { + ReceiverMode::Buffering => { + buffer.push(path); + + // Have we reached the maximum buffer size or maximum buffering time? + if buffer.len() > MAX_BUFFER_LENGTH + || time::Instant::now() - start > max_buffer_time + { + // Flush the buffer + for path in &buffer { + output::print_entry( + &mut stdout, + path, + &config, + &wants_to_quit, + ); } - } - ReceiverMode::Streaming => { - output::print_entry( - &mut stdout, - &path, - &config, - &wants_to_quit, - ); - let r = stdout.flush(); - if r.is_err() { + buffer.clear(); + if is_interactive && stdout.flush().is_err() { // Probably a broken pipe. Exit gracefully. - process::exit(ExitCode::GeneralError.into()); + return ExitCode::GeneralError; } + // Start streaming + mode = ReceiverMode::Streaming; } } - num_results += 1; - } - WorkerResult::Error(err) => { - if show_filesystem_errors { - print_error(err.to_string()); + ReceiverMode::Streaming => { + output::print_entry(&mut stdout, &path, &config, &wants_to_quit); + if is_interactive && stdout.flush().is_err() { + // Probably a broken pipe. Exit gracefully. + return ExitCode::GeneralError; + } } } + num_results += 1; } - if let Some(max_results) = config.max_results { - if num_results >= max_results { - break; + WorkerResult::Error(err) => { + if show_filesystem_errors { + print_error(err.to_string()); } } } - // If we have finished fast enough (faster than max_buffer_time), we haven't streamed - // anything to the console, yet. In this case, sort the results and print them: - if !buffer.is_empty() { - buffer.sort(); - for path in buffer { - output::print_entry(&mut stdout, &path, &config, &wants_to_quit); + if let Some(max_results) = config.max_results { + if num_results >= max_results { + break; } } - } else { - let mut num_results = 0; - for worker_result in rx { - match worker_result { - WorkerResult::Entry(path) => { - output::print_entry(&mut stdout, &path, &config, &wants_to_quit); - num_results += 1_usize; - } - WorkerResult::Error(err) => { - if show_filesystem_errors { - print_error(err.to_string()); - } - } - } - if let Some(max_results) = config.max_results { - if num_results >= max_results { - break; - } - } + } + // If we have finished fast enough (faster than max_buffer_time), we haven't streamed + // anything to the console, yet. In this case, sort the results and print them: + if !buffer.is_empty() { + buffer.sort(); + for path in buffer { + output::print_entry(&mut stdout, &path, &config, &wants_to_quit); } }