diff --git a/src/filetypes.rs b/src/filetypes.rs index 1f445da4d..2baf0f28f 100644 --- a/src/filetypes.rs +++ b/src/filetypes.rs @@ -1,3 +1,6 @@ +use crate::filesystem; +use crate::walk; + /// Whether or not to show pub struct FileTypes { pub files: bool, @@ -22,3 +25,28 @@ impl Default for FileTypes { } } } + +impl FileTypes { + pub fn should_ignore(&self, entry: &walk::DirEntry) -> bool { + if let Some(ref entry_type) = entry.file_type() { + (!self.files && entry_type.is_file()) + || (!self.directories && entry_type.is_dir()) + || (!self.symlinks && entry_type.is_symlink()) + || (!self.sockets && filesystem::is_socket(*entry_type)) + || (!self.pipes && filesystem::is_pipe(*entry_type)) + || (self.executables_only + && !entry + .metadata() + .map(|m| filesystem::is_executable(&m)) + .unwrap_or(false)) + || (self.empty_only && !filesystem::is_empty(&entry)) + || !(entry_type.is_file() + || entry_type.is_dir() + || entry_type.is_symlink() + || filesystem::is_socket(*entry_type) + || filesystem::is_pipe(*entry_type)) + } else { + true + } + } +} diff --git a/src/main.rs b/src/main.rs index 8654688ad..99470daa9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,8 @@ use anyhow::{anyhow, Context, Result}; use atty::Stream; use globset::GlobBuilder; use lscolors::LsColors; -use regex::bytes::{RegexBuilder, RegexSetBuilder}; use normpath::PathExt; +use regex::bytes::{RegexBuilder, RegexSetBuilder}; use crate::error::print_error; use crate::exec::CommandTemplate; diff --git a/src/walk.rs b/src/walk.rs index a07387322..e2966b38e 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -416,27 +416,7 @@ fn spawn_senders( // Filter out unwanted file types. if let Some(ref file_types) = config.file_types { - if let Some(ref entry_type) = entry.file_type() { - if (!file_types.files && entry_type.is_file()) - || (!file_types.directories && entry_type.is_dir()) - || (!file_types.symlinks && entry_type.is_symlink()) - || (!file_types.sockets && filesystem::is_socket(*entry_type)) - || (!file_types.pipes && filesystem::is_pipe(*entry_type)) - || (file_types.executables_only - && !entry - .metadata() - .map(|m| filesystem::is_executable(&m)) - .unwrap_or(false)) - || (file_types.empty_only && !filesystem::is_empty(&entry)) - || !(entry_type.is_file() - || entry_type.is_dir() - || entry_type.is_symlink() - || filesystem::is_socket(*entry_type) - || filesystem::is_pipe(*entry_type)) - { - return ignore::WalkState::Continue; - } - } else { + if file_types.should_ignore(&entry) { return ignore::WalkState::Continue; } }