lsd/src/meta/name.rs

101 lines
2.2 KiB
Rust
Raw Normal View History

use color::{ColoredString, Colors, Elem};
2018-12-01 17:59:53 +00:00
use meta::filetype::FileType;
use std::cmp::{Ordering, PartialOrd};
use std::path::Path;
#[derive(Debug, Eq)]
pub struct Name {
name: String,
extension: Option<String>,
file_type: FileType,
}
impl Name {
pub fn new(path: &Path, file_type: FileType) -> Self {
2018-12-10 17:26:34 +00:00
let name = path
2018-12-01 17:59:53 +00:00
.file_name()
.expect("failed to retrieve file name")
2018-12-05 20:13:36 +00:00
.to_string_lossy()
2018-12-01 17:59:53 +00:00
.to_string();
let mut extension = None;
if let Some(res) = path.extension() {
extension = Some(
res.to_str()
.expect("failed to encode file name")
.to_string(),
);
}
Name {
name,
extension,
file_type,
}
}
pub fn render(&self, colors: &Colors) -> ColoredString {
2018-12-01 17:59:53 +00:00
let mut content = String::with_capacity(self.name.len() + 3 /* spaces */);
2018-12-08 22:03:18 +00:00
let elem = match self.file_type {
FileType::Directory => &Elem::Dir,
FileType::SymLink => &Elem::SymLink,
_ => &Elem::File,
2018-12-01 17:59:53 +00:00
};
content += &self.name;
colors.colorize(content, elem)
2018-12-01 17:59:53 +00:00
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn extension(&self) -> Option<String> {
self.extension.clone()
}
2018-12-08 18:52:56 +00:00
pub fn file_type(&self) -> FileType {
self.file_type
2018-12-01 17:59:53 +00:00
}
pub fn is_hidden(&self) -> bool {
self.name.starts_with('.')
}
}
impl Ord for Name {
fn cmp(&self, other: &Name) -> Ordering {
self.name.cmp(&other.name)
}
}
impl PartialOrd for Name {
fn partial_cmp(&self, other: &Name) -> Option<Ordering> {
Some(self.name.cmp(&other.name))
}
}
impl PartialEq for Name {
fn eq(&self, other: &Name) -> bool {
let mut other_name = other.name.chars();
if self.name.len() != other.name.len() {
return false;
}
for c in self.name.chars() {
if let Some(c2) = other_name.next() {
if c != c2 {
return false;
}
} else {
return false;
}
}
true
}
}