Flip the rendering functions around

A field can now render itself using colours and a users reference, rather than a Table doing the rendering. This way, only the relevant fields in the Environment can be made available to the render function, and the test code loses some lines.
This commit is contained in:
Benjamin Sago 2017-05-20 20:29:22 +01:00
parent f2169faf94
commit 753fbc294a
3 changed files with 30 additions and 79 deletions

View File

@ -177,7 +177,7 @@ pub struct Environment<U> { // where U: Users+Groups
} }
impl<U> Environment<U> { impl<U> Environment<U> {
pub fn users(&self) -> MutexGuard<U> { pub fn lock_users(&self) -> MutexGuard<U> {
self.users.lock().unwrap() self.users.lock().unwrap()
} }
} }
@ -506,8 +506,8 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
Column::HardLinks => self.render_links(file.links()), Column::HardLinks => self.render_links(file.links()),
Column::Inode => self.render_inode(file.inode()), Column::Inode => self.render_inode(file.inode()),
Column::Blocks => self.render_blocks(file.blocks()), Column::Blocks => self.render_blocks(file.blocks()),
Column::User => self.render_user(file.user()), Column::User => file.user().render(&self.opts.colours, &*self.env.lock_users()),
Column::Group => self.render_group(file.group()), Column::Group => file.group().render(&self.opts.colours, &*self.env.lock_users()),
Column::GitStatus => self.render_git_status(file.git_status()), Column::GitStatus => self.render_git_status(file.git_status()),
} }
} }
@ -745,10 +745,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
#[cfg(test)] #[cfg(test)]
pub mod test { pub mod test {
use super::{Table, Environment, Details}; use super::Environment;
use std::sync::Mutex;
use output::column::Column;
use users::mock::MockUsers; use users::mock::MockUsers;
use datetime::fmt::DateFormat; use datetime::fmt::DateFormat;
@ -770,15 +767,4 @@ pub mod test {
} }
} }
} }
pub fn new_table<'a>(columns: &'a [Column], details: &'a Details, users: MockUsers) -> Table<'a, MockUsers> {
use std::sync::Arc;
Table {
columns: columns,
opts: details,
env: Arc::new(Environment { users: Mutex::new(users), ..Environment::default() }),
rows: Vec::new(),
}
}
} }

View File

