diff options
author | exitium <exitium@users.noreply.github.com> | 2017-09-16 20:36:07 -0400 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2017-09-17 09:25:01 +0200 |
commit | 2a3dd5b63181bda6067517d76269d8b3b0204a05 (patch) | |
tree | e13441840f19c70eef279edee30c30b555f9b2ee | |
parent | fbce0d4f393831755a78a180d87c448e7fedd02b (diff) |
Add file type filtering
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | src/main.rs | 41 | ||||
-rwxr-xr-x[-rw-r--r--] | tests/test.sh | 13 |
3 files changed, 53 insertions, 2 deletions
@@ -153,6 +153,7 @@ FLAGS: OPTIONS: -d, --max-depth <depth> Set maximum search depth (default: none) -j, --threads <threads> The number of threads used for searching + -t, --type <type> The type of file to search for [values: f, file, d, directory, s, symlink] ARGS: <pattern> the search pattern, a regular expression (optional) diff --git a/src/main.rs b/src/main.rs index 1928cd2..9e0ef75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,18 @@ struct FdOptions { /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines /// how to style different filetypes. - ls_colors: Option<LsColors> + ls_colors: Option<LsColors>, + + file_type: FileType, +} + +/// The type of file to search for. All files other than the specified type will be ignored. +#[derive(Copy, Clone)] +enum FileType { + Any, + RegularFile, + Directory, + SymLink, } /// The receiver thread can either be buffering results or directly streaming to the console. @@ -274,6 +285,20 @@ fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions>) { Err(_) => return ignore::WalkState::Continue }; + // Filter out unwanted file types. + match config.file_type { + FileType::Any => (), + FileType::RegularFile => if entry.file_type().map_or(false, |ft| !ft.is_file()) { + 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; + }, + } + let path_rel_buf = match fshelper::path_relative_from(entry.path(), &*base) { Some(p) => p, None => error("Error: could not get relative path for directory entry.") @@ -372,6 +397,12 @@ fn main() { .help("the search pattern, a regular expression (optional)")) .arg(Arg::with_name("path") .help("the root directory for the filesystem search (optional)")) + .arg(Arg::with_name("file-type") + .help("The type of file to search for") + .long("type") + .short("t") + .takes_value(true) + .possible_values(&["f", "file", "d", "directory", "s", "symlink"])) .get_matches(); // Get the search pattern @@ -448,7 +479,13 @@ fn main() { } else { PathDisplay::Relative }, - ls_colors: ls_colors + ls_colors: ls_colors, + file_type: match matches.value_of("file-type") { + Some("f") | Some("file") => FileType::RegularFile, + Some("d") | Some("directory") => FileType::Directory, + Some("s") | Some("symlink") => FileType::SymLink, + _ => FileType::Any, + }, }; let root = Path::new(ROOT_DIR); diff --git a/tests/test.sh b/tests/test.sh index 4e07886..5f2e20d 100644..100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -225,5 +225,18 @@ $abs_path/one/two/C.Foo2 $abs_path/one/two/three/d.foo $abs_path/one/two/three/directory_foo" foo "$abs_path" + +suite "File type filter (--type)" +expect "a.foo +one/b.foo +one/two/c.foo +one/two/C.Foo2 +one/two/three/d.foo" --type f +expect "one +one/two +one/two/three +one/two/three/directory_foo" --type d +expect "symlink" --type s + # All done echo |