Add the file_type into the Permission struct

This commit is contained in:
Peltoche 2018-12-01 14:16:23 +01:00
parent 21cc041775
commit 033181ca9b
No known key found for this signature in database
GPG key ID: CED68D0487156952
3 changed files with 13 additions and 16 deletions

View file

@ -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

View file

@ -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();

View file

@ -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 {