mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-09-28 05:09:01 +00:00
9b24649d68
This commit adds many traits, all named ‘Colours’, to the code. Each one asks for a colour needed to render a cell: the number of links asks for colours for the number and the multi-link-file special case; the file size asks for number, unit, punctuation, and device ID colours, or it can do a scale with its own colours, however it wants. This is a step towards LS_COLORS compatibility, believe it or not. If a text cell in a column doesn’t depend on Colours to render itself, then the source of the colours is open-ended. I am glad to have not needed any test changes here.
93 lines
2.4 KiB
Rust
93 lines
2.4 KiB
Rust
use ansi_term::Style;
|
|
use users::Users;
|
|
|
|
use fs::fields as f;
|
|
use output::cell::TextCell;
|
|
|
|
|
|
|
|
impl f::User {
|
|
pub fn render<C: Colours, U: Users>(&self, colours: &C, users: &U) -> TextCell {
|
|
let user_name = match users.get_user_by_uid(self.0) {
|
|
Some(user) => user.name().to_owned(),
|
|
None => self.0.to_string(),
|
|
};
|
|
|
|
let style = if users.get_current_uid() == self.0 { colours.you() }
|
|
else { colours.someone_else() };
|
|
TextCell::paint(style, user_name)
|
|
}
|
|
}
|
|
|
|
|
|
pub trait Colours {
|
|
fn you(&self) -> Style;
|
|
fn someone_else(&self) -> Style;
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
#[allow(unused_results)]
|
|
pub mod test {
|
|
use super::Colours;
|
|
use fs::fields as f;
|
|
use output::cell::TextCell;
|
|
|
|
use users::User;
|
|
use users::mock::MockUsers;
|
|
use ansi_term::Colour::*;
|
|
use ansi_term::Style;
|
|
|
|
|
|
struct TestColours;
|
|
|
|
impl Colours for TestColours {
|
|
fn you(&self) -> Style { Red.bold() }
|
|
fn someone_else(&self) -> Style { Blue.underline() }
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn named() {
|
|
let mut users = MockUsers::with_current_uid(1000);
|
|
users.add_user(User::new(1000, "enoch", 100));
|
|
|
|
let user = f::User(1000);
|
|
let expected = TextCell::paint_str(Red.bold(), "enoch");
|
|
assert_eq!(expected, user.render(&TestColours, &users))
|
|
}
|
|
|
|
#[test]
|
|
fn unnamed() {
|
|
let users = MockUsers::with_current_uid(1000);
|
|
|
|
let user = f::User(1000);
|
|
let expected = TextCell::paint_str(Red.bold(), "1000");
|
|
assert_eq!(expected, user.render(&TestColours, &users));
|
|
}
|
|
|
|
#[test]
|
|
fn different_named() {
|
|
let mut users = MockUsers::with_current_uid(0);
|
|
users.add_user(User::new(1000, "enoch", 100));
|
|
|
|
let user = f::User(1000);
|
|
let expected = TextCell::paint_str(Blue.underline(), "enoch");
|
|
assert_eq!(expected, user.render(&TestColours, &users));
|
|
}
|
|
|
|
#[test]
|
|
fn different_unnamed() {
|
|
let user = f::User(1000);
|
|
let expected = TextCell::paint_str(Blue.underline(), "1000");
|
|
assert_eq!(expected, user.render(&TestColours, &MockUsers::with_current_uid(0)));
|
|
}
|
|
|
|
#[test]
|
|
fn overflow() {
|
|
let user = f::User(2_147_483_648);
|
|
let expected = TextCell::paint_str(Blue.underline(), "2147483648");
|
|
assert_eq!(expected, user.render(&TestColours, &MockUsers::with_current_uid(0)));
|
|
}
|
|
}
|