Skip to content
Myr

Add option to print absolute paths, closes #10

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by sharkdp · parent db8d0cc · Browse files · 2 files+57 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+57 added−11 removed

  • tests:+9 added−0 removed
  • other:+48 added−11 removed

2 files+57 added−11 removed

modified:src/main.rs+48 added-11 removed
20 unmodified lines
21use ignore::WalkBuilder;2223use lscolors::LsColors;2425/// Defines how to display search result paths.26#[derive(PartialEq)]27enum PathDisplay {28 /// As an absolute path29 Absolute,3031 /// As a relative path32 Relative33}3435/// Configuration options for *fd*.36struct FdOptions {37 /// Determines whether the regex search is case-sensitive or case-insensitive.38 case_sensitive: bool,
16 unmodified lines
55 /// A depth of `1` includes all files under the current directory, a depth of `2` also includes56 /// all files under subdirectories of the current directory, etc.57 max_depth: Option<usize>,5859 /// Display results as relative or absolute path.60 path_display: PathDisplay,6162 /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines63 /// how to style different filetypes.64 ls_colors: Option<LsColors>65}6667/// Root directory68static ROOT_DIR : &str = "/";6970/// Print a search result to the console.55fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) {56 let path_full = path_root.join(path_entry);71fn print_entry(base: &Path, entry: &Path, config: &FdOptions) {72 let path_full = base.join(entry);7358 let path_str = match path_entry.to_str() {74 let path_str = match entry.to_str() {75 Some(p) => p,76 None => return77 };7879 if let Some(ref ls_colors) = config.ls_colors {64 let mut component_path = path_root.to_path_buf();80 let mut component_path = base.to_path_buf();8182 if config.path_display == PathDisplay::Absolute {83 print!("{}", ls_colors.directory.paint(ROOT_DIR));84 }8586 // Traverse the path and colorize each component67 for component in path_entry.components() {87 for component in entry.components() {88 let comp_str = match component {89 Component::Normal(p) => p.to_str().unwrap(),90 Component::ParentDir => "..",91 _ => error("Unexpected path component")
35 unmodified lines
127 }128 }129 println!();130 } else {111 // Uncolored output:131 // Uncolorized output:132133 if config.path_display == PathDisplay::Absolute {134 print!("{}", ROOT_DIR);135 }136 println!("{}", path_str);137 }138}139116/// Recursively scan the given root path and search for files / pathnames matching the pattern.117fn scan(cwd: &Path, root: &Path, pattern: &Regex, config: &FdOptions) {140/// Recursively scan the given search path and search for files / pathnames matching the pattern.141fn scan(root: &Path, pattern: &Regex, base: &Path, config: &FdOptions) {142 let walker = WalkBuilder::new(root)143 .hidden(config.ignore_hidden)144 .ignore(config.read_ignore)145 .git_ignore(config.read_ignore)
7 unmodified lines
153 .filter_map(|e| e.ok())154 .filter(|e| e.path() != root);155156 for entry in walker {133 let path_rel_buf = match fshelper::path_relative_from(entry.path(), cwd) {157 let path_rel_buf = match fshelper::path_relative_from(entry.path(), base) {158 Some(p) => p,159 None => error("Could not get relative path for directory entry.")160 };161 let path_rel = path_rel_buf.as_path();
6 unmodified lines
168 .and_then(OsStr::to_str)169 };170171 search_str.and_then(|s| pattern.find(s))148 .map(|_| print_entry(cwd, path_rel, config));172 .map(|_| print_entry(base, path_rel, config));173 }174}175176/// Print error message to stderr and exit with status `1`.
22 unmodified lines
199 opts.optflag("n", "no-color",200 "do not colorize output");201 opts.optopt("d", "max-depth",202 "maximum search depth (default: none)", "D");203 opts.optflag("a", "absolute",204 "show absolute instead of relative paths");205206 let matches = match opts.parse(&args[1..]) {207 Ok(m) => m,208 Err(e) => error(e.description())
49 unmodified lines
258 read_ignore: !matches.opt_present("no-ignore"),259 follow_links: matches.opt_present("follow"),260 max_depth: matches.opt_str("max-depth")261 .and_then(|ds| usize::from_str_radix(&ds, 10).ok()),262 path_display: if matches.opt_present("absolute") {263 PathDisplay::Absolute264 } else {265 PathDisplay::Relative266 },267 ls_colors: ls_colors268 };269270 let root = Path::new(ROOT_DIR);271 let base = match config.path_display {272 PathDisplay::Relative => current_dir,273 PathDisplay::Absolute => root274 };275276 match RegexBuilder::new(pattern)277 .case_insensitive(!config.case_sensitive)278 .build() {242 Ok(re) => scan(current_dir, root_dir, &re, &config),279 Ok(re) => scan(root_dir, &re, base, &config),280 Err(err) => error(err.description())281 }282}
modified:tests/test.sh+9 added