Move a file's type out of its permissions field

This commit is contained in:
Benjamin Sago 2016-04-16 20:01:45 +01:00
parent 570fac0c18
commit b83844f384
3 changed files with 17 additions and 10 deletions

View File

@ -44,10 +44,18 @@ pub enum Type {
File, Directory, Pipe, Link, Special, File, Directory, Pipe, Link, Special,
} }
impl Type {
pub fn is_regular_file(&self) -> bool {
match *self {
Type::File => true,
_ => false,
}
}
}
/// The files Unix permission bitfield, with one entry per bit. /// The files Unix permission bitfield, with one entry per bit.
pub struct Permissions { pub struct Permissions {
pub file_type: Type,
pub user_read: bool, pub user_read: bool,
pub user_write: bool, pub user_write: bool,
pub user_execute: bool, pub user_execute: bool,

View File

@ -270,7 +270,7 @@ impl<'dir> File<'dir> {
/// This is used in the leftmost column of the permissions column. /// This is used in the leftmost column of the permissions column.
/// Although the file type can usually be guessed from the colour of the /// Although the file type can usually be guessed from the colour of the
/// file, `ls` puts this character there, so people will expect it. /// file, `ls` puts this character there, so people will expect it.
fn type_char(&self) -> f::Type { pub fn type_char(&self) -> f::Type {
if self.is_file() { if self.is_file() {
f::Type::File f::Type::File
} }
@ -298,7 +298,6 @@ impl<'dir> File<'dir> {
let has_bit = |bit| { bits & bit == bit }; let has_bit = |bit| { bits & bit == bit };
f::Permissions { f::Permissions {
file_type: self.type_char(),
user_read: has_bit(modes::USER_READ), user_read: has_bit(modes::USER_READ),
user_write: has_bit(modes::USER_WRITE), user_write: has_bit(modes::USER_WRITE),
user_execute: has_bit(modes::USER_EXECUTE), user_execute: has_bit(modes::USER_EXECUTE),

View File

@ -482,7 +482,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
use output::column::TimeType::*; use output::column::TimeType::*;
match *column { match *column {
Column::Permissions => self.render_permissions(file.permissions(), xattrs), Column::Permissions => self.render_permissions(file.type_char(), file.permissions(), xattrs),
Column::FileSize(fmt) => self.render_size(file.size(), fmt), Column::FileSize(fmt) => self.render_size(file.size(), fmt),
Column::Timestamp(Modified) => self.render_time(file.modified_time()), Column::Timestamp(Modified) => self.render_time(file.modified_time()),
Column::Timestamp(Created) => self.render_time(file.created_time()), Column::Timestamp(Created) => self.render_time(file.created_time()),
@ -496,7 +496,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
} }
} }
fn render_permissions(&self, permissions: f::Permissions, xattrs: bool) -> TextCell { fn render_permissions(&self, file_type: f::Type, permissions: f::Permissions, xattrs: bool) -> TextCell {
let perms = self.opts.colours.perms; let perms = self.opts.colours.perms;
let types = self.opts.colours.filetypes; let types = self.opts.colours.filetypes;
@ -504,7 +504,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
if bit { style.paint(chr) } else { self.opts.colours.punctuation.paint("-") } if bit { style.paint(chr) } else { self.opts.colours.punctuation.paint("-") }
}; };
let file_type = match permissions.file_type { let type_char = match file_type {
f::Type::File => types.normal.paint("."), f::Type::File => types.normal.paint("."),
f::Type::Directory => types.directory.paint("d"), f::Type::Directory => types.directory.paint("d"),
f::Type::Pipe => types.special.paint("|"), f::Type::Pipe => types.special.paint("|"),
@ -512,11 +512,11 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
f::Type::Special => types.special.paint("?"), f::Type::Special => types.special.paint("?"),
}; };
let x_colour = if let f::Type::File = permissions.file_type { perms.user_execute_file } let x_colour = if file_type.is_regular_file() { perms.user_execute_file }
else { perms.user_execute_other }; else { perms.user_execute_other };
let mut chars = vec![ let mut chars = vec![
file_type, type_char,
bit(permissions.user_read, "r", perms.user_read), bit(permissions.user_read, "r", perms.user_read),
bit(permissions.user_write, "w", perms.user_write), bit(permissions.user_write, "w", perms.user_write),
bit(permissions.user_execute, "x", x_colour), bit(permissions.user_execute, "x", x_colour),