Skip to content
Myr

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

1 file+40 added−18 removed

modified:src/main.rs+40 added-18 removed
12 unmodified lines
13use regex::{Regex, RegexBuilder};14use getopts::Options;15use ansi_term::Colour;1617struct FdOptions {18 case_sensitive: bool,19 search_full_path: bool,20 follow_links: bool,21 colored: bool22}2324/// Print a search result to the console.25fn print_entry(entry: &DirEntry, path_str: &str, config: &FdOptions) {18fn 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}3738/// Check if filename of entry starts with a dot.39fn is_hidden(entry: &DirEntry) -> bool {
4 unmodified lines
44}4546/// Recursively scan the given root path and search for pathnames matching the47/// pattern.37fn scan(root: &Path, pattern: &Regex) {38 let walker = WalkDir::new(root).into_iter();48fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {49 let walker = WalkDir::new(root)50 .follow_links(config.follow_links)51 .into_iter();5253 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 lines
67 None => continue68 };6970 pattern.find(path_str)57 .map(|_| print_entry(&entry, path_str));71 .map(|_| print_entry(&entry, path_str, &config));72 }73}7475/// Print error message to stderr and exit with status `1`.
7 unmodified lines
83 let args: Vec<String> = env::args().collect();8485 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 lines
107 Err(_) => error("Could not get current directory!")108 };109 let current_dir = current_dir_buf.as_path();11094 // 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);111112 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 };121122 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(&current_dir, &re, &config),126 Err(err) => error(err.description())127 }128}