mirror of
https://github.com/sharkdp/fd.git
synced 2024-09-28 13:01:30 +02:00
Add filter trait for walk
This commit is contained in:
parent
570ea78d24
commit
4e811dbbec
55
src/walk.rs
55
src/walk.rs
@ -9,7 +9,7 @@ use std::sync::mpsc::channel;
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
use ignore::{self, WalkBuilder};
|
use ignore::{self, WalkBuilder, DirEntry};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
/// The receiver thread can either be buffering results or directly streaming to the console.
|
/// The receiver thread can either be buffering results or directly streaming to the console.
|
||||||
@ -31,6 +31,34 @@ pub enum FileType {
|
|||||||
SymLink,
|
SymLink,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait Filter {
|
||||||
|
fn filter_by_type(&self, item: &DirEntry) -> bool;
|
||||||
|
fn filter_by_ext(&self, item: &DirEntry) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Filter for FdOptions {
|
||||||
|
fn filter_by_type(&self, entry: &DirEntry) -> bool {
|
||||||
|
match self.file_type {
|
||||||
|
FileType::RegularFile => entry.file_type().map_or(true, |ft| !ft.is_file()),
|
||||||
|
FileType::Directory => entry.file_type().map_or(true, |ft| !ft.is_dir()),
|
||||||
|
FileType::SymLink => entry.file_type().map_or(true, |ft| !ft.is_symlink()),
|
||||||
|
FileType::Any => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn filter_by_ext(&self, entry: &DirEntry) -> bool {
|
||||||
|
if let Some(ref filter_ext) = self.extension {
|
||||||
|
let entry_ext = entry.path().extension().map(|e| {
|
||||||
|
e.to_string_lossy().to_lowercase()
|
||||||
|
});
|
||||||
|
if entry_ext.map_or(true, |ext| ext != *filter_ext) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
/// 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
|
||||||
@ -150,34 +178,13 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, config: Arc<FdOptions>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Filter out unwanted file types.
|
// Filter out unwanted file types.
|
||||||
match config.file_type {
|
if config.filter_by_type(&entry) {
|
||||||
FileType::Any => (),
|
|
||||||
FileType::RegularFile => {
|
|
||||||
if entry.file_type().map_or(false, |ft| !ft.is_file()) {
|
|
||||||
return ignore::WalkState::Continue;
|
return ignore::WalkState::Continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
FileType::Directory => {
|
|
||||||
if entry.file_type().map_or(false, |ft| !ft.is_dir()) {
|
|
||||||
return ignore::WalkState::Continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FileType::SymLink => {
|
|
||||||
if entry.file_type().map_or(false, |ft| !ft.is_symlink()) {
|
|
||||||
return ignore::WalkState::Continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter out unwanted extensions.
|
if config.filter_by_ext(&entry) {
|
||||||
if let Some(ref filter_ext) = config.extension {
|
|
||||||
let entry_ext = entry_path.extension().map(
|
|
||||||
|e| e.to_string_lossy().to_lowercase(),
|
|
||||||
);
|
|
||||||
if entry_ext.map_or(true, |ext| ext != *filter_ext) {
|
|
||||||
return ignore::WalkState::Continue;
|
return ignore::WalkState::Continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let search_str_o = if config.search_full_path {
|
let search_str_o = if config.search_full_path {
|
||||||
match fshelper::path_absolute_form(&entry_path) {
|
match fshelper::path_absolute_form(&entry_path) {
|
||||||
|
Loading…
Reference in New Issue
Block a user