Implement glob-based searches
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #284
authored by sharkdp · committed by David Peter · parent 641594c · Browse files · 6 files·+104 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
6 files+104 added−4 removed
- tests:+74 added−0 removed
- other:+30 added−4 removed
Provenance
- Commit
d5da615- Tree
8e1fc9b- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
641594c
6 files changed+104 added−4 removed
modified:src/internal/mod.rs+8 added-2 removed
8 unmodified lines9 use std::borrow::Cow;10 use std::ffi::{OsStr, OsString};11 12 use regex_syntax::hir::Hir;13-use regex_syntax::Parser;13+use regex_syntax::ParserBuilder;14 15 pub use self::file_types::FileTypes;16 17 mod file_types;28 unmodified lines46 }47 48 /// Determine if a regex pattern contains a literal uppercase character.49 pub fn pattern_has_uppercase_char(pattern: &str) -> bool {50- Parser::new()50+ let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();51+52+ parser53 .parse(pattern)54 .map(|hir| hir_has_uppercase_char(&hir))55 .unwrap_or(false)56 }3 unmodified lines60 use regex_syntax::hir::*;61 62 match *hir.kind() {63 HirKind::Literal(Literal::Unicode(c)) => c.is_uppercase(),64+ HirKind::Literal(Literal::Byte(b)) => char::from(b).is_uppercase(),65 HirKind::Class(Class::Unicode(ref ranges)) => ranges66 .iter()67 .any(|r| r.start().is_uppercase() || r.end().is_uppercase()),68+ HirKind::Class(Class::Bytes(ref ranges)) => ranges69+ .iter()70+ .any(|r| char::from(r.start()).is_uppercase() || char::from(r.end()).is_uppercase()),71 HirKind::Group(Group { ref hir, .. }) | HirKind::Repetition(Repetition { ref hir, .. }) => {72 hir_has_uppercase_char(hir)73 }74 HirKind::Concat(ref hirs) | HirKind::Alternation(ref hirs) => {123 unmodified linesmodified:src/app.rs+9 added
66 unmodified lines67 .short("i")68 .overrides_with("case-sensitive"),69 )70 .arg(71+ arg("glob")72+ .long("glob")73+ .short("g")74+ .conflicts_with("fixed-strings"),75+ )76+ .arg(77 arg("fixed-strings")78 .long("fixed-strings")79 .short("F")80 .alias("literal"),174 unmodified lines255 doc!(h, "ignore-case"256 , "Case-insensitive search (default: smart case)"257 , "Perform a case-insensitive search. By default, fd uses case-insensitive searches, \258 unless the pattern contains an uppercase character (smart case).");259+ doc!(h, "glob"260+ , "Glob-based search (default: regular expression)"261+ , "Perform a glob-based search instead of a regular expression search.");262 doc!(h, "fixed-strings"263 , "Treat the pattern as a literal string"264 , "Treat the pattern as a literal string instead of a regular expression.");265 doc!(h, "absolute-path"134 unmodified linesmodified:src/main.rs+11 added-2 removed
22 unmodified lines23 use std::sync::Arc;24 use std::time;25 26 use atty::Stream;27+use globset::Glob;28 use lscolors::LsColors;29 use regex::bytes::{RegexBuilder, RegexSetBuilder};30 31 use crate::exec::CommandTemplate;64 unmodified lines96 sep = std::path::MAIN_SEPARATOR,97 );98 }99 99- // Treat pattern as literal string if '--fixed-strings' is used100- let pattern_regex = if matches.is_present("fixed-strings") {100+ let pattern_regex = if matches.is_present("glob") {101+ let glob = match Glob::new(pattern) {102+ Ok(glob) => glob,103+ Err(e) => {104+ print_error_and_exit!("{}", e);105+ }106+ };107+ glob.regex().to_owned()108+ } else if matches.is_present("fixed-strings") {109+ // Treat pattern as literal string if '--fixed-strings' is used110 regex::escape(pattern)111 } else {112 String::from(pattern)113 };165 unmodified linesmodified:tests/tests.rs+74 added
237 unmodified lines238 one/two/C.Foo2",239 );240 }241 242+/// Glob-based searches (--glob)243+#[test]244+fn test_glob_searches() {245+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);246+247+ te.assert_output(248+ &["--glob", "*.foo"],249+ "a.foo250+ one/b.foo251+ one/two/c.foo252+ one/two/three/d.foo",253+ );254+255+ te.assert_output(256+ &["--glob", "[a-c].foo"],257+ "a.foo258+ one/b.foo259+ one/two/c.foo",260+ );261+262+ te.assert_output(263+ &["--glob", "[a-c].foo*"],264+ "a.foo265+ one/b.foo266+ one/two/C.Foo2267+ one/two/c.foo",268+ );269+}270+271+/// Glob-based searches (--glob) in combination with full path searches (--full-path)272+#[test]273+fn test_full_path_glob_searches() {274+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);275+276+ te.assert_output(277+ &["--glob", "--full-path", "**/one/**/*.foo"],278+ "one/b.foo279+ one/two/c.foo280+ one/two/three/d.foo",281+ );282+}283+284+#[test]285+fn test_smart_case_glob_searches() {286+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);287+288+ te.assert_output(289+ &["--glob", "c.foo*"],290+ "one/two/C.Foo2291+ one/two/c.foo",292+ );293+294+ te.assert_output(&["--glob", "C.Foo*"], "one/two/C.Foo2");295+}296+297+/// Glob-based searches (--glob) in combination with --case-sensitive298+#[test]299+fn test_case_sensitive_glob_searches() {300+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);301+302+ te.assert_output(&["--glob", "--case-sensitive", "c.foo*"], "one/two/c.foo");303+}304+305+/// Glob-based searches (--glob) in combination with --extension306+#[test]307+fn test_glob_searches_with_extension() {308+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);309+310+ te.assert_output(311+ &["--glob", "--extension", "foo2", "[a-z].*"],312+ "one/two/C.Foo2",313+ );314+}315+316 /// Full path search (--full-path)317 #[test]318 fn test_full_path() {319 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1024 unmodified linesmodified:Cargo.lock+1 added
102 unmodified lines103 "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",104 "ctrlc 3.1.3 (registry+https://github.com/rust-lang/crates.io-index)",105 "diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",106 "filetime 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",107+ "globset 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",108 "humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",109 "ignore 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",110 "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",111 "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",362 unmodified linesmodified:Cargo.toml+1 added
41 unmodified lines42434445464748495012 unmodified lines41 unmodified linesregex-syntax = "0.6"ctrlc = "3.1"humantime = "1.1.1"lscolors = "0.5"globset = "0.4"
[dependencies.clap]version = "2.31.2"features = ["suggestions", "color", "wrap_help"]12 unmodified lines