Option to follow symlinks, disable colors, closes #16, closes #17
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 92e2fd3 · Browse files · 1 file·+40 added−18 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+40 added−18 removed
- other:+40 added−18 removed
Provenance
- Commit
051af3d- Tree
617328c- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
92e2fd3
1 file changed+40 added−18 removed
modified:src/main.rs+40 added-18 removed
12 unmodified lines13 use regex::{Regex, RegexBuilder};14 use getopts::Options;15 use ansi_term::Colour;16 17+struct FdOptions {18+ case_sensitive: bool,19+ search_full_path: bool,20+ follow_links: bool,21+ colored: bool22+}23+24 /// Print a search result to the console.25+fn print_entry(entry: &DirEntry, path_str: &str, config: &FdOptions) {18-fn print_entry(entry: &DirEntry, path_str: &str) {19- let style = match entry {20- e if e.path_is_symbolic_link() => Colour::Purple,21- e if e.path().is_dir() => Colour::Cyan,22- _ => Colour::White23- };24- println!("{}", style.paint(path_str));26+ if config.colored {27+ let style = match entry {28+ e if e.path_is_symbolic_link() => Colour::Purple,29+ e if e.path().is_dir() => Colour::Cyan,30+ _ => Colour::White31+ };32+ println!("{}", style.paint(path_str));33+ } else {34+ println!("{}", path_str);35+ }36 }37 38 /// Check if filename of entry starts with a dot.39 fn is_hidden(entry: &DirEntry) -> bool {4 unmodified lines44 }45 46 /// Recursively scan the given root path and search for pathnames matching the47 /// pattern.37-fn scan(root: &Path, pattern: &Regex) {38- let walker = WalkDir::new(root).into_iter();48+fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {49+ let walker = WalkDir::new(root)50+ .follow_links(config.follow_links)51+ .into_iter();52+53 for entry in walker.filter_entry(|e| !is_hidden(e))54 .filter_map(|e| e.ok()) {55 if entry.path() == root {56 continue;10 unmodified lines67 None => continue68 };69 70 pattern.find(path_str)57- .map(|_| print_entry(&entry, path_str));71+ .map(|_| print_entry(&entry, path_str, &config));72 }73 }74 75 /// Print error message to stderr and exit with status `1`.7 unmodified lines83 let args: Vec<String> = env::args().collect();84 85 let mut opts = Options::new();86 opts.optflag("h", "help", "print this help message");73- opts.optflag("s", "sensitive", "case-sensitive search");87+ opts.optflag("s", "sensitive", "case-sensitive search (default: smart case)");88+ opts.optflag("f", "full-path", "search full path (default: only filename)");89+ opts.optflag("F", "follow", "follow symlinks (default: off)");90+ opts.optflag("n", "no-color", "do not colorize output");91 let matches = match opts.parse(&args[1..]) {92 Ok(m) => m,93 Err(e) => error(e.description())94 };12 unmodified lines107 Err(_) => error("Could not get current directory!")108 };109 let current_dir = current_dir_buf.as_path();110 94- // The search will be case-sensitive if the command line flag is set or if95- // the pattern has an uppercase character (smart case).96- let case_sensitive =97- matches.opt_present("s") ||98- pattern.chars().any(char::is_uppercase);111 112+ let config = FdOptions {113+ // The search will be case-sensitive if the command line flag is set or if114+ // the pattern has an uppercase character (smart case).115+ case_sensitive: matches.opt_present("s") ||116+ pattern.chars().any(char::is_uppercase),117+ search_full_path: matches.opt_present("f"),118+ colored: !matches.opt_present("n"),119+ follow_links: matches.opt_present("F")120+ };121+122 match RegexBuilder::new(pattern)101- .case_insensitive(!case_sensitive)123+ .case_insensitive(!config.case_sensitive)124 .build() {103- Ok(re) => scan(current_dir, &re),125+ Ok(re) => scan(¤t_dir, &re, &config),126 Err(err) => error(err.description())127 }128 }