mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 06:02:36 +00:00
remove name property from Name struct
This commit is contained in:
parent
e6f47d75ad
commit
79adfff370
5 changed files with 60 additions and 56 deletions
|
@ -1,6 +1,7 @@
|
||||||
use ansi_term::{ANSIString, Colour, Style};
|
use ansi_term::{ANSIString, Colour, Style};
|
||||||
use lscolors::{Indicator, LsColors};
|
use lscolors::{Indicator, LsColors};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Hash, Debug, Eq, PartialEq, Clone)]
|
#[derive(Hash, Debug, Eq, PartialEq, Clone)]
|
||||||
|
@ -96,7 +97,7 @@ impl Colors {
|
||||||
pub fn colorize_using_path<'a>(
|
pub fn colorize_using_path<'a>(
|
||||||
&self,
|
&self,
|
||||||
input: String,
|
input: String,
|
||||||
path: &str,
|
path: &Path,
|
||||||
elem: &Elem,
|
elem: &Elem,
|
||||||
) -> ColoredString<'a> {
|
) -> ColoredString<'a> {
|
||||||
let style_from_path = self.style_from_path(path);
|
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 {
|
match &self.lscolors {
|
||||||
Some(lscolors) => lscolors
|
Some(lscolors) => lscolors
|
||||||
.style_for_path(path)
|
.style_for_path(path)
|
||||||
|
|
|
@ -95,7 +95,7 @@ impl Core {
|
||||||
meta_list.push(meta);
|
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) => {
|
Ok(content) => {
|
||||||
meta.content = content;
|
meta.content = content;
|
||||||
meta_list.push(meta);
|
meta_list.push(meta);
|
||||||
|
|
33
src/icon.rs
33
src/icon.rs
|
@ -91,26 +91,25 @@ impl Icons {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the known names.
|
if let Some(icon) = name.file_name()
|
||||||
if let Some(icon) = self.icons_by_name.get(name.name().as_str()) {
|
.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;
|
||||||
res += ICON_SPACE;
|
res += ICON_SPACE;
|
||||||
return res;
|
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;
|
||||||
|
res
|
||||||
|
} else {
|
||||||
|
// Use the default icons.
|
||||||
|
res += self.default_file_icon;
|
||||||
|
res += ICON_SPACE;
|
||||||
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the known extensions.
|
|
||||||
if let Some(extension) = name.extension() {
|
|
||||||
if let Some(icon) = self.icons_by_extension.get(extension.as_str()) {
|
|
||||||
res += icon;
|
|
||||||
res += ICON_SPACE;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use the default icons.
|
|
||||||
res += self.default_file_icon;
|
|
||||||
res += ICON_SPACE;
|
|
||||||
res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_default_icons_by_name() -> HashMap<&'static str, &'static str> {
|
fn get_default_icons_by_name() -> HashMap<&'static str, &'static str> {
|
||||||
|
|
|
@ -27,7 +27,7 @@ use crate::print_error;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::read_link;
|
use std::fs::read_link;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use globset::GlobSet;
|
use globset::GlobSet;
|
||||||
|
|
||||||
|
@ -49,6 +49,7 @@ pub struct Meta {
|
||||||
impl Meta {
|
impl Meta {
|
||||||
pub fn recurse_into(
|
pub fn recurse_into(
|
||||||
&self,
|
&self,
|
||||||
|
base_path: &Path,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
display: Display,
|
display: Display,
|
||||||
ignore_globs: &GlobSet,
|
ignore_globs: &GlobSet,
|
||||||
|
@ -77,8 +78,8 @@ impl Meta {
|
||||||
let mut content: Vec<Meta> = Vec::new();
|
let mut content: Vec<Meta> = Vec::new();
|
||||||
|
|
||||||
if let Display::DisplayAll = display {
|
if let Display::DisplayAll = display {
|
||||||
let mut current_meta;
|
let current_meta;
|
||||||
let mut parent_meta;
|
let parent_meta;
|
||||||
|
|
||||||
let absolute_path = fs::canonicalize(&self.path)?;
|
let absolute_path = fs::canonicalize(&self.path)?;
|
||||||
let parent_path = match absolute_path.parent() {
|
let parent_path = match absolute_path.parent() {
|
||||||
|
@ -86,11 +87,12 @@ impl Meta {
|
||||||
Some(path) => PathBuf::from(path),
|
Some(path) => PathBuf::from(path),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//replace it by display
|
||||||
current_meta = self.clone();
|
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 = Self::from_path(&parent_path)?;
|
||||||
parent_meta.name.name = "..".to_string();
|
//parent_meta.name.name = "..".to_string();
|
||||||
|
|
||||||
content.push(current_meta);
|
content.push(current_meta);
|
||||||
content.push(parent_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,
|
Ok(content) => entry_meta.content = content,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
print_error!("lsd: {}: {}\n", path.display(), 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() {
|
let metadata = if read_link(path).is_ok() {
|
||||||
// If the file is a link, retrieve the metadata without following
|
// If the file is a link, retrieve the metadata without following
|
||||||
// the link.
|
// the link.
|
||||||
|
@ -219,7 +221,7 @@ impl Meta {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
inode,
|
inode,
|
||||||
path: path.to_path_buf(),
|
path: path.to_path_buf(),
|
||||||
symlink: SymLink::from(path.as_path()),
|
symlink: SymLink::from(path),
|
||||||
size: Size::from(&metadata),
|
size: Size::from(&metadata),
|
||||||
date: Date::from(&metadata),
|
date: Date::from(&metadata),
|
||||||
indicator: Indicator::from(file_type),
|
indicator: Indicator::from(file_type),
|
||||||
|
|
|
@ -2,23 +2,22 @@ use crate::color::{ColoredString, Colors, Elem};
|
||||||
use crate::icon::Icons;
|
use crate::icon::Icons;
|
||||||
use crate::meta::filetype::FileType;
|
use crate::meta::filetype::FileType;
|
||||||
use std::cmp::{Ordering, PartialOrd};
|
use std::cmp::{Ordering, PartialOrd};
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::ffi::OsStr;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq)]
|
#[derive(Clone, Debug, Eq)]
|
||||||
pub struct Name {
|
pub struct Name {
|
||||||
pub name: String,
|
path: PathBuf,
|
||||||
path: String,
|
|
||||||
extension: Option<String>,
|
extension: Option<String>,
|
||||||
file_type: FileType,
|
file_type: FileType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Name {
|
impl Name {
|
||||||
pub fn new(path: &Path, file_type: FileType) -> Self {
|
pub fn file_name(&self) -> Option<&OsStr> {
|
||||||
let name = match path.file_name() {
|
self.path.file_name()
|
||||||
Some(name) => name.to_string_lossy().to_string(),
|
}
|
||||||
None => path.to_string_lossy().to_string(),
|
|
||||||
};
|
|
||||||
|
|
||||||
|
pub fn new(path: &Path, file_type: FileType) -> Self {
|
||||||
let mut extension = None;
|
let mut extension = None;
|
||||||
if let Some(res) = path.extension() {
|
if let Some(res) = path.extension() {
|
||||||
extension = Some(
|
extension = Some(
|
||||||
|
@ -28,11 +27,8 @@ impl Name {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let path_string = path.to_string_lossy().to_string();
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
name,
|
path: PathBuf::from(path),
|
||||||
path: path_string,
|
|
||||||
extension,
|
extension,
|
||||||
file_type,
|
file_type,
|
||||||
}
|
}
|
||||||
|
@ -40,11 +36,9 @@ impl Name {
|
||||||
|
|
||||||
pub fn name_string(&self, icons: &Icons) -> String {
|
pub fn name_string(&self, icons: &Icons) -> String {
|
||||||
let icon = icons.get(self);
|
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();
|
format!("{}{}", icon.as_str(), display)
|
||||||
content += &self.name;
|
|
||||||
content
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, colors: &Colors, icons: &Icons) -> ColoredString {
|
pub fn render(&self, colors: &Colors, icons: &Icons) -> ColoredString {
|
||||||
|
@ -64,12 +58,8 @@ impl Name {
|
||||||
colors.colorize_using_path(content, &self.path, &elem)
|
colors.colorize_using_path(content, &self.path, &elem)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> String {
|
pub fn extension(&self) -> Option<&str> {
|
||||||
self.name.clone()
|
self.extension.as_ref().map(|string| string.as_str())
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extension(&self) -> Option<String> {
|
|
||||||
self.extension.clone()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file_type(&self) -> FileType {
|
pub fn file_type(&self) -> FileType {
|
||||||
|
@ -79,19 +69,19 @@ impl Name {
|
||||||
|
|
||||||
impl Ord for Name {
|
impl Ord for Name {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
self.name.to_lowercase().cmp(&other.name.to_lowercase())
|
self.path.cmp(&other.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for Name {
|
impl PartialOrd for Name {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
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 {
|
impl PartialEq for Name {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
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 colors = Colors::new(color::Theme::NoLscolors);
|
||||||
let file_type = FileType::new(&meta, &Permissions::from(&meta));
|
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!(
|
assert_eq!(
|
||||||
Colour::Fixed(44).paint(" target.tmp"),
|
Colour::Fixed(44).paint(" target.tmp"),
|
||||||
|
@ -198,7 +188,7 @@ mod test {
|
||||||
|
|
||||||
let colors = Colors::new(color::Theme::NoLscolors);
|
let colors = Colors::new(color::Theme::NoLscolors);
|
||||||
let file_type = FileType::new(&meta, &Permissions::from(&meta));
|
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!(
|
assert_eq!(
|
||||||
Colour::Fixed(184).paint(" pipe.tmp"),
|
Colour::Fixed(184).paint(" pipe.tmp"),
|
||||||
|
@ -229,6 +219,7 @@ mod test {
|
||||||
let path = Path::new("some-file.txt");
|
let path = Path::new("some-file.txt");
|
||||||
|
|
||||||
let name = Name::new(
|
let name = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path,
|
&path,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -236,7 +227,7 @@ mod test {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(Some(String::from("txt")), name.extension());
|
assert_eq!(Some("txt"), name.extension());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -244,6 +235,7 @@ mod test {
|
||||||
let path = Path::new(".gitignore");
|
let path = Path::new(".gitignore");
|
||||||
|
|
||||||
let name = Name::new(
|
let name = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path,
|
&path,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -258,6 +250,7 @@ mod test {
|
||||||
fn test_order_impl_is_case_insensitive() {
|
fn test_order_impl_is_case_insensitive() {
|
||||||
let path_1 = Path::new("AAAA");
|
let path_1 = Path::new("AAAA");
|
||||||
let name_1 = Name::new(
|
let name_1 = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_1,
|
&path_1,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -267,6 +260,7 @@ mod test {
|
||||||
|
|
||||||
let path_2 = Path::new("aaaa");
|
let path_2 = Path::new("aaaa");
|
||||||
let name_2 = Name::new(
|
let name_2 = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_2,
|
&path_2,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -281,6 +275,7 @@ mod test {
|
||||||
fn test_partial_order_impl() {
|
fn test_partial_order_impl() {
|
||||||
let path_a = Path::new("aaaa");
|
let path_a = Path::new("aaaa");
|
||||||
let name_a = Name::new(
|
let name_a = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_a,
|
&path_a,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -290,6 +285,7 @@ mod test {
|
||||||
|
|
||||||
let path_z = Path::new("zzzz");
|
let path_z = Path::new("zzzz");
|
||||||
let name_z = Name::new(
|
let name_z = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_z,
|
&path_z,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -304,6 +300,7 @@ mod test {
|
||||||
fn test_partial_order_impl_is_case_insensitive() {
|
fn test_partial_order_impl_is_case_insensitive() {
|
||||||
let path_a = Path::new("aaaa");
|
let path_a = Path::new("aaaa");
|
||||||
let name_a = Name::new(
|
let name_a = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_a,
|
&path_a,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -313,6 +310,7 @@ mod test {
|
||||||
|
|
||||||
let path_z = Path::new("ZZZZ");
|
let path_z = Path::new("ZZZZ");
|
||||||
let name_z = Name::new(
|
let name_z = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_z,
|
&path_z,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -327,6 +325,7 @@ mod test {
|
||||||
fn test_partial_eq_impl() {
|
fn test_partial_eq_impl() {
|
||||||
let path_1 = Path::new("aaaa");
|
let path_1 = Path::new("aaaa");
|
||||||
let name_1 = Name::new(
|
let name_1 = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_1,
|
&path_1,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -336,6 +335,7 @@ mod test {
|
||||||
|
|
||||||
let path_2 = Path::new("aaaa");
|
let path_2 = Path::new("aaaa");
|
||||||
let name_2 = Name::new(
|
let name_2 = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_2,
|
&path_2,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -350,6 +350,7 @@ mod test {
|
||||||
fn test_partial_eq_impl_is_case_insensitive() {
|
fn test_partial_eq_impl_is_case_insensitive() {
|
||||||
let path_1 = Path::new("AAAA");
|
let path_1 = Path::new("AAAA");
|
||||||
let name_1 = Name::new(
|
let name_1 = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_1,
|
&path_1,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
@ -359,6 +360,7 @@ mod test {
|
||||||
|
|
||||||
let path_2 = Path::new("aaaa");
|
let path_2 = Path::new("aaaa");
|
||||||
let name_2 = Name::new(
|
let name_2 = Name::new(
|
||||||
|
Path::new("/"),
|
||||||
&path_2,
|
&path_2,
|
||||||
FileType::File {
|
FileType::File {
|
||||||
uid: false,
|
uid: false,
|
||||||
|
|
Loading…
Reference in a new issue