@ -1,28 +1,26 @@
use users::{Users, Groups}; use users::{Users, Groups};
use fs::fields as f; use fs::fields as f;
use output::colours::Colours;
use output::cell::TextCell; use output::cell::TextCell;
use output::details::Table;
impl<'a, U: Users+Groups+'a> Table<'a, U> { impl f::Group {
pub fn render<U: Users+Groups>(&self, colours: &Colours, users: &U) -> TextCell {
pub fn render_group(&self, group: f::Group) -> TextCell {
use users::os::unix::GroupExt; use users::os::unix::GroupExt;
let mut style = self.opts.colours.users.group_not_yours; let mut style = colours.users.group_not_yours;
let users = self.env.users(); let group = match users.get_group_by_gid(self.0) {
let group = match users.get_group_by_gid(group.0) {
Some(g) => (*g).clone(), Some(g) => (*g).clone(),
None => return TextCell::paint(style, group.0.to_string()), None => return TextCell::paint(style, self.0.to_string()),
}; };
let current_uid = users.get_current_uid(); let current_uid = users.get_current_uid();
if let Some(current_user) = users.get_user_by_uid(current_uid) { if let Some(current_user) = users.get_user_by_uid(current_uid) {
if current_user.primary_group_id() == group.gid() if current_user.primary_group_id() == group.gid()
|| group.members().contains(&current_user.name().to_owned()) { || group.members().contains(&current_user.name().to_owned()) {
style = self.opts.colours.users.group_yours; style = colours.users.group_yours;
} }
} }
@ -35,51 +33,43 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
#[allow(unused_results)] #[allow(unused_results)]
pub mod test { pub mod test {
use output::details::Details; use output::details::Details;
use output::details::test::new_table;
use fs::fields as f; use fs::fields as f;
use output::column::Columns;
use output::cell::TextCell; use output::cell::TextCell;
use users::{User, Group}; use users::{User, Group};
use users::mock::MockUsers; use users::mock::MockUsers;
use users::os::unix::GroupExt; use users::os::unix::GroupExt;
use ansi_term::Style;
use ansi_term::Colour::*; use ansi_term::Colour::*;
#[test] #[test]
fn named() { fn named() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.group_not_yours = Fixed(101).normal(); details.colours.users.group_not_yours = Fixed(101).normal();
let mut users = MockUsers::with_current_uid(1000); let mut users = MockUsers::with_current_uid(1000);
users.add_group(Group::new(100, "folk")); users.add_group(Group::new(100, "folk"));
let table = new_table(&columns, &details, users);
let group = f::Group(100); let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(101).normal(), "folk"); let expected = TextCell::paint_str(Fixed(101).normal(), "folk");
assert_eq!(expected, table.render_group(group)) assert_eq!(expected, group.render(&details.colours, &users))
} }
#[test] #[test]
fn unnamed() { fn unnamed() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.group_not_yours = Fixed(87).normal(); details.colours.users.group_not_yours = Fixed(87).normal();
let users = MockUsers::with_current_uid(1000); let users = MockUsers::with_current_uid(1000);
let table = new_table(&columns, &details, users);
let group = f::Group(100); let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(87).normal(), "100"); let expected = TextCell::paint_str(Fixed(87).normal(), "100");
assert_eq!(expected, table.render_group(group)); assert_eq!(expected, group.render(&details.colours, &users));
} }
#[test] #[test]
fn primary() { fn primary() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.group_yours = Fixed(64).normal(); details.colours.users.group_yours = Fixed(64).normal();
@ -87,16 +77,13 @@ pub mod test {
users.add_user(User::new(2, "eve", 100)); users.add_user(User::new(2, "eve", 100));
users.add_group(Group::new(100, "folk")); users.add_group(Group::new(100, "folk"));
let table = new_table(&columns, &details, users);
let group = f::Group(100); let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(64).normal(), "folk"); let expected = TextCell::paint_str(Fixed(64).normal(), "folk");
assert_eq!(expected, table.render_group(group)) assert_eq!(expected, group.render(&details.colours, &users))
} }
#[test] #[test]
fn secondary() { fn secondary() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.group_yours = Fixed(31).normal(); details.colours.users.group_yours = Fixed(31).normal();
@ -106,23 +93,18 @@ pub mod test {
let test_group = Group::new(100, "folk").add_member("eve"); let test_group = Group::new(100, "folk").add_member("eve");
users.add_group(test_group); users.add_group(test_group);
let table = new_table(&columns, &details, users);
let group = f::Group(100); let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(31).normal(), "folk"); let expected = TextCell::paint_str(Fixed(31).normal(), "folk");
assert_eq!(expected, table.render_group(group)) assert_eq!(expected, group.render(&details.colours, &users))
} }
#[test] #[test]
fn overflow() { fn overflow() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.group_not_yours = Blue.underline(); details.colours.users.group_not_yours = Blue.underline();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let group = f::Group(2_147_483_648); let group = f::Group(2_147_483_648);
let expected = TextCell::paint_str(Blue.underline(), "2147483648"); let expected = TextCell::paint_str(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_group(group)); assert_eq!(expected, group.render(&details.colours, &MockUsers::with_current_uid(0)));
} }
} }

View File

