From 1c852cab1c1e658ef9fc86adbd09972cb7e823b4 Mon Sep 17 00:00:00 2001 From: Peltoche Date: Sun, 25 Nov 2018 16:30:43 +0100 Subject: [PATCH] Add the node type at before the permissions --- src/color.rs | 30 ++++++++++++++++++++++++---- src/formatter.rs | 51 ++++++++++++++++++++++++------------------------ src/meta.rs | 4 ++-- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/color.rs b/src/color.rs index 5cac7d9..a96361e 100644 --- a/src/color.rs +++ b/src/color.rs @@ -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 = { + 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 + }; +} diff --git a/src/formatter.rs b/src/formatter.rs index 3023580..e3b4c4f 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -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() diff --git a/src/meta.rs b/src/meta.rs index 9fa055a..6cab982 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -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.