remove canonicalize in relative_name function and add None Display Option

This commit is contained in:
dvvvvvv 2020-02-04 21:17:02 +09:00 committed by Abin Simon
parent 00e4f04eec
commit 70aea2947b
2 changed files with 7 additions and 21 deletions

View file

@ -20,12 +20,8 @@ pub fn grid(metas: &[Meta], flags: &Flags, colors: &Colors, icons: &Icons) -> St
None => None, None => None,
}; };
let current_dir = std::env::current_dir().unwrap();
inner_display_grid( inner_display_grid(
&DisplayOption::Relative { &DisplayOption::None,
base_path: &current_dir,
},
metas, metas,
&flags, &flags,
colors, colors,

View file

@ -9,6 +9,7 @@ use std::path::{Component, Path, PathBuf};
pub enum DisplayOption<'a> { pub enum DisplayOption<'a> {
FileName, FileName,
Relative { base_path: &'a Path }, Relative { base_path: &'a Path },
None,
} }
#[derive(Clone, Debug, Eq)] #[derive(Clone, Debug, Eq)]
@ -48,24 +49,12 @@ impl Name {
} }
fn relative_path<T: AsRef<Path> + Clone>(&self, base_path: T) -> PathBuf { fn relative_path<T: AsRef<Path> + Clone>(&self, base_path: T) -> PathBuf {
use std::borrow::Cow; let base_path = base_path.as_ref();
let target_path = if self.path.is_absolute() { if self.path == base_path {
Cow::Borrowed(&self.path)
} else {
Cow::Owned(self.path.canonicalize().unwrap())
};
let base_path = if base_path.as_ref().is_absolute() {
Cow::Borrowed(base_path.as_ref())
} else {
Cow::Owned(base_path.as_ref().canonicalize().unwrap())
};
if target_path == base_path {
return PathBuf::from("."); return PathBuf::from(".");
} }
let shared_components: PathBuf = target_path let shared_components: PathBuf = self.path
.components() .components()
.zip(base_path.components()) .zip(base_path.components())
.take_while(|(target_component, base_component)| target_component == base_component) .take_while(|(target_component, base_component)| target_component == base_component)
@ -78,7 +67,7 @@ impl Name {
.components() .components()
.map(|_| Component::ParentDir) .map(|_| Component::ParentDir)
.chain( .chain(
target_path self.path
.strip_prefix(&shared_components) .strip_prefix(&shared_components)
.unwrap() .unwrap()
.components(), .components(),
@ -99,6 +88,7 @@ impl Name {
icons.get(self), icons.get(self),
self.relative_path(base_path).to_string_lossy() self.relative_path(base_path).to_string_lossy()
), ),
DisplayOption::None => format!("{}{}", icons.get(self), self.path.to_string_lossy())
}; };
let elem = match self.file_type { let elem = match self.file_type {