mirror of https://github.com/sharkdp/fd.git
Refactor: use some nice Rust methods in buffering code
This commit is contained in:
parent
45a86459b2
commit
3de948ae0d
27
src/walk.rs
27
src/walk.rs
|
@ -40,6 +40,8 @@ pub enum WorkerResult {
|
||||||
|
|
||||||
/// Maximum size of the output buffer before flushing results to the console
|
/// Maximum size of the output buffer before flushing results to the console
|
||||||
pub const MAX_BUFFER_LENGTH: usize = 1000;
|
pub const MAX_BUFFER_LENGTH: usize = 1000;
|
||||||
|
/// Default duration until output buffering switches to streaming.
|
||||||
|
pub const DEFAULT_MAX_BUFFER_TIME: time::Duration = time::Duration::from_millis(100);
|
||||||
|
|
||||||
/// Recursively scan the given search path for files / pathnames matching the pattern.
|
/// Recursively scan the given search path for files / pathnames matching the pattern.
|
||||||
///
|
///
|
||||||
|
@ -220,9 +222,7 @@ fn spawn_receiver(
|
||||||
let mut mode = ReceiverMode::Buffering;
|
let mut mode = ReceiverMode::Buffering;
|
||||||
|
|
||||||
// Maximum time to wait before we start streaming to the console.
|
// Maximum time to wait before we start streaming to the console.
|
||||||
let max_buffer_time = config
|
let max_buffer_time = config.max_buffer_time.unwrap_or(DEFAULT_MAX_BUFFER_TIME);
|
||||||
.max_buffer_time
|
|
||||||
.unwrap_or_else(|| time::Duration::from_millis(100));
|
|
||||||
|
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut stdout = stdout.lock();
|
let mut stdout = stdout.lock();
|
||||||
|
@ -242,7 +242,7 @@ fn spawn_receiver(
|
||||||
|
|
||||||
// Have we reached the maximum buffer size or maximum buffering time?
|
// Have we reached the maximum buffer size or maximum buffering time?
|
||||||
if buffer.len() > MAX_BUFFER_LENGTH
|
if buffer.len() > MAX_BUFFER_LENGTH
|
||||||
|| time::Instant::now() - start > max_buffer_time
|
|| start.elapsed() > max_buffer_time
|
||||||
{
|
{
|
||||||
// Flush the buffer
|
// Flush the buffer
|
||||||
for v in &buffer {
|
for v in &buffer {
|
||||||
|
@ -265,6 +265,11 @@ fn spawn_receiver(
|
||||||
}
|
}
|
||||||
|
|
||||||
num_results += 1;
|
num_results += 1;
|
||||||
|
if let Some(max_results) = config.max_results {
|
||||||
|
if num_results >= max_results {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
WorkerResult::Error(err) => {
|
WorkerResult::Error(err) => {
|
||||||
if show_filesystem_errors {
|
if show_filesystem_errors {
|
||||||
|
@ -272,21 +277,13 @@ fn spawn_receiver(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// 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:
|
// anything to the console, yet. In this case, sort the results and print them:
|
||||||
if !buffer.is_empty() {
|
buffer.sort();
|
||||||
buffer.sort();
|
for value in buffer {
|
||||||
for value in buffer {
|
output::print_entry(&mut stdout, &value, &config, &wants_to_quit);
|
||||||
output::print_entry(&mut stdout, &value, &config, &wants_to_quit);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.quiet {
|
if config.quiet {
|
||||||
|
|
Loading…
Reference in New Issue