@ -1,21 +1,19 @@
use users::Users; use users::Users;
use fs::fields as f; use fs::fields as f;
use output::colours::Colours;
use output::cell::TextCell; use output::cell::TextCell;
use output::details::Table;
impl<'a, U: Users+'a> Table<'a, U> { impl f::User {
pub fn render_user(&self, user: f::User) -> TextCell { pub fn render(&self, colours: &Colours, users: &Users) -> TextCell {
let users = self.env.users(); let user_name = match users.get_user_by_uid(self.0) {
let user_name = match users.get_user_by_uid(user.0) {
Some(user) => user.name().to_owned(), Some(user) => user.name().to_owned(),
None => user.0.to_string(), None => self.0.to_string(),
}; };
let style = if users.get_current_uid() == user.0 { self.opts.colours.users.user_you } let style = if users.get_current_uid() == self.0 { colours.users.user_you }
else { self.opts.colours.users.user_someone_else }; else { colours.users.user_someone_else };
TextCell::paint(style, user_name) TextCell::paint(style, user_name)
} }
} }
@ -24,10 +22,8 @@ impl<'a, U: Users+'a> Table<'a, U> {
#[allow(unused_results)] #[allow(unused_results)]
pub mod test { pub mod test {
use output::details::Details; use output::details::Details;
use output::details::test::new_table;
use fs::fields as f; use fs::fields as f;
use output::column::Columns;
use output::cell::TextCell; use output::cell::TextCell;
use users::User; use users::User;
@ -36,72 +32,59 @@ pub mod test {
#[test] #[test]
fn named() { fn named() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.user_you = Red.bold(); details.colours.users.user_you = Red.bold();
let mut users = MockUsers::with_current_uid(1000); let mut users = MockUsers::with_current_uid(1000);
users.add_user(User::new(1000, "enoch", 100)); users.add_user(User::new(1000, "enoch", 100));
let table = new_table(&columns, &details, users);
let user = f::User(1000); let user = f::User(1000);
let expected = TextCell::paint_str(Red.bold(), "enoch"); let expected = TextCell::paint_str(Red.bold(), "enoch");
assert_eq!(expected, table.render_user(user)) assert_eq!(expected, user.render(&details.colours, &users))
} }
#[test] #[test]
fn unnamed() { fn unnamed() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.user_you = Cyan.bold(); details.colours.users.user_you = Cyan.bold();
let users = MockUsers::with_current_uid(1000); let users = MockUsers::with_current_uid(1000);
let table = new_table(&columns, &details, users);
let user = f::User(1000); let user = f::User(1000);
let expected = TextCell::paint_str(Cyan.bold(), "1000"); let expected = TextCell::paint_str(Cyan.bold(), "1000");
assert_eq!(expected, table.render_user(user)); assert_eq!(expected, user.render(&details.colours, &users));
} }
#[test] #[test]
fn different_named() { fn different_named() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.user_someone_else = Green.bold(); details.colours.users.user_someone_else = Green.bold();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0)); let mut users = MockUsers::with_current_uid(0);
table.env.users().add_user(User::new(1000, "enoch", 100)); users.add_user(User::new(1000, "enoch", 100));
let user = f::User(1000); let user = f::User(1000);
let expected = TextCell::paint_str(Green.bold(), "enoch"); let expected = TextCell::paint_str(Green.bold(), "enoch");
assert_eq!(expected, table.render_user(user)); assert_eq!(expected, user.render(&details.colours, &users));
} }
#[test] #[test]
fn different_unnamed() { fn different_unnamed() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.user_someone_else = Red.normal(); details.colours.users.user_someone_else = Red.normal();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let user = f::User(1000); let user = f::User(1000);
let expected = TextCell::paint_str(Red.normal(), "1000"); let expected = TextCell::paint_str(Red.normal(), "1000");
assert_eq!(expected, table.render_user(user)); assert_eq!(expected, user.render(&details.colours, &MockUsers::with_current_uid(0)));
} }
#[test] #[test]
fn overflow() { fn overflow() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.user_someone_else = Blue.underline(); details.colours.users.user_someone_else = Blue.underline();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let user = f::User(2_147_483_648); let user = f::User(2_147_483_648);
let expected = TextCell::paint_str(Blue.underline(), "2147483648"); let expected = TextCell::paint_str(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_user(user)); assert_eq!(expected, user.render(&details.colours, &MockUsers::with_current_uid(0)));
} }
} }