|
@@ -1,16 +1,17 @@
|
|
|
|
|
+use ansi_term::{ANSIString, Style};
|
|
|
|
|
+
|
|
|
use fs::fields as f;
|
|
use fs::fields as f;
|
|
|
-use output::colours::Colours;
|
|
|
|
|
use output::cell::{TextCell, DisplayWidth};
|
|
use output::cell::{TextCell, DisplayWidth};
|
|
|
-use ansi_term::{ANSIString, Style};
|
|
|
|
|
|
|
+use output::render::FiletypeColours;
|
|
|
|
|
|
|
|
|
|
|
|
|
impl f::PermissionsPlus {
|
|
impl f::PermissionsPlus {
|
|
|
- pub fn render(&self, colours: &Colours) -> TextCell {
|
|
|
|
|
|
|
+ pub fn render<C: Colours+FiletypeColours>(&self, colours: &C) -> TextCell {
|
|
|
let mut chars = vec![ self.file_type.render(colours) ];
|
|
let mut chars = vec![ self.file_type.render(colours) ];
|
|
|
chars.extend(self.permissions.render(colours, self.file_type.is_regular_file()));
|
|
chars.extend(self.permissions.render(colours, self.file_type.is_regular_file()));
|
|
|
|
|
|
|
|
if self.xattrs {
|
|
if self.xattrs {
|
|
|
- chars.push(colours.perms.attribute.paint("@"));
|
|
|
|
|
|
|
+ chars.push(colours.attribute().paint("@"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// As these are all ASCII characters, we can guarantee that they’re
|
|
// As these are all ASCII characters, we can guarantee that they’re
|
|
@@ -23,87 +24,115 @@ impl f::PermissionsPlus {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
impl f::Permissions {
|
|
impl f::Permissions {
|
|
|
- pub fn render(&self, colours: &Colours, is_regular_file: bool) -> Vec<ANSIString<'static>> {
|
|
|
|
|
|
|
+ pub fn render<C: Colours>(&self, colours: &C, is_regular_file: bool) -> Vec<ANSIString<'static>> {
|
|
|
|
|
+
|
|
|
let bit = |bit, chr: &'static str, style: Style| {
|
|
let bit = |bit, chr: &'static str, style: Style| {
|
|
|
- if bit { style.paint(chr) } else { colours.punctuation.paint("-") }
|
|
|
|
|
|
|
+ if bit { style.paint(chr) } else { colours.dash().paint("-") }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
vec![
|
|
vec![
|
|
|
- bit(self.user_read, "r", colours.perms.user_read),
|
|
|
|
|
- bit(self.user_write, "w", colours.perms.user_write),
|
|
|
|
|
|
|
+ bit(self.user_read, "r", colours.user_read()),
|
|
|
|
|
+ bit(self.user_write, "w", colours.user_write()),
|
|
|
self.user_execute_bit(colours, is_regular_file),
|
|
self.user_execute_bit(colours, is_regular_file),
|
|
|
- bit(self.group_read, "r", colours.perms.group_read),
|
|
|
|
|
- bit(self.group_write, "w", colours.perms.group_write),
|
|
|
|
|
|
|
+ bit(self.group_read, "r", colours.group_read()),
|
|
|
|
|
+ bit(self.group_write, "w", colours.group_write()),
|
|
|
self.group_execute_bit(colours),
|
|
self.group_execute_bit(colours),
|
|
|
- bit(self.other_read, "r", colours.perms.other_read),
|
|
|
|
|
- bit(self.other_write, "w", colours.perms.other_write),
|
|
|
|
|
|
|
+ bit(self.other_read, "r", colours.other_read()),
|
|
|
|
|
+ bit(self.other_write, "w", colours.other_write()),
|
|
|
self.other_execute_bit(colours)
|
|
self.other_execute_bit(colours)
|
|
|
]
|
|
]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fn user_execute_bit(&self, colours: &Colours, is_regular_file: bool) -> ANSIString<'static> {
|
|
|
|
|
|
|
+ fn user_execute_bit<C: Colours>(&self, colours: &C, is_regular_file: bool) -> ANSIString<'static> {
|
|
|
match (self.user_execute, self.setuid, is_regular_file) {
|
|
match (self.user_execute, self.setuid, is_regular_file) {
|
|
|
- (false, false, _) => colours.punctuation.paint("-"),
|
|
|
|
|
- (true, false, false) => colours.perms.user_execute_other.paint("x"),
|
|
|
|
|
- (true, false, true) => colours.perms.user_execute_file.paint("x"),
|
|
|
|
|
- (false, true, _) => colours.perms.special_other.paint("S"),
|
|
|
|
|
- (true, true, false) => colours.perms.special_other.paint("s"),
|
|
|
|
|
- (true, true, true) => colours.perms.special_user_file.paint("s"),
|
|
|
|
|
|
|
+ (false, false, _) => colours.dash().paint("-"),
|
|
|
|
|
+ (true, false, false) => colours.user_execute_other().paint("x"),
|
|
|
|
|
+ (true, false, true) => colours.user_execute_file().paint("x"),
|
|
|
|
|
+ (false, true, _) => colours.special_other().paint("S"),
|
|
|
|
|
+ (true, true, false) => colours.special_other().paint("s"),
|
|
|
|
|
+ (true, true, true) => colours.special_user_file().paint("s"),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fn group_execute_bit(&self, colours: &Colours) -> ANSIString<'static> {
|
|
|
|
|
|
|
+ fn group_execute_bit<C: Colours>(&self, colours: &C) -> ANSIString<'static> {
|
|
|
match (self.group_execute, self.setgid) {
|
|
match (self.group_execute, self.setgid) {
|
|
|
- (false, false) => colours.punctuation.paint("-"),
|
|
|
|
|
- (true, false) => colours.perms.group_execute.paint("x"),
|
|
|
|
|
- (false, true) => colours.perms.special_other.paint("S"),
|
|
|
|
|
- (true, true) => colours.perms.special_other.paint("s"),
|
|
|
|
|
|
|
+ (false, false) => colours.dash().paint("-"),
|
|
|
|
|
+ (true, false) => colours.group_execute().paint("x"),
|
|
|
|
|
+ (false, true) => colours.special_other().paint("S"),
|
|
|
|
|
+ (true, true) => colours.special_other().paint("s"),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fn other_execute_bit(&self, colours: &Colours) -> ANSIString<'static> {
|
|
|
|
|
|
|
+ fn other_execute_bit<C: Colours>(&self, colours: &C) -> ANSIString<'static> {
|
|
|
match (self.other_execute, self.sticky) {
|
|
match (self.other_execute, self.sticky) {
|
|
|
- (false, false) => colours.punctuation.paint("-"),
|
|
|
|
|
- (true, false) => colours.perms.other_execute.paint("x"),
|
|
|
|
|
- (false, true) => colours.perms.special_other.paint("T"),
|
|
|
|
|
- (true, true) => colours.perms.special_other.paint("t"),
|
|
|
|
|
|
|
+ (false, false) => colours.dash().paint("-"),
|
|
|
|
|
+ (true, false) => colours.other_execute().paint("x"),
|
|
|
|
|
+ (false, true) => colours.special_other().paint("T"),
|
|
|
|
|
+ (true, true) => colours.special_other().paint("t"),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-impl f::Type {
|
|
|
|
|
- pub fn render(&self, colours: &Colours) -> ANSIString<'static> {
|
|
|
|
|
- match *self {
|
|
|
|
|
- f::Type::File => colours.filetypes.normal.paint("."),
|
|
|
|
|
- f::Type::Directory => colours.filetypes.directory.paint("d"),
|
|
|
|
|
- f::Type::Pipe => colours.filetypes.pipe.paint("|"),
|
|
|
|
|
- f::Type::Link => colours.filetypes.symlink.paint("l"),
|
|
|
|
|
- f::Type::CharDevice => colours.filetypes.device.paint("c"),
|
|
|
|
|
- f::Type::BlockDevice => colours.filetypes.device.paint("b"),
|
|
|
|
|
- f::Type::Socket => colours.filetypes.socket.paint("s"),
|
|
|
|
|
- f::Type::Special => colours.filetypes.special.paint("?"),
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
|
|
+pub trait Colours {
|
|
|
|
|
+ fn dash(&self) -> Style;
|
|
|
|
|
+
|
|
|
|
|
+ fn user_read(&self) -> Style;
|
|
|
|
|
+ fn user_write(&self) -> Style;
|
|
|
|
|
+ fn user_execute_file(&self) -> Style;
|
|
|
|
|
+ fn user_execute_other(&self) -> Style;
|
|
|
|
|
+
|
|
|
|
|
+ fn group_read(&self) -> Style;
|
|
|
|
|
+ fn group_write(&self) -> Style;
|
|
|
|
|
+ fn group_execute(&self) -> Style;
|
|
|
|
|
+
|
|
|
|
|
+ fn other_read(&self) -> Style;
|
|
|
|
|
+ fn other_write(&self) -> Style;
|
|
|
|
|
+ fn other_execute(&self) -> Style;
|
|
|
|
|
+
|
|
|
|
|
+ fn special_user_file(&self) -> Style;
|
|
|
|
|
+ fn special_other(&self) -> Style;
|
|
|
|
|
+
|
|
|
|
|
+ fn attribute(&self) -> Style;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
#[cfg(test)]
|
|
|
#[allow(unused_results)]
|
|
#[allow(unused_results)]
|
|
|
pub mod test {
|
|
pub mod test {
|
|
|
- use output::colours::Colours;
|
|
|
|
|
|
|
+ use super::Colours;
|
|
|
use output::cell::TextCellContents;
|
|
use output::cell::TextCellContents;
|
|
|
use fs::fields as f;
|
|
use fs::fields as f;
|
|
|
|
|
|
|
|
use ansi_term::Colour::*;
|
|
use ansi_term::Colour::*;
|
|
|
|
|
+ use ansi_term::Style;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ struct TestColours;
|
|
|
|
|
+
|
|
|
|
|
+ impl Colours for TestColours {
|
|
|
|
|
+ fn dash(&self) -> Style { Fixed(11).normal() }
|
|
|
|
|
+ fn user_read(&self) -> Style { Fixed(101).normal() }
|
|
|
|
|
+ fn user_write(&self) -> Style { Fixed(102).normal() }
|
|
|
|
|
+ fn user_execute_file(&self) -> Style { Fixed(103).normal() }
|
|
|
|
|
+ fn user_execute_other(&self) -> Style { Fixed(113).normal() }
|
|
|
|
|
+ fn group_read(&self) -> Style { Fixed(104).normal() }
|
|
|
|
|
+ fn group_write(&self) -> Style { Fixed(105).normal() }
|
|
|
|
|
+ fn group_execute(&self) -> Style { Fixed(106).normal() }
|
|
|
|
|
+ fn other_read(&self) -> Style { Fixed(107).normal() }
|
|
|
|
|
+ fn other_write(&self) -> Style { Fixed(108).normal() }
|
|
|
|
|
+ fn other_execute(&self) -> Style { Fixed(109).normal() }
|
|
|
|
|
+ fn special_user_file(&self) -> Style { Fixed(110).normal() }
|
|
|
|
|
+ fn special_other(&self) -> Style { Fixed(111).normal() }
|
|
|
|
|
+ fn attribute(&self) -> Style { Fixed(112).normal() }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
|
fn negate() {
|
|
fn negate() {
|
|
|
- let mut colours = Colours::default();
|
|
|
|
|
- colours.punctuation = Fixed(11).normal();
|
|
|
|
|
-
|
|
|
|
|
let bits = f::Permissions {
|
|
let bits = f::Permissions {
|
|
|
user_read: false, user_write: false, user_execute: false, setuid: false,
|
|
user_read: false, user_write: false, user_execute: false, setuid: false,
|
|
|
group_read: false, group_write: false, group_execute: false, setgid: false,
|
|
group_read: false, group_write: false, group_execute: false, setgid: false,
|
|
@@ -116,25 +145,12 @@ pub mod test {
|
|
|
Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(11).paint("-"),
|
|
Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(11).paint("-"),
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- assert_eq!(expected, bits.render(&colours, false).into())
|
|
|
|
|
|
|
+ assert_eq!(expected, bits.render(&TestColours, false).into())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
|
fn affirm() {
|
|
fn affirm() {
|
|
|
- let mut colours = Colours::default();
|
|
|
|
|
- colours.perms.user_read = Fixed(101).normal();
|
|
|
|
|
- colours.perms.user_write = Fixed(102).normal();
|
|
|
|
|
- colours.perms.user_execute_file = Fixed(103).normal();
|
|
|
|
|
-
|
|
|
|
|
- colours.perms.group_read = Fixed(104).normal();
|
|
|
|
|
- colours.perms.group_write = Fixed(105).normal();
|
|
|
|
|
- colours.perms.group_execute = Fixed(106).normal();
|
|
|
|
|
-
|
|
|
|
|
- colours.perms.other_read = Fixed(107).normal();
|
|
|
|
|
- colours.perms.other_write = Fixed(108).normal();
|
|
|
|
|
- colours.perms.other_execute = Fixed(109).normal();
|
|
|
|
|
-
|
|
|
|
|
let bits = f::Permissions {
|
|
let bits = f::Permissions {
|
|
|
user_read: true, user_write: true, user_execute: true, setuid: false,
|
|
user_read: true, user_write: true, user_execute: true, setuid: false,
|
|
|
group_read: true, group_write: true, group_execute: true, setgid: false,
|
|
group_read: true, group_write: true, group_execute: true, setgid: false,
|
|
@@ -147,17 +163,12 @@ pub mod test {
|
|
|
Fixed(107).paint("r"), Fixed(108).paint("w"), Fixed(109).paint("x"),
|
|
Fixed(107).paint("r"), Fixed(108).paint("w"), Fixed(109).paint("x"),
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- assert_eq!(expected, bits.render(&colours, true).into())
|
|
|
|
|
|
|
+ assert_eq!(expected, bits.render(&TestColours, true).into())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
|
fn specials() {
|
|
fn specials() {
|
|
|
- let mut colours = Colours::default();
|
|
|
|
|
- colours.punctuation = Fixed(11).normal();
|
|
|
|
|
- colours.perms.special_user_file = Fixed(77).normal();
|
|
|
|
|
- colours.perms.special_other = Fixed(88).normal();
|
|
|
|
|
-
|
|
|
|
|
let bits = f::Permissions {
|
|
let bits = f::Permissions {
|
|
|
user_read: false, user_write: false, user_execute: true, setuid: true,
|
|
user_read: false, user_write: false, user_execute: true, setuid: true,
|
|
|
group_read: false, group_write: false, group_execute: true, setgid: true,
|
|
group_read: false, group_write: false, group_execute: true, setgid: true,
|
|
@@ -165,21 +176,17 @@ pub mod test {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
let expected = TextCellContents::from(vec![
|
|
let expected = TextCellContents::from(vec![
|
|
|
- Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(77).paint("s"),
|
|
|
|
|
- Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(88).paint("s"),
|
|
|
|
|
- Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(88).paint("t"),
|
|
|
|
|
|
|
+ Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(110).paint("s"),
|
|
|
|
|
+ Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(111).paint("s"),
|
|
|
|
|
+ Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(111).paint("t"),
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- assert_eq!(expected, bits.render(&colours, true).into())
|
|
|
|
|
|
|
+ assert_eq!(expected, bits.render(&TestColours, true).into())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
|
fn extra_specials() {
|
|
fn extra_specials() {
|
|
|
- let mut colours = Colours::default();
|
|
|
|
|
- colours.punctuation = Fixed(11).normal();
|
|
|
|
|
- colours.perms.special_other = Fixed(88).normal();
|
|
|
|
|
-
|
|
|
|
|
let bits = f::Permissions {
|
|
let bits = f::Permissions {
|
|
|
user_read: false, user_write: false, user_execute: false, setuid: true,
|
|
user_read: false, user_write: false, user_execute: false, setuid: true,
|
|
|
group_read: false, group_write: false, group_execute: false, setgid: true,
|
|
group_read: false, group_write: false, group_execute: false, setgid: true,
|
|
@@ -187,11 +194,11 @@ pub mod test {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
let expected = TextCellContents::from(vec![
|
|
let expected = TextCellContents::from(vec![
|
|
|
- Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(88).paint("S"),
|
|
|
|
|
- Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(88).paint("S"),
|
|
|
|
|
- Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(88).paint("T"),
|
|
|
|
|
|
|
+ Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(111).paint("S"),
|
|
|
|
|
+ Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(111).paint("S"),
|
|
|
|
|
+ Fixed(11).paint("-"), Fixed(11).paint("-"), Fixed(111).paint("T"),
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- assert_eq!(expected, bits.render(&colours, true).into())
|
|
|
|
|
|
|
+ assert_eq!(expected, bits.render(&TestColours, true).into())
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|