Add the node type at before the permissions

This commit is contained in:
Peltoche 2018-11-25 16:30:43 +01:00
parent 6199c8d888
commit 1c852cab1c
No known key found for this signature in database
GPG key ID: CED68D0487156952
3 changed files with 54 additions and 31 deletions

View file

@ -4,9 +4,11 @@ use std::collections::HashMap;
#[allow(dead_code)]
#[derive(Hash, Debug, Eq, PartialEq, Copy, Clone)]
pub enum Elem {
/// Path Kind
/// Node type
File,
UnrecognizedFile,
RecognizedFile,
SymLink,
Dir,
/// Permissions
@ -48,9 +50,10 @@ lazy_static! {
m.insert(Elem::NoAccess, Colour::RGB(0xD7, 0x89, 0x89));
// Path Kind
m.insert(Elem::UnrecognizedFile, Colour::RGB(0xFF, 0xFF, 0x04)); // gold
m.insert(Elem::RecognizedFile, Colour::RGB(0x04, 0xFF, 0x04)); // limon
m.insert(Elem::Dir, Colour::RGB(0x00, 0xAF, 0xFF)); // dodgerblue
m.insert(Elem::UnrecognizedFile, Colour::RGB(0xFF, 0xFF, 0x04));
m.insert(Elem::RecognizedFile, Colour::RGB(0x04, 0xFF, 0x04));
m.insert(Elem::Dir, Colour::RGB(0x00, 0xAF, 0xFF));
m.insert(Elem::SymLink, Colour::RGB(0xFF, 0x00, 0x00));
// Last Time Modified
m.insert(Elem::HourOld, Colour::RGB(0x2C, 0xFF, 0x2C));
@ -68,3 +71,22 @@ lazy_static! {
m
};
}
lazy_static! {
pub static ref PrecomputedElems : HashMap<Elem, String> = {
let mut m = HashMap::new();
// Permissions
m.insert(Elem::Read, Colors[&Elem::Read].paint(String::from("r")).to_string());
m.insert(Elem::Write, Colors[&Elem::Write].paint(String::from("w")).to_string());
m.insert(Elem::Exec, Colors[&Elem::Exec].paint(String::from("x")).to_string());
m.insert(Elem::NoAccess, Colors[&Elem::NoAccess].paint(String::from("-")).to_string());
// Note types
m.insert(Elem::File , Colors[&Elem::UnrecognizedFile].paint(String::from(".")).to_string());
m.insert(Elem::Dir, Colors[&Elem::Dir].paint(String::from("d")).to_string());
m.insert(Elem::SymLink, Colors[&Elem::SymLink].paint(String::from("l")).to_string());
m
};
}

View file

@ -1,4 +1,4 @@
use color::{Colors, Elem};
use color::{Colors, Elem, PrecomputedElems};
use icon;
use meta::{Meta, Type};
use std::os::unix::fs::PermissionsExt;
@ -38,7 +38,7 @@ impl Formatter {
pub fn format_symlink(&self, meta: &Meta) -> String {
let mut content = String::new();
if let Type::Symlink(ref target) = meta.node_type {
if let Type::SymLink(ref target) = meta.node_type {
let color = Colors[&Elem::Link];
content += &color.paint(String::from("") + &color.paint(target).to_string());
}
@ -75,67 +75,68 @@ impl Formatter {
}
pub fn format_permissions(&self, meta: &Meta) -> String {
let mut res = String::with_capacity(10);
let mut res = String::with_capacity(11);
let mode = meta.metadata.permissions().mode();
let read_perm = Colors[&Elem::Read].paint(String::from("r")).to_string();
let write_perm = Colors[&Elem::Write].paint(String::from("w")).to_string();
let exec_perm = Colors[&Elem::Exec].paint(String::from("x")).to_string();
let no_access = Colors[&Elem::NoAccess].paint(String::from("-")).to_string();
match meta.node_type {
Type::File => res += PrecomputedElems[&Elem::File].as_str(),
Type::Directory => res += PrecomputedElems[&Elem::Dir].as_str(),
Type::SymLink(_) => res += PrecomputedElems[&Elem::SymLink].as_str(),
}
// User Read Permisssions
match mode & 0o400 {
0 => res += no_access.as_str(),
_ => res += read_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Read].as_str(),
}
// User Write Permisssions
match mode & 0o200 {
0 => res += no_access.as_str(),
_ => res += write_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Write].as_str(),
}
// User Exec Permisssions
match mode & 0o100 {
0 => res += no_access.as_str(),
_ => res += exec_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Exec].as_str(),
}
// Group Read Permisssions
match mode & 0o040 {
0 => res += no_access.as_str(),
_ => res += read_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Read].as_str(),
}
// Group Write Permisssions
match mode & 0o020 {
0 => res += no_access.as_str(),
_ => res += write_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Write].as_str(),
}
// Group Exec Permisssions
match mode & 0o010 {
0 => res += no_access.as_str(),
_ => res += exec_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Exec].as_str(),
}
// Other Read Permisssions
match mode & 0o040 {
0 => res += no_access.as_str(),
_ => res += read_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Read].as_str(),
}
// Other Write Permisssions
match mode & 0o020 {
0 => res += no_access.as_str(),
_ => res += write_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Write].as_str(),
}
// Other Exec Permisssions
match mode & 0o010 {
0 => res += no_access.as_str(),
_ => res += exec_perm.as_str(),
0 => res += PrecomputedElems[&Elem::NoAccess].as_str(),
_ => res += PrecomputedElems[&Elem::Exec].as_str(),
}
res.to_string()

View file

@ -18,7 +18,7 @@ pub enum MetaError {
#[derive(Debug)]
pub enum Type {
Symlink(String),
SymLink(String),
File,
Directory,
}
@ -80,7 +80,7 @@ impl Meta {
.expect("failed to convert symlink to str")
.to_string();
(meta, Type::Symlink(target))
(meta, Type::SymLink(target))
}
_ => {
// This path is a file.