Skip to content
Myr

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

1 file+23 added−23 removed

modified:src/main.rs+23 added-23 removed
2 unmodified lines
3extern crate getopts;4extern crate ansi_term;56use std::env;7use std::path::Path;7use std::error::Error;8use std::ffi::OsStr;9use std::io::Write;10use std::path::Path;11use std::process;10use std::error::Error;1213use walkdir::{WalkDir, DirEntry, WalkDirIterator};14use regex::{Regex, RegexBuilder};15use getopts::Options;
27 unmodified lines
43 .map(|s| s.starts_with("."))44 .unwrap_or(false)45}4646/// 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.49fn 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);5653 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 }5859 let path_relative =60 match entry.path().strip_prefix(root) {61 Ok(r) => r,62 Err(_) => continue63 };6465 let path_str = match path_relative.to_str() {66 Some(p) => p,67 None => continue68 };6970 pattern.find(path_str)71 .map(|_| print_entry(&entry, path_str, &config));71 }72}7374/// Print error message to stderr and exit with status `1`.
11 unmodified lines
86 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");9091 let matches = match opts.parse(&args[1..]) {92 Ok(m) => m,93 Err(e) => error(e.description())94 };
34 unmodified lines