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

see #33
This commit is contained in:
sharkdp 2017-06-11 14:15:25 +02:00
parent 1adcf76f1b
commit 087e709acd
4 changed files with 122 additions and 58 deletions

65
Cargo.lock generated
View File

@ -4,7 +4,7 @@ version = "1.1.0"
dependencies = [ dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.24.2 (registry+https://github.com/rust-lang/crates.io-index)",
"ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -32,6 +32,26 @@ dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "bitflags"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clap"
version = "2.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "crossbeam" name = "crossbeam"
version = "0.2.10" version = "0.2.10"
@ -42,11 +62,6 @@ name = "fnv"
version = "1.0.5" version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "getopts"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "globset" name = "globset"
version = "0.2.0" version = "0.2.0"
@ -132,6 +147,21 @@ dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "strsim"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "term_size"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "thread-id" name = "thread-id"
version = "3.0.0" version = "3.0.0"
@ -150,6 +180,16 @@ dependencies = [
"unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "unicode-segmentation"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-width"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "unreachable" name = "unreachable"
version = "0.1.1" version = "0.1.1"
@ -163,6 +203,11 @@ name = "utf8-ranges"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "void" name = "void"
version = "1.0.2" version = "1.0.2"
@ -192,9 +237,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" "checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159"
"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4"
"checksum clap 2.24.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b8f69e518f967224e628896b54e41ff6acfb4dcfefc5076325c36525dac900f"
"checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97" "checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97"
"checksum fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6cc484842f1e2884faf56f529f960cc12ad8c71ce96cc7abba0a067c98fee344" "checksum fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6cc484842f1e2884faf56f529f960cc12ad8c71ce96cc7abba0a067c98fee344"
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
"checksum globset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "feeb1b6840809ef5efcf7a4a990bc4e1b7ee3df8cf9e2379a75aeb2ba42ac9c3" "checksum globset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "feeb1b6840809ef5efcf7a4a990bc4e1b7ee3df8cf9e2379a75aeb2ba42ac9c3"
"checksum ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9bda0eaa44a060e21581315efb449e7fabceb93cf38c8376dd1b7e0623e07844" "checksum ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9bda0eaa44a060e21581315efb449e7fabceb93cf38c8376dd1b7e0623e07844"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
@ -205,10 +251,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01" "checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01"
"checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457" "checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457"
"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7" "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"
"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"
"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"
"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"
"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7" "checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"
"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff" "checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"

View File

@ -5,7 +5,7 @@ authors = ["David Peter <mail@david-peter.de>"]
[dependencies] [dependencies]
ansi_term = "0.9" ansi_term = "0.9"
getopts = "0.2" clap = "2.24.2"
atty = "0.2" atty = "0.2"
regex = "0.2" regex = "0.2"
ignore = "0.2" ignore = "0.2"

View File

@ -1,5 +1,6 @@
#[macro_use]
extern crate clap;
extern crate ansi_term; extern crate ansi_term;
extern crate getopts;
extern crate atty; extern crate atty;
extern crate regex; extern crate regex;
extern crate ignore; extern crate ignore;
@ -17,7 +18,7 @@ use std::os::unix::fs::PermissionsExt;
use std::path::{Path, Component}; use std::path::{Path, Component};
use std::process; use std::process;
use getopts::Options; use clap::{App, AppSettings, Arg};
use atty::Stream; use atty::Stream;
use regex::{Regex, RegexBuilder}; use regex::{Regex, RegexBuilder};
use ignore::WalkBuilder; use ignore::WalkBuilder;
@ -201,42 +202,54 @@ fn error(message: &str) -> ! {
} }
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let matches =
App::new("fd")
let mut opts = Options::new(); .version(crate_version!())
opts.optflag("h", "help", .usage("fd [FLAGS/OPTIONS] [<pattern>] [<path>]")
"print this help message"); .setting(AppSettings::ColoredHelp)
opts.optflag("s", "sensitive", .setting(AppSettings::DeriveDisplayOrder)
"case-sensitive search (default: smart case)"); .arg(Arg::with_name("case-sensitive")
opts.optflag("p", "full-path", .long("case-sensitive")
"search full path (default: file-/dirname only)"); .short("s")
opts.optflag("H", "hidden", .help("Case-sensitive search (default: smart case)"))
"search hidden files/directories"); .arg(Arg::with_name("full-path")
opts.optflag("I", "no-ignore", .long("full-path")
"do not respect .(git)ignore files"); .short("p")
opts.optflag("f", "follow", .help("Search full path (default: file-/dirname only)"))
"follow symlinks"); .arg(Arg::with_name("hidden")
opts.optflag("n", "no-color", .long("hidden")
"do not colorize output"); .short("H")
opts.optopt("d", "max-depth", .help("Search hidden files and directories"))
"maximum search depth (default: none)", "D"); .arg(Arg::with_name("no-ignore")
opts.optflag("a", "absolute", .long("no-ignore")
"show absolute instead of relative paths"); .short("I")
.help("Do not respect .(git)ignore files"))
let matches = match opts.parse(&args[1..]) { .arg(Arg::with_name("follow")
Ok(m) => m, .long("follow")
Err(e) => error(e.description()) .short("f")
}; .help("Follow symlinks"))
.arg(Arg::with_name("absolute-path")
if matches.opt_present("h") { .long("absolute-path")
let brief = "Usage: fd [options] [PATTERN] [PATH]"; .short("a")
print!("{}", opts.usage(brief)); .help("Show absolute instead of relative paths"))
process::exit(1); .arg(Arg::with_name("no-color")
} .long("no-color")
.short("n")
.help("Do not colorize output"))
.arg(Arg::with_name("depth")
.long("max-depth")
.short("d")
.takes_value(true)
.help("Set maximum search depth (default: none)"))
.arg(Arg::with_name("pattern")
.help("the search pattern, a regular expression (optional)"))
.arg(Arg::with_name("path")
.help("the root directory for the filesystem search (optional)"))
.get_matches();
// Get the search pattern // Get the search pattern
let empty_pattern = String::new(); let empty_pattern = String::new();
let pattern = matches.free.get(0).unwrap_or(&empty_pattern); let pattern = matches.value_of("pattern").unwrap_or(&empty_pattern);
// Get the current working directory // Get the current working directory
let current_dir_buf = match env::current_dir() { let current_dir_buf = match env::current_dir() {
@ -246,17 +259,17 @@ fn main() {
let current_dir = current_dir_buf.as_path(); let current_dir = current_dir_buf.as_path();
// Get the root directory for the search // Get the root directory for the search
let root_dir_buf = matches.free.get(1) let root_dir_buf = matches.value_of("path")
.and_then(|r| fs::canonicalize(r).ok()) .and_then(|r| fs::canonicalize(r).ok())
.unwrap_or_else(|| current_dir_buf.clone()); .unwrap_or_else(|| current_dir_buf.clone());
let root_dir = root_dir_buf.as_path(); let root_dir = root_dir_buf.as_path();
// The search will be case-sensitive if the command line flag is set or // The search will be case-sensitive if the command line flag is set or
// if the pattern has an uppercase character (smart case). // if the pattern has an uppercase character (smart case).
let case_sensitive = matches.opt_present("sensitive") || let case_sensitive = matches.is_present("case-sensitive") ||
pattern.chars().any(char::is_uppercase); pattern.chars().any(char::is_uppercase);
let colored_output = !matches.opt_present("no-color") && let colored_output = !matches.is_present("no-color") &&
atty::is(Stream::Stdout); atty::is(Stream::Stdout);
let ls_colors = let ls_colors =
@ -273,13 +286,13 @@ fn main() {
let config = FdOptions { let config = FdOptions {
case_sensitive: case_sensitive, case_sensitive: case_sensitive,
search_full_path: matches.opt_present("full-path"), search_full_path: matches.is_present("full-path"),
ignore_hidden: !matches.opt_present("hidden"), ignore_hidden: !matches.is_present("hidden"),
read_ignore: !matches.opt_present("no-ignore"), read_ignore: !matches.is_present("no-ignore"),
follow_links: matches.opt_present("follow"), follow_links: matches.is_present("follow"),
max_depth: matches.opt_str("max-depth") max_depth: matches.value_of("depth")
.and_then(|ds| usize::from_str_radix(&ds, 10).ok()), .and_then(|ds| usize::from_str_radix(&ds, 10).ok()),
path_display: if matches.opt_present("absolute") { path_display: if matches.is_present("absolute-path") {
PathDisplay::Absolute PathDisplay::Absolute
} else { } else {
PathDisplay::Relative PathDisplay::Relative

View File

@ -113,7 +113,7 @@ one/two/c.foo
one/two/C.Foo" '[a-c].foo' one/two/C.Foo" '[a-c].foo'
expect "a.foo expect "a.foo
one/b.foo one/b.foo
one/two/c.foo" --sensitive '[a-c].foo' one/two/c.foo" --case-sensitive '[a-c].foo'
@ -124,9 +124,9 @@ expect "one/two/C.Foo" C.Foo
expect "one/two/C.Foo" Foo expect "one/two/C.Foo" Foo
suite "Case-sensitivity (--sensitive)" suite "Case-sensitivity (--case-sensitive)"
expect "one/two/c.foo" --sensitive c.foo expect "one/two/c.foo" --case-sensitive c.foo
expect "one/two/C.Foo" --sensitive C.Foo expect "one/two/C.Foo" --case-sensitive C.Foo
suite "Full path search (--full-path)" suite "Full path search (--full-path)"
@ -190,13 +190,13 @@ one
symlink" --max-depth 1 symlink" --max-depth 1
suite "Absolute paths (--absolute)" suite "Absolute paths (--absolute-path)"
expect "$root/a.foo expect "$root/a.foo
$root/one/b.foo $root/one/b.foo
$root/one/two/c.foo $root/one/two/c.foo
$root/one/two/C.Foo $root/one/two/C.Foo
$root/one/two/three/d.foo $root/one/two/three/d.foo
$root/one/two/three/directory_foo" --absolute foo $root/one/two/three/directory_foo" --absolute-path foo
# All done # All done
echo echo