Refactor file types check to be on impl of FileTypes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Relates to #382
authored by Thayne McCombs · committed by David Peter · parent c06c995 · Browse files · 3 files·+30 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−22 removed
- other:+30 added−22 removed
Provenance
- Commit
115ae93- Tree
e4ebc49- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
c06c995
3 files changed+30 added−22 removed
modified:src/filetypes.rs+28 added
1+use crate::filesystem;2+use crate::walk;3+4 /// Whether or not to show5 pub struct FileTypes {6 pub files: bool,7 pub directories: bool,16 unmodified lines24 empty_only: false,25 }26 }27 }28+29+impl FileTypes {30+ pub fn should_ignore(&self, entry: &walk::DirEntry) -> bool {31+ if let Some(ref entry_type) = entry.file_type() {32+ (!self.files && entry_type.is_file())33+ || (!self.directories && entry_type.is_dir())34+ || (!self.symlinks && entry_type.is_symlink())35+ || (!self.sockets && filesystem::is_socket(*entry_type))36+ || (!self.pipes && filesystem::is_pipe(*entry_type))37+ || (self.executables_only38+ && !entry39+ .metadata()40+ .map(|m| filesystem::is_executable(&m))41+ .unwrap_or(false))42+ || (self.empty_only && !filesystem::is_empty(&entry))43+ || !(entry_type.is_file()44+ || entry_type.is_dir()45+ || entry_type.is_symlink()46+ || filesystem::is_socket(*entry_type)47+ || filesystem::is_pipe(*entry_type))48+ } else {49+ true50+ }51+ }52+}modified:src/main.rs+1 added-1 removed
18 unmodified lines19 use anyhow::{anyhow, Context, Result};20 use atty::Stream;21 use globset::GlobBuilder;22 use lscolors::LsColors;23-use regex::bytes::{RegexBuilder, RegexSetBuilder};23 use normpath::PathExt;24+use regex::bytes::{RegexBuilder, RegexSetBuilder};25 26 use crate::error::print_error;27 use crate::exec::CommandTemplate;28 use crate::exit_codes::ExitCode;463 unmodified linesmodified:src/walk.rs+1 added-21 removed
414 unmodified lines415 }416 417 // Filter out unwanted file types.418 if let Some(ref file_types) = config.file_types {419- if let Some(ref entry_type) = entry.file_type() {420- if (!file_types.files && entry_type.is_file())421- || (!file_types.directories && entry_type.is_dir())422- || (!file_types.symlinks && entry_type.is_symlink())423- || (!file_types.sockets && filesystem::is_socket(*entry_type))424- || (!file_types.pipes && filesystem::is_pipe(*entry_type))425- || (file_types.executables_only426- && !entry427- .metadata()428- .map(|m| filesystem::is_executable(&m))429- .unwrap_or(false))430- || (file_types.empty_only && !filesystem::is_empty(&entry))431- || !(entry_type.is_file()432- || entry_type.is_dir()433- || entry_type.is_symlink()434- || filesystem::is_socket(*entry_type)435- || filesystem::is_pipe(*entry_type))436- {437- return ignore::WalkState::Continue;438- }439- } else {419+ if file_types.should_ignore(&entry) {420 return ignore::WalkState::Continue;421 }422 }423 64 unmodified lines