Code-cleanup
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 085c8dd · Browse files · 1 file·+23 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+23 added−23 removed
- other:+23 added−23 removed
Provenance
- Commit
e20bd9d- Tree
2153b86- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
085c8dd
1 file changed+23 added−23 removed
modified:src/main.rs+23 added-23 removed
2 unmodified lines3 extern crate getopts;4 extern crate ansi_term;5 6 use std::env;7-use std::path::Path;7+use std::error::Error;8+use std::ffi::OsStr;9 use std::io::Write;10+use std::path::Path;11 use std::process;10-use std::error::Error;12 13 use walkdir::{WalkDir, DirEntry, WalkDirIterator};14 use regex::{Regex, RegexBuilder};15 use getopts::Options;27 unmodified lines43 .map(|s| s.starts_with("."))44 .unwrap_or(false)45 }46 46-/// Recursively scan the given root path and search for pathnames matching the47-/// pattern.47+/// Recursively scan the given root path and search for files / pathnames48+/// matching the pattern.49 fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {50 let walker = WalkDir::new(root)51 .follow_links(config.follow_links)51- .into_iter();52+ .into_iter()53+ .filter_entry(|e| !is_hidden(e))54+ .filter_map(|e| e.ok())55+ .filter(|e| e.path() != root);56 53- for entry in walker.filter_entry(|e| !is_hidden(e))54- .filter_map(|e| e.ok()) {55- if entry.path() == root {56- continue;57+ for entry in walker {58+ if let Ok(path_relative) = entry.path().strip_prefix(root) {59+ if let Some(path_str) = path_relative.to_str() {60+ let haystack = if config.search_full_path {61+ path_str62+ } else {63+ path_relative.file_name()64+ .and_then(OsStr::to_str)65+ .unwrap()66+ };67+ pattern.find(haystack)68+ .map(|_| print_entry(&entry, path_str, &config));69+ }70 }58-59- let path_relative =60- match entry.path().strip_prefix(root) {61- Ok(r) => r,62- Err(_) => continue63- };64-65- let path_str = match path_relative.to_str() {66- Some(p) => p,67- None => continue68- };69-70- pattern.find(path_str)71- .map(|_| print_entry(&entry, path_str, &config));71 }72 }73 74 /// Print error message to stderr and exit with status `1`.11 unmodified lines86 opts.optflag("s", "sensitive", "case-sensitive search (default: smart case)");87 opts.optflag("f", "full-path", "search full path (default: only filename)");88 opts.optflag("F", "follow", "follow symlinks (default: off)");89 opts.optflag("n", "no-color", "do not colorize output");90+91 let matches = match opts.parse(&args[1..]) {92 Ok(m) => m,93 Err(e) => error(e.description())94 };34 unmodified lines