mirror of https://github.com/sharkdp/fd.git
Code-cleanup
This commit is contained in:
parent
085c8dd291
commit
e20bd9d3f8
48
src/main.rs
48
src/main.rs
|
@ -4,10 +4,11 @@ extern crate getopts;
|
||||||
extern crate ansi_term;
|
extern crate ansi_term;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::process;
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::ffi::OsStr;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
use walkdir::{WalkDir, DirEntry, WalkDirIterator};
|
use walkdir::{WalkDir, DirEntry, WalkDirIterator};
|
||||||
use regex::{Regex, RegexBuilder};
|
use regex::{Regex, RegexBuilder};
|
||||||
|
@ -43,32 +44,30 @@ fn is_hidden(entry: &DirEntry) -> bool {
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Recursively scan the given root path and search for pathnames matching the
|
/// Recursively scan the given root path and search for files / pathnames
|
||||||
/// pattern.
|
/// matching the pattern.
|
||||||
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)
|
||||||
.into_iter();
|
.into_iter()
|
||||||
|
.filter_entry(|e| !is_hidden(e))
|
||||||
|
.filter_map(|e| e.ok())
|
||||||
|
.filter(|e| e.path() != root);
|
||||||
|
|
||||||
for entry in walker.filter_entry(|e| !is_hidden(e))
|
for entry in walker {
|
||||||
.filter_map(|e| e.ok()) {
|
if let Ok(path_relative) = entry.path().strip_prefix(root) {
|
||||||
if entry.path() == root {
|
if let Some(path_str) = path_relative.to_str() {
|
||||||
continue;
|
let haystack = if config.search_full_path {
|
||||||
|
path_str
|
||||||
|
} else {
|
||||||
|
path_relative.file_name()
|
||||||
|
.and_then(OsStr::to_str)
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
pattern.find(haystack)
|
||||||
|
.map(|_| print_entry(&entry, path_str, &config));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let path_relative =
|
|
||||||
match entry.path().strip_prefix(root) {
|
|
||||||
Ok(r) => r,
|
|
||||||
Err(_) => continue
|
|
||||||
};
|
|
||||||
|
|
||||||
let path_str = match path_relative.to_str() {
|
|
||||||
Some(p) => p,
|
|
||||||
None => continue
|
|
||||||
};
|
|
||||||
|
|
||||||
pattern.find(path_str)
|
|
||||||
.map(|_| print_entry(&entry, path_str, &config));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +87,7 @@ fn main() {
|
||||||
opts.optflag("f", "full-path", "search full path (default: only filename)");
|
opts.optflag("f", "full-path", "search full path (default: only filename)");
|
||||||
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");
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => error(e.description())
|
Err(e) => error(e.description())
|
||||||
|
|
Loading…
Reference in New Issue