mirror of
https://github.com/sharkdp/fd.git
synced 2024-11-18 09:50:34 +01:00
feat: enable display of error messages
This commit is contained in:
parent
27caa33729
commit
2ebef2d46f
2 changed files with 46 additions and 22 deletions
|
@ -9,13 +9,14 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::mpsc::Receiver;
|
use std::sync::mpsc::Receiver;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
use::walk::WorkerResult;
|
||||||
|
use::internal::error;
|
||||||
use super::CommandTemplate;
|
use super::CommandTemplate;
|
||||||
|
|
||||||
/// An event loop that listens for inputs from the `rx` receiver. Each received input will
|
/// An event loop that listens for inputs from the `rx` receiver. Each received input will
|
||||||
/// generate a command with the supplied command template. The generated command will then
|
/// generate a command with the supplied command template. The generated command will then
|
||||||
/// be executed, and this process will continue until the receiver's sender has closed.
|
/// be executed, and this process will continue until the receiver's sender has closed.
|
||||||
pub fn job(rx: Arc<Mutex<Receiver<PathBuf>>>, cmd: Arc<CommandTemplate>, out_perm: Arc<Mutex<()>>) {
|
pub fn job(rx: Arc<Mutex<Receiver<WorkerResult>>>, cmd: Arc<CommandTemplate>, out_perm: Arc<Mutex<()>>) {
|
||||||
loop {
|
loop {
|
||||||
// Create a lock on the shared receiver for this thread.
|
// Create a lock on the shared receiver for this thread.
|
||||||
let lock = rx.lock().unwrap();
|
let lock = rx.lock().unwrap();
|
||||||
|
@ -23,7 +24,14 @@ pub fn job(rx: Arc<Mutex<Receiver<PathBuf>>>, cmd: Arc<CommandTemplate>, out_per
|
||||||
// Obtain the next path from the receiver, else if the channel
|
// Obtain the next path from the receiver, else if the channel
|
||||||
// has closed, exit from the loop
|
// has closed, exit from the loop
|
||||||
let value: PathBuf = match lock.recv() {
|
let value: PathBuf = match lock.recv() {
|
||||||
Ok(value) => value,
|
Ok(value) => {
|
||||||
|
match value {
|
||||||
|
WorkerResult::Entry(val) => val,
|
||||||
|
WorkerResult::Error(err) => {
|
||||||
|
error(&format!("{}", err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
54
src/walk.rs
54
src/walk.rs
|
@ -36,6 +36,12 @@ enum ReceiverMode {
|
||||||
Streaming,
|
Streaming,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The Worker threads can result in a valid entry having PathBuf or an error.
|
||||||
|
pub enum WorkerResult {
|
||||||
|
Entry(PathBuf),
|
||||||
|
Error(ignore::Error)
|
||||||
|
}
|
||||||
|
|
||||||
/// Recursively scan the given search path for files / pathnames matching the pattern.
|
/// 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
|
/// If the `--exec` argument was supplied, this will create a thread pool for executing
|
||||||
|
@ -156,27 +162,34 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
||||||
.max_buffer_time
|
.max_buffer_time
|
||||||
.unwrap_or_else(|| time::Duration::from_millis(100));
|
.unwrap_or_else(|| time::Duration::from_millis(100));
|
||||||
|
|
||||||
for value in rx {
|
for worker_result in rx {
|
||||||
match mode {
|
match worker_result {
|
||||||
ReceiverMode::Buffering => {
|
WorkerResult::Entry(value) => {
|
||||||
buffer.push(value);
|
match mode {
|
||||||
|
ReceiverMode::Buffering => {
|
||||||
|
buffer.push(value);
|
||||||
|
|
||||||
// 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
|
|| time::Instant::now() - start > max_buffer_time
|
||||||
{
|
{
|
||||||
// Flush the buffer
|
// Flush the buffer
|
||||||
for v in &buffer {
|
for v in &buffer {
|
||||||
output::print_entry(v, &rx_config, &receiver_wtq);
|
output::print_entry(v, &rx_config, &receiver_wtq);
|
||||||
|
}
|
||||||
|
buffer.clear();
|
||||||
|
|
||||||
|
// Start streaming
|
||||||
|
mode = ReceiverMode::Streaming;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ReceiverMode::Streaming => {
|
||||||
|
output::print_entry(&value, &rx_config, &receiver_wtq);
|
||||||
}
|
}
|
||||||
buffer.clear();
|
|
||||||
|
|
||||||
// Start streaming
|
|
||||||
mode = ReceiverMode::Streaming;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ReceiverMode::Streaming => {
|
WorkerResult::Error(err) => {
|
||||||
output::print_entry(&value, &rx_config, &receiver_wtq);
|
error(&format!("{}", err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -206,7 +219,10 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
||||||
|
|
||||||
let entry = match entry_o {
|
let entry = match entry_o {
|
||||||
Ok(e) => e,
|
Ok(e) => e,
|
||||||
Err(_) => return ignore::WalkState::Continue,
|
Err(err) => {
|
||||||
|
tx_thread.send(WorkerResult::Error(err)).unwrap();
|
||||||
|
return ignore::WalkState::Continue;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if entry.depth() == 0 {
|
if entry.depth() == 0 {
|
||||||
|
@ -284,7 +300,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
||||||
if let Some(search_str) = search_str_o {
|
if let Some(search_str) = search_str_o {
|
||||||
if pattern.is_match(&*search_str) {
|
if pattern.is_match(&*search_str) {
|
||||||
// TODO: take care of the unwrap call
|
// TODO: take care of the unwrap call
|
||||||
tx_thread.send(entry_path.to_owned()).unwrap()
|
tx_thread.send(WorkerResult::Entry(entry_path.to_owned())).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue