diff --git a/src/core.rs b/src/core.rs index 3cf30a3..2573556 100644 --- a/src/core.rs +++ b/src/core.rs @@ -106,7 +106,7 @@ impl<'a> Core<'a> { for meta in metas { println!( "{} {} {} {} {} {}{}", - self.formatter.format_permissions(&meta), + meta.permissions.render(), self.formatter.format_user(&meta.user, max_user_length), self.formatter.format_group(&meta.group, max_group_length), meta.size diff --git a/src/formatter.rs b/src/formatter.rs index 98ab218..001349f 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -73,20 +73,6 @@ impl Formatter { color.paint(time.ctime().to_string()).to_string() } - pub fn format_permissions(&self, meta: &Meta) -> String { - let mut res = String::with_capacity(11); - - match meta.node_type { - Type::File => res += &Colors[&Elem::File].paint("."), - Type::Directory => res += &Colors[&Elem::Dir].paint("d"), - Type::SymLink(_) => res += &Colors[&Elem::SymLink].paint("l"), - } - - res += &meta.permissions.render(); - - res.to_string() - } - pub fn format_user(&self, user_name: &str, max_user_size: usize) -> String { if user_name.len() == max_user_size { return Colors[&Elem::User].paint(user_name).to_string(); diff --git a/src/meta/permissions.rs b/src/meta/permissions.rs index c3b5eac..5b0bd40 100644 --- a/src/meta/permissions.rs +++ b/src/meta/permissions.rs @@ -1,3 +1,4 @@ +use super::Type; use ansi_term::{ANSIString, Colour}; use color::{Colors, Elem}; use std::fs::Metadata; @@ -5,6 +6,8 @@ use std::os::unix::fs::PermissionsExt; #[derive(Debug)] pub struct Permissions { + pub file_type: Type, + pub user_read: bool, pub user_write: bool, pub user_execute: bool, @@ -28,6 +31,8 @@ impl<'a> From<&'a Metadata> for Permissions { let has_bit = |bit| bits & bit == bit; Permissions { + file_type: Type::from(meta), + user_read: has_bit(modes::USER_READ), user_write: has_bit(modes::USER_WRITE), user_execute: has_bit(modes::USER_EXECUTE), @@ -49,7 +54,13 @@ impl<'a> From<&'a Metadata> for Permissions { impl Permissions { pub fn render(&self) -> String { - let mut res = String::with_capacity(10); + let mut res = String::with_capacity(11); + + match self.file_type { + Type::File => res += &Colors[&Elem::File].paint("."), + Type::Directory => res += &Colors[&Elem::Dir].paint("d"), + Type::SymLink(_) => res += &Colors[&Elem::SymLink].paint("l"), + } let bit = |bit, chr: &'static str, color: Colour| { if bit {