Add extension filtering
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matthias Reitinger · committed by David Peter · parent 8073635 · Browse files · 3 files·+41 added−4 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+41 added−4 removed
- tests:+12 added−0 removed
- other:+29 added−4 removed
Provenance
- Commit
83db867- Tree
8f3958a- Author
Matthias Reitinger <reima@edenprime.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
8073635
3 files changed+41 added−4 removed
modified:src/main.rs+23 added
91 unmodified lines92 ls_colors: Option<LsColors>,93 94 /// The type of file to search for. All files other than the specified type will be ignored.95 file_type: FileType,96+97+ /// The extension to search for. Only entries matching the extension will be included.98+ ///99+ /// The value (if present) will be a lowercase string without leading dots.100+ extension: Option<String>,101 }102 103 /// The receiver thread can either be buffering results or directly streaming to the console.104 enum ReceiverMode {200 unmodified lines305 return ignore::WalkState::Continue;306 },307 }308 309+ // Filter out unwanted extensions.310+ match (&config.extension, entry.path().extension()) {311+ (&None, _) => (),312+ (&Some(_), None) => return ignore::WalkState::Continue,313+ (&Some(ref e1), Some(e2)) => {314+ if e1 != &e2.to_string_lossy().to_lowercase() {315+ return ignore::WalkState::Continue;316+ }317+ },318+ }319+320 let path_rel_buf = match fshelper::path_relative_from(entry.path(), &*base) {321 Some(p) => p,322 None => error("Error: could not get relative path for directory entry.")323 };96 unmodified lines420 .long("type")421 .short("t")422 .takes_value(true)423 .possible_values(&["f", "file", "d", "directory", "s", "symlink"]))424+ .arg(Arg::with_name("extension")425+ .help("The file extension to search for")426+ .long("extension")427+ .short("e")428+ .takes_value(true))429 .get_matches();430 431 // Get the search pattern432 let empty_pattern = String::new();75 unmodified lines508 Some("d") | Some("directory") => FileType::Directory,509 Some("s") | Some("symlink") => FileType::SymLink,510 _ => FileType::Any,511 },512+ extension: matches.value_of("extension")513+ .map(|e| e.trim_left_matches('.').to_lowercase()),514 };515 516 let root = Path::new(ROOT_DIR);517 let base = match config.path_display {11 unmodified linesmodified:tests/test.sh+12 added
236 unmodified lines237 one/two/three238 one/two/three/directory_foo" --type d239 expect "symlink" --type s240 241+242+suite "File extension (--ext)"243+expect "a.foo244+one/b.foo245+one/two/c.foo246+one/two/three/d.foo" --extension foo247+expect "a.foo248+one/b.foo249+one/two/c.foo250+one/two/three/d.foo" --extension .foo251+expect "one/two/C.Foo2" --extension foo2252+253 # All done254 echomodified:README.md+6 added-4 removed
151 unmodified linesExpand all152153154155156157158159156157158159160161162163164165166151 unmodified linesExpand all -h, --help Prints help information -V, --version Prints version information
OPTIONS: -d, --max-depth <depth> Set maximum search depth (default: none) -j, --threads <threads> The number of threads used for searching -t, --type <file-type> The type of file to search for [values: f, file, d, directory, s, symlink] -d, --max-depth <depth> Set maximum search depth (default: none) -j, --threads <threads> The number of threads used for searching -t, --type <file-type> The type of file to search for [values: f, file, d, directory, s, symlink] -e, --extension <extension> The file extension to search for
ARGS: <pattern> the search pattern, a regular expression (optional) <path> the root directory for the filesystem search (optional)```