remove name property from Name struct

This commit is contained in:
dvvvvvv 2020-02-02 22:55:44 +09:00 committed by Abin Simon
parent e6f47d75ad
commit 79adfff370
5 changed files with 60 additions and 56 deletions

View file

@ -1,6 +1,7 @@
use ansi_term::{ANSIString, Colour, Style};
use lscolors::{Indicator, LsColors};
use std::collections::HashMap;
use std::path::Path;
#[allow(dead_code)]
#[derive(Hash, Debug, Eq, PartialEq, Clone)]
@ -96,7 +97,7 @@ impl Colors {
pub fn colorize_using_path<'a>(
&self,
input: String,
path: &str,
path: &Path,
elem: &Elem,
) -> ColoredString<'a> {
let style_from_path = self.style_from_path(path);
@ -106,7 +107,7 @@ impl Colors {
}
}
fn style_from_path(&self, path: &str) -> Option<Style> {
fn style_from_path(&self, path: &Path) -> Option<Style> {
match &self.lscolors {
Some(lscolors) => lscolors
.style_for_path(path)

View file

@ -95,7 +95,7 @@ impl Core {
meta_list.push(meta);
}
_ => {
match meta.recurse_into(depth, self.flags.display, &self.flags.ignore_globs) {
match meta.recurse_into(base_path, depth, self.flags.display, &self.flags.ignore_globs) {
Ok(content) => {
meta.content = content;
meta_list.push(meta);

View file

@ -91,27 +91,26 @@ impl Icons {
return res;
}
// Check the known names.
if let Some(icon) = self.icons_by_name.get(name.name().as_str()) {
if let Some(icon) = name.file_name()
.map(std::ffi::OsStr::to_string_lossy)
.and_then(|file_name| self.icons_by_name.get(file_name.as_ref())) {
// Use the known names.
res += icon;
res += ICON_SPACE;
return res;
}
// Check the known extensions.
if let Some(extension) = name.extension() {
if let Some(icon) = self.icons_by_extension.get(extension.as_str()) {
res
} else if let Some(icon) = name.extension()
.and_then(|extension| self.icons_by_extension.get(extension)) {
// Use the known extensions.
res += icon;
res += ICON_SPACE;
return res;
}
}
res
} else {
// Use the default icons.
res += self.default_file_icon;
res += ICON_SPACE;
res
}
}
fn get_default_icons_by_name() -> HashMap<&'static str, &'static str> {
let mut m = HashMap::new();

View file

@ -27,7 +27,7 @@ use crate::print_error;
use std::fs;
use std::fs::read_link;
use std::io::{Error, ErrorKind};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use globset::GlobSet;
@ -49,6 +49,7 @@ pub struct Meta {
impl Meta {
pub fn recurse_into(
&self,
base_path: &Path,
depth: usize,
display: Display,
ignore_globs: &GlobSet,
@ -77,8 +78,8 @@ impl Meta {
let mut content: Vec<Meta> = Vec::new();
if let Display::DisplayAll = display {
let mut current_meta;
let mut parent_meta;
let current_meta;
let parent_meta;
let absolute_path = fs::canonicalize(&self.path)?;
let parent_path = match absolute_path.parent() {
@ -86,11 +87,12 @@ impl Meta {
Some(path) => PathBuf::from(path),
};
//replace it by display
current_meta = self.clone();
current_meta.name.name = ".".to_string();
//current_meta.name.path = PathBuf::from(".");
parent_meta = Self::from_path(&parent_path)?;
parent_meta.name.name = "..".to_string();
//parent_meta.name.name = "..".to_string();
content.push(current_meta);
content.push(parent_meta);
@ -121,7 +123,7 @@ impl Meta {
}
};
match entry_meta.recurse_into(depth - 1, display, ignore_globs) {
match entry_meta.recurse_into(base_path, depth - 1, display, ignore_globs) {
Ok(content) => entry_meta.content = content,
Err(err) => {
print_error!("lsd: {}: {}\n", path.display(), err);
@ -195,7 +197,7 @@ impl Meta {
}
}
pub fn from_path(path: &PathBuf) -> Result<Self, std::io::Error> {
pub fn from_path(path: &Path) -> Result<Self, std::io::Error> {
let metadata = if read_link(path).is_ok() {
// If the file is a link, retrieve the metadata without following
// the link.
@ -219,7 +221,7 @@ impl Meta {
Ok(Self {
inode,
path: path.to_path_buf(),
symlink: SymLink::from(path.as_path()),
symlink: SymLink::from(path),
size: Size::from(&metadata),
date: Date::from(&metadata),
indicator: Indicator::from(file_type),

View file

@ -2,23 +2,22 @@ use crate::color::{ColoredString, Colors, Elem};
use crate::icon::Icons;
use crate::meta::filetype::FileType;
use std::cmp::{Ordering, PartialOrd};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::ffi::OsStr;
#[derive(Clone, Debug, Eq)]
pub struct Name {
pub name: String,
path: String,
path: PathBuf,
extension: Option<String>,
file_type: FileType,
}
impl Name {
pub fn new(path: &Path, file_type: FileType) -> Self {
let name = match path.file_name() {
Some(name) => name.to_string_lossy().to_string(),
None => path.to_string_lossy().to_string(),
};
pub fn file_name(&self) -> Option<&OsStr> {
self.path.file_name()
}
pub fn new(path: &Path, file_type: FileType) -> Self {
let mut extension = None;
if let Some(res) = path.extension() {
extension = Some(
@ -28,11 +27,8 @@ impl Name {
);
}
let path_string = path.to_string_lossy().to_string();
Self {
name,
path: path_string,
path: PathBuf::from(path),
extension,
file_type,
}
@ -40,11 +36,9 @@ impl Name {
pub fn name_string(&self, icons: &Icons) -> String {
let icon = icons.get(self);
let mut content = String::with_capacity(icon.len() + self.name.len() + 3 /* spaces */);
let display = &self.path.to_string_lossy();
content += icon.as_str();
content += &self.name;
content
format!("{}{}", icon.as_str(), display)
}
pub fn render(&self, colors: &Colors, icons: &Icons) -> ColoredString {
@ -64,12 +58,8 @@ impl Name {
colors.colorize_using_path(content, &self.path, &elem)
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn extension(&self) -> Option<String> {
self.extension.clone()
pub fn extension(&self) -> Option<&str> {
self.extension.as_ref().map(|string| string.as_str())
}
pub fn file_type(&self) -> FileType {
@ -79,19 +69,19 @@ impl Name {
impl Ord for Name {
fn cmp(&self, other: &Self) -> Ordering {
self.name.to_lowercase().cmp(&other.name.to_lowercase())
self.path.cmp(&other.path)
}
}
impl PartialOrd for Name {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.name.to_lowercase().cmp(&other.name.to_lowercase()))
self.path.partial_cmp(&other.path)
}
}
impl PartialEq for Name {
fn eq(&self, other: &Self) -> bool {
self.name.eq_ignore_ascii_case(&other.name)
self.path.eq(&other.path)
}
}
@ -172,7 +162,7 @@ mod test {
let colors = Colors::new(color::Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&symlink_path, file_type);
let name = Name::new(&tmp_dir.into_path(), &symlink_path, file_type);
assert_eq!(
Colour::Fixed(44).paint(" target.tmp"),
@ -198,7 +188,7 @@ mod test {
let colors = Colors::new(color::Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&pipe_path, file_type);
let name = Name::new(&tmp_dir.into_path(), &pipe_path, file_type);
assert_eq!(
Colour::Fixed(184).paint(" pipe.tmp"),
@ -229,6 +219,7 @@ mod test {
let path = Path::new("some-file.txt");
let name = Name::new(
Path::new("/"),
&path,
FileType::File {
uid: false,
@ -236,7 +227,7 @@ mod test {
},
);
assert_eq!(Some(String::from("txt")), name.extension());
assert_eq!(Some("txt"), name.extension());
}
#[test]
@ -244,6 +235,7 @@ mod test {
let path = Path::new(".gitignore");
let name = Name::new(
Path::new("/"),
&path,
FileType::File {
uid: false,
@ -258,6 +250,7 @@ mod test {
fn test_order_impl_is_case_insensitive() {
let path_1 = Path::new("AAAA");
let name_1 = Name::new(
Path::new("/"),
&path_1,
FileType::File {
uid: false,
@ -267,6 +260,7 @@ mod test {
let path_2 = Path::new("aaaa");
let name_2 = Name::new(
Path::new("/"),
&path_2,
FileType::File {
uid: false,
@ -281,6 +275,7 @@ mod test {
fn test_partial_order_impl() {
let path_a = Path::new("aaaa");
let name_a = Name::new(
Path::new("/"),
&path_a,
FileType::File {
uid: false,
@ -290,6 +285,7 @@ mod test {
let path_z = Path::new("zzzz");
let name_z = Name::new(
Path::new("/"),
&path_z,
FileType::File {
uid: false,
@ -304,6 +300,7 @@ mod test {
fn test_partial_order_impl_is_case_insensitive() {
let path_a = Path::new("aaaa");
let name_a = Name::new(
Path::new("/"),
&path_a,
FileType::File {
uid: false,
@ -313,6 +310,7 @@ mod test {
let path_z = Path::new("ZZZZ");
let name_z = Name::new(
Path::new("/"),
&path_z,
FileType::File {
uid: false,
@ -327,6 +325,7 @@ mod test {
fn test_partial_eq_impl() {
let path_1 = Path::new("aaaa");
let name_1 = Name::new(
Path::new("/"),
&path_1,
FileType::File {
uid: false,
@ -336,6 +335,7 @@ mod test {
let path_2 = Path::new("aaaa");
let name_2 = Name::new(
Path::new("/"),
&path_2,
FileType::File {
uid: false,
@ -350,6 +350,7 @@ mod test {
fn test_partial_eq_impl_is_case_insensitive() {
let path_1 = Path::new("AAAA");
let name_1 = Name::new(
Path::new("/"),
&path_1,
FileType::File {
uid: false,
@ -359,6 +360,7 @@ mod test {
let path_2 = Path::new("aaaa");
let name_2 = Name::new(
Path::new("/"),
&path_2,
FileType::File {
uid: false,