diff --git a/README.md b/README.md index 6ce5076..b4199b9 100644 --- a/README.md +++ b/README.md @@ -153,10 +153,12 @@ FLAGS: -V, --version Prints version information OPTIONS: - -d, --max-depth Set maximum search depth (default: none) - -j, --threads The number of threads used for searching - -t, --type The type of file to search for [values: f, file, - d, directory, s, symlink] + -d, --max-depth Set maximum search depth (default: none) + -j, --threads The number of threads used for searching + -t, --type + The type of file to search for [values: f, file, + d, directory, s, symlink] + -e, --extension The file extension to search for ARGS: the search pattern, a regular expression (optional) diff --git a/src/main.rs b/src/main.rs index 3bc4742..b260b52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,6 +93,11 @@ struct FdOptions { /// The type of file to search for. All files other than the specified type will be ignored. file_type: FileType, + + /// The extension to search for. Only entries matching the extension will be included. + /// + /// The value (if present) will be a lowercase string without leading dots. + extension: Option, } /// The receiver thread can either be buffering results or directly streaming to the console. @@ -301,6 +306,17 @@ fn scan(root: &Path, pattern: Arc, base: &Path, config: Arc) { }, } + // Filter out unwanted extensions. + match (&config.extension, entry.path().extension()) { + (&None, _) => (), + (&Some(_), None) => return ignore::WalkState::Continue, + (&Some(ref e1), Some(e2)) => { + if e1 != &e2.to_string_lossy().to_lowercase() { + 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.") @@ -405,6 +421,11 @@ fn main() { .short("t") .takes_value(true) .possible_values(&["f", "file", "d", "directory", "s", "symlink"])) + .arg(Arg::with_name("extension") + .help("The file extension to search for") + .long("extension") + .short("e") + .takes_value(true)) .get_matches(); // Get the search pattern @@ -488,6 +509,8 @@ fn main() { Some("s") | Some("symlink") => FileType::SymLink, _ => FileType::Any, }, + extension: matches.value_of("extension") + .map(|e| e.trim_left_matches('.').to_lowercase()), }; let root = Path::new(ROOT_DIR); diff --git a/tests/test.sh b/tests/test.sh index 5f2e20d..4bcbbf3 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -238,5 +238,17 @@ one/two/three one/two/three/directory_foo" --type d expect "symlink" --type s + +suite "File extension (--ext)" +expect "a.foo +one/b.foo +one/two/c.foo +one/two/three/d.foo" --extension foo +expect "a.foo +one/b.foo +one/two/c.foo +one/two/three/d.foo" --extension .foo +expect "one/two/C.Foo2" --extension foo2 + # All done echo