Skip to content
Myr

Use clap, change --sensitive to --case-sensitive

Before adoption: this commit came in with the history the project adopted, so no landing records it.

see #33

authored by sharkdp · committed by sharkdp · parent 1adcf76 · Browse files · 4 files+122 added−58 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+122 added−58 removed

  • tests:+6 added−6 removed
  • other:+116 added−52 removed

4 files+122 added−58 removed

modified:src/main.rs+57 added-44 removed
1#[macro_use]2extern crate clap;3extern crate ansi_term;2extern crate getopts;4extern crate atty;5extern crate regex;6extern crate ignore;7
9 unmodified lines
17use std::os::unix::fs::PermissionsExt;18use std::path::{Path, Component};19use std::process;2020use getopts::Options;21use clap::{App, AppSettings, Arg};22use atty::Stream;23use regex::{Regex, RegexBuilder};24use ignore::WalkBuilder;25
175 unmodified lines
201 process::exit(1);202}203204fn main() {204 let args: Vec<String> = env::args().collect();205 let matches =206 App::new("fd")207 .version(crate_version!())208 .usage("fd [FLAGS/OPTIONS] [<pattern>] [<path>]")209 .setting(AppSettings::ColoredHelp)210 .setting(AppSettings::DeriveDisplayOrder)211 .arg(Arg::with_name("case-sensitive")212 .long("case-sensitive")213 .short("s")214 .help("Case-sensitive search (default: smart case)"))215 .arg(Arg::with_name("full-path")216 .long("full-path")217 .short("p")218 .help("Search full path (default: file-/dirname only)"))219 .arg(Arg::with_name("hidden")220 .long("hidden")221 .short("H")222 .help("Search hidden files and directories"))223 .arg(Arg::with_name("no-ignore")224 .long("no-ignore")225 .short("I")226 .help("Do not respect .(git)ignore files"))227 .arg(Arg::with_name("follow")228 .long("follow")229 .short("f")230 .help("Follow symlinks"))231 .arg(Arg::with_name("absolute-path")232 .long("absolute-path")233 .short("a")234 .help("Show absolute instead of relative paths"))235 .arg(Arg::with_name("no-color")236 .long("no-color")237 .short("n")238 .help("Do not colorize output"))239 .arg(Arg::with_name("depth")240 .long("max-depth")241 .short("d")242 .takes_value(true)243 .help("Set maximum search depth (default: none)"))244 .arg(Arg::with_name("pattern")245 .help("the search pattern, a regular expression (optional)"))246 .arg(Arg::with_name("path")247 .help("the root directory for the filesystem search (optional)"))248 .get_matches();249206 let mut opts = Options::new();207 opts.optflag("h", "help",208 "print this help message");209 opts.optflag("s", "sensitive",210 "case-sensitive search (default: smart case)");211 opts.optflag("p", "full-path",212 "search full path (default: file-/dirname only)");213 opts.optflag("H", "hidden",214 "search hidden files/directories");215 opts.optflag("I", "no-ignore",216 "do not respect .(git)ignore files");217 opts.optflag("f", "follow",218 "follow symlinks");219 opts.optflag("n", "no-color",220 "do not colorize output");221 opts.optopt("d", "max-depth",222 "maximum search depth (default: none)", "D");223 opts.optflag("a", "absolute",224 "show absolute instead of relative paths");225226 let matches = match opts.parse(&args[1..]) {227 Ok(m) => m,228 Err(e) => error(e.description())229 };230231 if matches.opt_present("h") {232 let brief = "Usage: fd [options] [PATTERN] [PATH]";233 print!("{}", opts.usage(brief));234 process::exit(1);235 }236250 // Get the search pattern251 let empty_pattern = String::new();239 let pattern = matches.free.get(0).unwrap_or(&empty_pattern);252 let pattern = matches.value_of("pattern").unwrap_or(&empty_pattern);253254 // Get the current working directory255 let current_dir_buf = match env::current_dir() {256 Ok(cd) => cd,
1 unmodified line
258 };259 let current_dir = current_dir_buf.as_path();260261 // Get the root directory for the search249 let root_dir_buf = matches.free.get(1)262 let root_dir_buf = matches.value_of("path")263 .and_then(|r| fs::canonicalize(r).ok())264 .unwrap_or_else(|| current_dir_buf.clone());265 let root_dir = root_dir_buf.as_path();266267 // The search will be case-sensitive if the command line flag is set or268 // if the pattern has an uppercase character (smart case).256 let case_sensitive = matches.opt_present("sensitive") ||269 let case_sensitive = matches.is_present("case-sensitive") ||270 pattern.chars().any(char::is_uppercase);271259 let colored_output = !matches.opt_present("no-color") &&272 let colored_output = !matches.is_present("no-color") &&273 atty::is(Stream::Stdout);274275 let ls_colors =276 if colored_output {
8 unmodified lines
285 };286287 let config = FdOptions {288 case_sensitive: case_sensitive,276 search_full_path: matches.opt_present("full-path"),277 ignore_hidden: !matches.opt_present("hidden"),278 read_ignore: !matches.opt_present("no-ignore"),279 follow_links: matches.opt_present("follow"),280 max_depth: matches.opt_str("max-depth")289 search_full_path: matches.is_present("full-path"),290 ignore_hidden: !matches.is_present("hidden"),291 read_ignore: !matches.is_present("no-ignore"),292 follow_links: matches.is_present("follow"),293 max_depth: matches.value_of("depth")294 .and_then(|ds| usize::from_str_radix(&ds, 10).ok()),282 path_display: if matches.opt_present("absolute") {295 path_display: if matches.is_present("absolute-path") {296 PathDisplay::Absolute297 } else {298 PathDisplay::Relative299 },
16 unmodified lines
modified:tests/test.sh+6 added-6 removed
111 unmodified lines
112one/two/c.foo113one/two/C.Foo" '[a-c].foo'114expect "a.foo115one/b.foo116one/two/c.foo" --sensitive '[a-c].foo'116one/two/c.foo" --case-sensitive '[a-c].foo'117118119120suite "Smart case"
2 unmodified lines
123expect "one/two/C.Foo" C.Foo124expect "one/two/C.Foo" Foo125126127suite "Case-sensitivity (--sensitive)"128expect "one/two/c.foo" --sensitive c.foo129expect "one/two/C.Foo" --sensitive C.Foo127suite "Case-sensitivity (--case-sensitive)"128expect "one/two/c.foo" --case-sensitive c.foo129expect "one/two/C.Foo" --case-sensitive C.Foo130131132suite "Full path search (--full-path)"133expect "one/two/three/d.foo
55 unmodified lines
189one190symlink" --max-depth 1191192193suite "Absolute paths (--absolute)"193suite "Absolute paths (--absolute-path)"194expect "$root/a.foo195$root/one/b.foo196$root/one/two/c.foo197$root/one/two/C.Foo198$root/one/two/three/d.foo199$root/one/two/three/directory_foo" --absolute foo199$root/one/two/three/directory_foo" --absolute-path foo200201# All done202echo
modified:Cargo.lock+58 added-7 removed
2 unmodified lines
3version = "1.1.0"4dependencies = [5 "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",6 "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",7 "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",7 "clap 2.24.2 (registry+https://github.com/rust-lang/crates.io-index)",8 "ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",9 "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",10]11
20 unmodified lines
32 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",33]3435[[package]]36name = "bitflags"37version = "0.8.2"38source = "registry+https://github.com/rust-lang/crates.io-index"3940[[package]]41name = "clap"42version = "2.24.2"43source = "registry+https://github.com/rust-lang/crates.io-index"44dependencies = [45 "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",46 "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",47 "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",48 "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",49 "term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",50 "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",51 "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",52 "vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",53]5455[[package]]56name = "crossbeam"57version = "0.2.10"58source = "registry+https://github.com/rust-lang/crates.io-index"59
2 unmodified lines
62version = "1.0.5"63source = "registry+https://github.com/rust-lang/crates.io-index"6465[[package]]46name = "getopts"47version = "0.2.14"48source = "registry+https://github.com/rust-lang/crates.io-index"4950[[package]]66name = "globset"67version = "0.2.0"68source = "registry+https://github.com/rust-lang/crates.io-index"69dependencies = [
77 unmodified lines
147 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",148]149150[[package]]151name = "strsim"152version = "0.6.0"153source = "registry+https://github.com/rust-lang/crates.io-index"154155[[package]]156name = "term_size"157version = "0.3.0"158source = "registry+https://github.com/rust-lang/crates.io-index"159dependencies = [160 "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",161 "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",162 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",163]164165[[package]]166name = "thread-id"167version = "3.0.0"168source = "registry+https://github.com/rust-lang/crates.io-index"169dependencies = [
10 unmodified lines
180 "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",181]182183[[package]]184name = "unicode-segmentation"185version = "1.2.0"186source = "registry+https://github.com/rust-lang/crates.io-index"187188[[package]]189name = "unicode-width"190version = "0.1.4"191source = "registry+https://github.com/rust-lang/crates.io-index"192193[[package]]194name = "unreachable"195version = "0.1.1"196source = "registry+https://github.com/rust-lang/crates.io-index"197dependencies = [
5 unmodified lines
203version = "1.0.0"204source = "registry+https://github.com/rust-lang/crates.io-index"205206[[package]]207name = "vec_map"208version = "0.8.0"209source = "registry+https://github.com/rust-lang/crates.io-index"210211[[package]]212name = "void"213version = "1.0.2"214source = "registry+https://github.com/rust-lang/crates.io-index"215
20 unmodified lines
236[metadata]237"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"238"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"239"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159"240"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4"241"checksum clap 2.24.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b8f69e518f967224e628896b54e41ff6acfb4dcfefc5076325c36525dac900f"242"checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97"243"checksum fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6cc484842f1e2884faf56f529f960cc12ad8c71ce96cc7abba0a067c98fee344"197"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"244"checksum globset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "feeb1b6840809ef5efcf7a4a990bc4e1b7ee3df8cf9e2379a75aeb2ba42ac9c3"245"checksum ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9bda0eaa44a060e21581315efb449e7fabceb93cf38c8376dd1b7e0623e07844"246"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"247"checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf"
2 unmodified lines
250"checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"251"checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01"252"checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457"253"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"254"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"255"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"256"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"257"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"258"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946"259"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"260"checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"261"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"262"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"263"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"264"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"265"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"266"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
modified:Cargo.toml+1 added-1 removed