Added support for filtering by multiple filetypes and extensions (#205)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #177 Closes #199
authored by Thejaswi Kadur · committed by David Peter · parent 4d66c84 · Browse files · 5 files·+62 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+62 added−34 removed
- tests:+28 added−0 removed
- other:+34 added−34 removed
Provenance
- Commit
faf934d- Tree
0686692- Author
Thejaswi Kadur <tkadur@users.noreply.github.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
4d66c84
5 files changed+62 added−34 removed
modified:src/app.rs+7 added-2 removed
68 unmodified lines69 .arg(70 arg("file-type")71 .long("type")72 .short("t")73+ .multiple(true)74+ .number_of_values(1)75 .takes_value(true)76 .value_name("filetype")77 .possible_values(&["f", "file", "d", "directory", "l", "symlink"])78 .hide_possible_values(true),1 unmodified line80 .arg(81 arg("extension")82 .long("extension")83 .short("e")84+ .multiple(true)85+ .number_of_values(1)86 .takes_value(true)87 .value_name("ext"),88 )89 .arg(84 unmodified lines174 , "Limit the directory traversal to a given depth. By default, there is no limit \175 on the search depth.");176 doc!(h, "file-type"177 , "Filter by type: f(ile), d(irectory), (sym)l(ink)"174- , "Filter the search by type:\n \178+ , "Filter the search by type (multiple allowable filetypes can be specified):\n \179 'f' or 'file': regular files\n \180 'd' or 'directory': directories\n \181 'l' or 'symlink': symbolic links");182 doc!(h, "extension"183 , "Filter by file extension"180- , "(Additionally) filter search results by their file extension.");184+ , "(Additionally) filter search results by their file extension. Multiple allowable file \185+ extensions can be specified.");186 doc!(h, "exec"187 , "Execute a command for each search result"188 , "Execute a command for each search result.\n\189 All arguments following --exec are taken to be arguments to the command until the \38 unmodified linesmodified:src/internal.rs+3 added-2 removed
4 unmodified lines5 // at your option. All files in the project carrying such6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9+use std::collections::HashSet;10 use std::process;11 use std::time;12 use std::io::Write;13 44 unmodified lines58 /// how to style different filetypes.59 pub ls_colors: Option<LsColors>,60 61 /// The type of file to search for. All files other than the specified type will be ignored.61- pub file_type: FileType,62+ pub file_types: HashSet<FileType>,63 64 /// The extension to search for. Only entries matching the extension will be included.65 ///66 /// The value (if present) will be a lowercase string without leading dots.66- pub extension: Option<String>,67+ pub extensions: Option<HashSet<String>>,68 69 /// If a value is supplied, each item found will be used to generate and execute commands.70 pub command: Option<CommandTemplate>,71 34 unmodified linesmodified:src/main.rs+13 added-8 removed
134 unmodified lines135 .value_of("max-buffer-time")136 .and_then(|n| u64::from_str_radix(n, 10).ok())137 .map(time::Duration::from_millis),138 ls_colors,139- file_type: match matches.value_of("file-type") {140- Some("f") | Some("file") => FileType::RegularFile,141- Some("d") |142- Some("directory") => FileType::Directory,143- Some("l") | Some("symlink") => FileType::SymLink,144- _ => FileType::Any,139+ file_types: match matches.values_of("file-type") {140+ None => vec![FileType::RegularFile,141+ FileType::Directory,142+ FileType::SymLink]143+ .into_iter().collect(),144+ Some(values) => values.map(|value| match value {145+ "f" | "file" => FileType::RegularFile,146+ "d" | "directory" => FileType::Directory,147+ "l" | "symlink" => FileType::SymLink,148+ _ => FileType::RegularFile,149+ }).collect()150 },146- extension: matches.value_of("extension").map(|e| {147- e.trim_left_matches('.').to_lowercase()151+ extensions: matches.values_of("extension").map(|exts| {152+ exts.map(|e| e.trim_left_matches('.').to_lowercase()).collect()153 }),154 command,155 exclude_patterns: matches156 .values_of("exclude")13 unmodified linesmodified:src/walk.rs+11 added-22 removed
32 unmodified lines33 /// Receiver is directly printing results to the output.34 Streaming,35 }36 37-/// The type of file to search for.38-#[derive(Copy, Clone)]37+/// The types of file to search for.38+#[derive(Copy, Clone, PartialEq, Eq, Hash)]39 pub enum FileType {40- Any,40 RegularFile,41 Directory,42 SymLink,43 }145 unmodified lines189 return ignore::WalkState::Continue;190 }191 192 // Filter out unwanted file types.194- match config.file_type {195- FileType::Any => (),196- FileType::RegularFile => {197- if entry.file_type().map_or(true, |ft| !ft.is_file()) {198- return ignore::WalkState::Continue;199- }200- }201- FileType::Directory => {202- if entry.file_type().map_or(true, |ft| !ft.is_dir()) {203- return ignore::WalkState::Continue;204- }205- }206- FileType::SymLink => {207- if entry.file_type().map_or(true, |ft| !ft.is_symlink()) {208- return ignore::WalkState::Continue;209- }210- }193+ if (entry.file_type().map_or(false, |ft| ft.is_file()) &&194+ !config.file_types.contains(&FileType::RegularFile)) ||195+ (entry.file_type().map_or(false, |ft| ft.is_dir()) &&196+ !config.file_types.contains(&FileType::Directory)) ||197+ (entry.file_type().map_or(false, |ft| ft.is_symlink()) &&198+ !config.file_types.contains(&FileType::SymLink)) {199+ return ignore::WalkState::Continue;200 }201 202 // Filter out unwanted extensions.214- if let Some(ref filter_ext) = config.extension {203+ if let Some(ref filter_exts) = config.extensions {204 let entry_ext = entry_path.extension().map(205 |e| e.to_string_lossy().to_lowercase(),206 );218- if entry_ext.map_or(true, |ext| ext != *filter_ext) {207+ if entry_ext.map_or(true, |ext| !filter_exts.contains(&ext)) {208 return ignore::WalkState::Continue;209 }210 }211 27 unmodified linesmodified:tests/tests.rs+28 added
469 unmodified lines470 one/two/three/d.foo",471 );472 473 te.assert_output(474+ &["--type", "f", "e1"],475+ "e1 e2",476+ );477+478+ te.assert_output(479 &["--type", "d"],480 "one481 one/two482 one/two/three483 one/two/three/directory_foo",484 );485 486+ te.assert_output(487+ &["--type", "d", "--type", "l"],488+ "one489+ one/two490+ one/two/three491+ one/two/three/directory_foo492+ symlink",493+ );494+495 te.assert_output(&["--type", "l"], "symlink");496 }497 498 /// File extension (--extension)16 unmodified lines515 one/two/c.foo516 one/two/three/d.foo",517 );518 519+ te.assert_output(520+ &["--extension", ".foo", "--extension", "foo2"],521+ "a.foo522+ one/b.foo523+ one/two/c.foo524+ one/two/three/d.foo525+ one/two/C.Foo2",526+ );527+528+ te.assert_output(529+ &["--extension", ".foo", "a"],530+ "a.foo",531+ );532+533 te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");534 }535 536 /// Symlinks misc224 unmodified lines