mirror of https://github.com/sharkdp/fd.git
Configurable search depth, closes #13
This commit is contained in:
parent
7076dcbc68
commit
e43353a0bb
11
src/main.rs
11
src/main.rs
|
@ -20,7 +20,8 @@ struct FdOptions {
|
||||||
search_full_path: bool,
|
search_full_path: bool,
|
||||||
search_hidden: bool,
|
search_hidden: bool,
|
||||||
follow_links: bool,
|
follow_links: bool,
|
||||||
colored: bool
|
colored: bool,
|
||||||
|
max_depth: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Print a search result to the console.
|
/// Print a search result to the console.
|
||||||
|
@ -50,6 +51,7 @@ fn is_hidden(entry: &DirEntry) -> bool {
|
||||||
fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {
|
fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {
|
||||||
let walker = WalkDir::new(root)
|
let walker = WalkDir::new(root)
|
||||||
.follow_links(config.follow_links)
|
.follow_links(config.follow_links)
|
||||||
|
.max_depth(config.max_depth)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_entry(|e| config.search_hidden || !is_hidden(e))
|
.filter_entry(|e| config.search_hidden || !is_hidden(e))
|
||||||
.filter_map(|e| e.ok())
|
.filter_map(|e| e.ok())
|
||||||
|
@ -98,6 +100,7 @@ fn main() {
|
||||||
"search hidden files/directories (default: off)");
|
"search hidden files/directories (default: off)");
|
||||||
opts.optflag("F", "follow", "follow symlinks (default: off)");
|
opts.optflag("F", "follow", "follow symlinks (default: off)");
|
||||||
opts.optflag("n", "no-color", "do not colorize output");
|
opts.optflag("n", "no-color", "do not colorize output");
|
||||||
|
opts.optopt("d", "max-depth", "maximum search depth", "DEPTH");
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
|
@ -128,7 +131,11 @@ fn main() {
|
||||||
search_full_path: !matches.opt_present("filename"),
|
search_full_path: !matches.opt_present("filename"),
|
||||||
search_hidden: matches.opt_present("hidden"),
|
search_hidden: matches.opt_present("hidden"),
|
||||||
colored: !matches.opt_present("no-color"),
|
colored: !matches.opt_present("no-color"),
|
||||||
follow_links: matches.opt_present("follow")
|
follow_links: matches.opt_present("follow"),
|
||||||
|
max_depth:
|
||||||
|
matches.opt_str("max-depth")
|
||||||
|
.and_then(|ds| usize::from_str_radix(&ds, 10).ok())
|
||||||
|
.unwrap_or(25)
|
||||||
};
|
};
|
||||||
|
|
||||||
match RegexBuilder::new(pattern)
|
match RegexBuilder::new(pattern)
|
||||||
|
|
Loading…
Reference in New Issue