Add file type filtering
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by exitium · committed by David Peter · parent fbce0d4 · Browse files · 3 files·+53 added−2 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+53 added−2 removed
- tests:+13 added−0 removed
- other:+40 added−2 removed
Provenance
- Commit
2a3dd5b- Tree
e134418- Author
exitium <exitium@users.noreply.github.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
fbce0d4
3 files changed+53 added−2 removed
modified:src/main.rs+39 added-2 removed
78 unmodified lines79 path_display: PathDisplay,80 81 /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines82 /// how to style different filetypes.83- ls_colors: Option<LsColors>83+ ls_colors: Option<LsColors>,84+85+ file_type: FileType,86 }87 88+/// The type of file to search for. All files other than the specified type will be ignored.89+#[derive(Copy, Clone)]90+enum FileType {91+ Any,92+ RegularFile,93+ Directory,94+ SymLink,95+}96+97 /// The receiver thread can either be buffering results or directly streaming to the console.98 enum ReceiverMode {99 /// Receiver is still buffering in order to sort the results, if the search finishes fast100 /// enough.183 unmodified lines284 Ok(e) => e,285 Err(_) => return ignore::WalkState::Continue286 };287 288+ // Filter out unwanted file types.289+ match config.file_type {290+ FileType::Any => (),291+ FileType::RegularFile => if entry.file_type().map_or(false, |ft| !ft.is_file()) {292+ return ignore::WalkState::Continue;293+ },294+ FileType::Directory => if entry.file_type().map_or(false, |ft| !ft.is_dir()) {295+ return ignore::WalkState::Continue;296+ },297+ FileType::SymLink => if entry.file_type().map_or(false, |ft| !ft.is_symlink()) {298+ return ignore::WalkState::Continue;299+ },300+ }301+302 let path_rel_buf = match fshelper::path_relative_from(entry.path(), &*base) {303 Some(p) => p,304 None => error("Error: could not get relative path for directory entry.")305 };90 unmodified lines396 .arg(Arg::with_name("pattern")397 .help("the search pattern, a regular expression (optional)"))398 .arg(Arg::with_name("path")399 .help("the root directory for the filesystem search (optional)"))400+ .arg(Arg::with_name("file-type")401+ .help("The type of file to search for")402+ .long("type")403+ .short("t")404+ .takes_value(true)405+ .possible_values(&["f", "file", "d", "directory", "s", "symlink"]))406 .get_matches();407 408 // Get the search pattern409 let empty_pattern = String::new();68 unmodified lines478 PathDisplay::Absolute479 } else {480 PathDisplay::Relative481 },451- ls_colors: ls_colors482+ ls_colors: ls_colors,483+ file_type: match matches.value_of("file-type") {484+ Some("f") | Some("file") => FileType::RegularFile,485+ Some("d") | Some("directory") => FileType::Directory,486+ Some("s") | Some("symlink") => FileType::SymLink,487+ _ => FileType::Any,488+ },489 };490 491 let root = Path::new(ROOT_DIR);492 let base = match config.path_display {11 unmodified linesmodified:tests/test.sh+13 added
223 unmodified lines224 $abs_path/one/two/C.Foo2225 $abs_path/one/two/three/d.foo226 $abs_path/one/two/three/directory_foo" foo "$abs_path"227 228+229+suite "File type filter (--type)"230+expect "a.foo231+one/b.foo232+one/two/c.foo233+one/two/C.Foo2234+one/two/three/d.foo" --type f235+expect "one236+one/two237+one/two/three238+one/two/three/directory_foo" --type d239+expect "symlink" --type s240+241 # All done242 echomodified:README.md+1 added
151 unmodified linesExpand all152153154155156157158159160161151 unmodified linesExpand all
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) <path> the root directory for the filesystem search (optional)```