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::sync::mpsc::Receiver;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use::walk::WorkerResult;
|
||||
use::internal::error;
|
||||
use super::CommandTemplate;
|
||||
|
||||
/// 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
|
||||
/// 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 {
|
||||
// Create a lock on the shared receiver for this thread.
|
||||
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
|
||||
// has closed, exit from the loop
|
||||
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,
|
||||
};
|
||||
|
||||
|
|
22
src/walk.rs
22
src/walk.rs
|
@ -36,6 +36,12 @@ enum ReceiverMode {
|
|||
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.
|
||||
///
|
||||
/// If the `--exec` argument was supplied, this will create a thread pool for executing
|
||||
|
@ -156,7 +162,9 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
|||
.max_buffer_time
|
||||
.unwrap_or_else(|| time::Duration::from_millis(100));
|
||||
|
||||
for value in rx {
|
||||
for worker_result in rx {
|
||||
match worker_result {
|
||||
WorkerResult::Entry(value) => {
|
||||
match mode {
|
||||
ReceiverMode::Buffering => {
|
||||
buffer.push(value);
|
||||
|
@ -180,6 +188,11 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
|||
}
|
||||
}
|
||||
}
|
||||
WorkerResult::Error(err) => {
|
||||
error(&format!("{}", err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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:
|
||||
|
@ -206,7 +219,10 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
|||
|
||||
let entry = match entry_o {
|
||||
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 {
|
||||
|
@ -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 pattern.is_match(&*search_str) {
|
||||
// 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