Skip to content

Commit

Permalink
Keep behaviour of sorting results for fast responses even with intera…
Browse files Browse the repository at this point in the history
…ctive terminal
  • Loading branch information
tmccombs committed Nov 15, 2021
1 parent 39dcac7 commit cb4e57f
Showing 1 changed file with 49 additions and 78 deletions.
127 changes: 49 additions & 78 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit cb4e57f

Please sign in to comment.