Skip to content
Myr

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

3 files+53 added−2 removed

modified:src/main.rs+39 added-2 removed
78 unmodified lines
79 path_display: PathDisplay,8081 /// `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>,8485 file_type: FileType,86}8788/// The type of file to search for. All files other than the specified type will be ignored.89#[derive(Copy, Clone)]90enum FileType {91 Any,92 RegularFile,93 Directory,94 SymLink,95}9697/// The receiver thread can either be buffering results or directly streaming to the console.98enum ReceiverMode {99 /// Receiver is still buffering in order to sort the results, if the search finishes fast100 /// enough.
183 unmodified lines
284 Ok(e) => e,285 Err(_) => return ignore::WalkState::Continue286 };287288 // 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 }301302 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 lines
396 .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();407408 // Get the search pattern409 let empty_pattern = String::new();
68 unmodified lines
478 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 };490491 let root = Path::new(ROOT_DIR);492 let base = match config.path_display {
11 unmodified lines
modified:tests/test.sh+13 added
223 unmodified lines
224$abs_path/one/two/C.Foo2225$abs_path/one/two/three/d.foo226$abs_path/one/two/three/directory_foo" foo "$abs_path"227228229suite "File type filter (--type)"230expect "a.foo231one/b.foo232one/two/c.foo233one/two/C.Foo2234one/two/three/d.foo" --type f235expect "one236one/two237one/two/three238one/two/three/directory_foo" --type d239expect "symlink" --type s240241# All done242echo
modified:README.md+1 added