Skip to content

Commit

Permalink
Refactor file types check to be on impl of FileTypes
Browse files Browse the repository at this point in the history
Relates to #382
  • Loading branch information
tmccombs authored and sharkdp committed Aug 10, 2021
1 parent c06c995 commit 115ae93
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
28 changes: 28 additions & 0 deletions src/filetypes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::filesystem;
use crate::walk;

/// Whether or not to show
pub struct FileTypes {
pub files: bool,
Expand All @@ -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
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 1 addition & 21 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 115ae93

Please sign in to comment.