mirror of
https://github.com/sharkdp/fd
synced 2024-10-04 14:59:15 +00:00
Use absolute paths if root dir is an absolute path, fixes #40
This commit is contained in:
parent
1b1f644398
commit
54f33e5227
18
src/main.rs
18
src/main.rs
|
@ -11,7 +11,6 @@ pub mod fshelper;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
|
@ -105,7 +104,7 @@ fn print_entry(base: &Path, entry: &Path, config: &FdOptions) {
|
||||||
let comp_str = match component {
|
let comp_str = match component {
|
||||||
Component::Normal(p) => p.to_string_lossy(),
|
Component::Normal(p) => p.to_string_lossy(),
|
||||||
Component::ParentDir => Cow::from(PARENT_DIR),
|
Component::ParentDir => Cow::from(PARENT_DIR),
|
||||||
_ => error("Error: unexpected path component")
|
_ => error("Error: unexpected path component.")
|
||||||
};
|
};
|
||||||
|
|
||||||
component_path.push(Path::new(comp_str.deref()));
|
component_path.push(Path::new(comp_str.deref()));
|
||||||
|
@ -257,21 +256,26 @@ fn main() {
|
||||||
// Get the current working directory
|
// Get the current working directory
|
||||||
let current_dir_buf = match env::current_dir() {
|
let current_dir_buf = match env::current_dir() {
|
||||||
Ok(cd) => cd,
|
Ok(cd) => cd,
|
||||||
Err(_) => error("Error: could not get current directory!")
|
Err(_) => error("Error: could not get current directory.")
|
||||||
};
|
};
|
||||||
let current_dir = current_dir_buf.as_path();
|
let current_dir = current_dir_buf.as_path();
|
||||||
|
|
||||||
// Get the root directory for the search
|
// Get the root directory for the search
|
||||||
|
let mut root_dir_is_absolute = false;
|
||||||
let root_dir_buf = if let Some(rd) = matches.value_of("path") {
|
let root_dir_buf = if let Some(rd) = matches.value_of("path") {
|
||||||
fs::canonicalize(rd).unwrap_or_else(
|
let path = Path::new(rd);
|
||||||
|_| error(&format!("Error: could not find directory '{}'", rd))
|
|
||||||
|
root_dir_is_absolute = path.is_absolute();
|
||||||
|
|
||||||
|
path.canonicalize().unwrap_or_else(
|
||||||
|
|_| error(&format!("Error: could not find directory '{}'.", rd))
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
current_dir_buf.clone()
|
current_dir_buf.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
if !root_dir_buf.is_dir() {
|
if !root_dir_buf.is_dir() {
|
||||||
error(&format!("Error: '{}' is not a directory", root_dir_buf.to_string_lossy()));
|
error(&format!("Error: '{}' is not a directory.", root_dir_buf.to_string_lossy()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let root_dir = root_dir_buf.as_path();
|
let root_dir = root_dir_buf.as_path();
|
||||||
|
@ -304,7 +308,7 @@ fn main() {
|
||||||
follow_links: matches.is_present("follow"),
|
follow_links: matches.is_present("follow"),
|
||||||
max_depth: matches.value_of("depth")
|
max_depth: matches.value_of("depth")
|
||||||
.and_then(|ds| usize::from_str_radix(ds, 10).ok()),
|
.and_then(|ds| usize::from_str_radix(ds, 10).ok()),
|
||||||
path_display: if matches.is_present("absolute-path") {
|
path_display: if matches.is_present("absolute-path") || root_dir_is_absolute {
|
||||||
PathDisplay::Absolute
|
PathDisplay::Absolute
|
||||||
} else {
|
} else {
|
||||||
PathDisplay::Relative
|
PathDisplay::Relative
|
||||||
|
|
|
@ -197,6 +197,12 @@ $root/one/two/c.foo
|
||||||
$root/one/two/C.Foo
|
$root/one/two/C.Foo
|
||||||
$root/one/two/three/d.foo
|
$root/one/two/three/d.foo
|
||||||
$root/one/two/three/directory_foo" --absolute-path foo
|
$root/one/two/three/directory_foo" --absolute-path foo
|
||||||
|
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" foo "$root"
|
||||||
|
|
||||||
|
|
||||||
suite "Invalid UTF-8"
|
suite "Invalid UTF-8"
|
||||||
|
|
Loading…
Reference in a new issue