diff --git a/src/output/details.rs b/src/output/details.rs index d96b618..99b9bb4 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -177,7 +177,7 @@ pub struct Environment { // where U: Users+Groups } impl Environment { - pub fn users(&self) -> MutexGuard { + pub fn lock_users(&self) -> MutexGuard { 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::Inode => self.render_inode(file.inode()), Column::Blocks => self.render_blocks(file.blocks()), - Column::User => self.render_user(file.user()), - Column::Group => self.render_group(file.group()), + Column::User => file.user().render(&self.opts.colours, &*self.env.lock_users()), + Column::Group => file.group().render(&self.opts.colours, &*self.env.lock_users()), Column::GitStatus => self.render_git_status(file.git_status()), } } @@ -745,10 +745,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> { #[cfg(test)] pub mod test { - use super::{Table, Environment, Details}; - use std::sync::Mutex; - - use output::column::Column; + use super::Environment; use users::mock::MockUsers; 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(), - } - } } diff --git a/src/output/groups.rs b/src/output/groups.rs index b4959de..3c2ddfe 100644 --- a/src/output/groups.rs +++ b/src/output/groups.rs @@ -1,28 +1,26 @@ use users::{Users, Groups}; use fs::fields as f; +use output::colours::Colours; use output::cell::TextCell; -use output::details::Table; -impl<'a, U: Users+Groups+'a> Table<'a, U> { - - pub fn render_group(&self, group: f::Group) -> TextCell { +impl f::Group { + pub fn render(&self, colours: &Colours, users: &U) -> TextCell { 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(group.0) { + let group = match users.get_group_by_gid(self.0) { 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(); if let Some(current_user) = users.get_user_by_uid(current_uid) { if current_user.primary_group_id() == group.gid() || group.members().contains(¤t_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)] pub mod test { use output::details::Details; - use output::details::test::new_table; use fs::fields as f; - use output::column::Columns; use output::cell::TextCell; use users::{User, Group}; use users::mock::MockUsers; use users::os::unix::GroupExt; - use ansi_term::Style; use ansi_term::Colour::*; #[test] fn named() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); details.colours.users.group_not_yours = Fixed(101).normal(); let mut users = MockUsers::with_current_uid(1000); users.add_group(Group::new(100, "folk")); - let table = new_table(&columns, &details, users); let group = f::Group(100); 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] fn unnamed() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); details.colours.users.group_not_yours = Fixed(87).normal(); let users = MockUsers::with_current_uid(1000); - let table = new_table(&columns, &details, users); let group = f::Group(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] fn primary() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); 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_group(Group::new(100, "folk")); - let table = new_table(&columns, &details, users); - let group = f::Group(100); 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] fn secondary() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); 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"); users.add_group(test_group); - let table = new_table(&columns, &details, users); - let group = f::Group(100); 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] fn overflow() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); 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 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))); } } diff --git a/src/output/users.rs b/src/output/users.rs index c98d4d9..69e55b3 100644 --- a/src/output/users.rs +++ b/src/output/users.rs @@ -1,21 +1,19 @@ use users::Users; use fs::fields as f; +use output::colours::Colours; use output::cell::TextCell; -use output::details::Table; -impl<'a, U: Users+'a> Table<'a, U> { - pub fn render_user(&self, user: f::User) -> TextCell { - let users = self.env.users(); - - let user_name = match users.get_user_by_uid(user.0) { +impl f::User { + pub fn render(&self, colours: &Colours, users: &Users) -> TextCell { + let user_name = match users.get_user_by_uid(self.0) { 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 } - else { self.opts.colours.users.user_someone_else }; + let style = if users.get_current_uid() == self.0 { colours.users.user_you } + else { colours.users.user_someone_else }; TextCell::paint(style, user_name) } } @@ -24,10 +22,8 @@ impl<'a, U: Users+'a> Table<'a, U> { #[allow(unused_results)] pub mod test { use output::details::Details; - use output::details::test::new_table; use fs::fields as f; - use output::column::Columns; use output::cell::TextCell; use users::User; @@ -36,72 +32,59 @@ pub mod test { #[test] fn named() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); details.colours.users.user_you = Red.bold(); let mut users = MockUsers::with_current_uid(1000); users.add_user(User::new(1000, "enoch", 100)); - let table = new_table(&columns, &details, users); - let user = f::User(1000); let expected = TextCell::paint_str(Red.bold(), "enoch"); - assert_eq!(expected, table.render_user(user)) + assert_eq!(expected, user.render(&details.colours, &users)) } #[test] fn unnamed() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); details.colours.users.user_you = Cyan.bold(); let users = MockUsers::with_current_uid(1000); - let table = new_table(&columns, &details, users); - let user = f::User(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] fn different_named() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); details.colours.users.user_someone_else = Green.bold(); - let table = new_table(&columns, &details, MockUsers::with_current_uid(0)); - table.env.users().add_user(User::new(1000, "enoch", 100)); + 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(Green.bold(), "enoch"); - assert_eq!(expected, table.render_user(user)); + assert_eq!(expected, user.render(&details.colours, &users)); } #[test] fn different_unnamed() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); 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 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] fn overflow() { - let columns = Columns::default().for_dir(None); let mut details = Details::default(); 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 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))); } }