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
Provenance
- Commit
8adb5b9- Tree
c9c806d- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
db8d0cc
2 files changed+57 added−11 removed
modified:src/main.rs+48 added-11 removed
20 unmodified lines21 use ignore::WalkBuilder;22 23 use lscolors::LsColors;24 25+/// Defines how to display search result paths.26+#[derive(PartialEq)]27+enum PathDisplay {28+ /// As an absolute path29+ Absolute,30+31+ /// As a relative path32+ Relative33+}34+35 /// Configuration options for *fd*.36 struct FdOptions {37 /// Determines whether the regex search is case-sensitive or case-insensitive.38 case_sensitive: bool,16 unmodified lines55 /// 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>,58 59+ /// Display results as relative or absolute path.60+ path_display: PathDisplay,61+62 /// `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 }66 67+/// Root directory68+static ROOT_DIR : &str = "/";69+70 /// Print a search result to the console.55-fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) {56- let path_full = path_root.join(path_entry);71+fn print_entry(base: &Path, entry: &Path, config: &FdOptions) {72+ let path_full = base.join(entry);73 58- let path_str = match path_entry.to_str() {74+ let path_str = match entry.to_str() {75 Some(p) => p,76 None => return77 };78 79 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();81 82+ if config.path_display == PathDisplay::Absolute {83+ print!("{}", ls_colors.directory.paint(ROOT_DIR));84+ }85+86 // 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 lines127 }128 }129 println!();130 } else {111- // Uncolored output:131+ // Uncolorized output:132+133+ if config.path_display == PathDisplay::Absolute {134+ print!("{}", ROOT_DIR);135+ }136 println!("{}", path_str);137 }138 }139 116-/// Recursively scan the given root path and search for files / pathnames matching the pattern.117-fn scan(cwd: &Path, root: &Path, pattern: &Regex, config: &FdOptions) {140+/// Recursively scan the given search path and search for files / pathnames matching the pattern.141+fn 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 lines153 .filter_map(|e| e.ok())154 .filter(|e| e.path() != root);155 156 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 lines168 .and_then(OsStr::to_str)169 };170 171 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 }175 176 /// Print error message to stderr and exit with status `1`.22 unmodified lines199 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");205 206 let matches = match opts.parse(&args[1..]) {207 Ok(m) => m,208 Err(e) => error(e.description())49 unmodified lines258 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 };269 270+ let root = Path::new(ROOT_DIR);271+ let base = match config.path_display {272+ PathDisplay::Relative => current_dir,273+ PathDisplay::Absolute => root274+ };275+276 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
187 unmodified linesExpand all188189190191192193194195196197198199200201202187 unmodified linesExpand allexpect "a.fooonesymlink" --max-depth 1
suite "Absolute paths (--absolute)"expect "$root/a.foo$root/one/b.foo$root/one/two/c.foo$root/one/two/C.Foo$root/one/two/three/d.foo$root/one/two/three/directory_foo" --absolute foo
# All doneecho