2017-08-20 19:29:23 +00:00
|
|
|
use ansi_term::Style;
|
2017-05-20 17:39:37 +00:00
|
|
|
use users::{Users, Groups};
|
|
|
|
|
|
|
|
use fs::fields as f;
|
|
|
|
use output::cell::TextCell;
|
|
|
|
|
|
|
|
|
2017-05-20 19:29:22 +00:00
|
|
|
impl f::Group {
|
2017-08-20 19:29:23 +00:00
|
|
|
pub fn render<C: Colours, U: Users+Groups>(&self, colours: &C, users: &U) -> TextCell {
|
2017-05-20 17:39:37 +00:00
|
|
|
use users::os::unix::GroupExt;
|
|
|
|
|
2017-08-20 19:29:23 +00:00
|
|
|
let mut style = colours.not_yours();
|
2017-05-20 17:39:37 +00:00
|
|
|
|
2017-05-20 19:29:22 +00:00
|
|
|
let group = match users.get_group_by_gid(self.0) {
|
2017-05-20 17:39:37 +00:00
|
|
|
Some(g) => (*g).clone(),
|
2017-05-20 19:29:22 +00:00
|
|
|
None => return TextCell::paint(style, self.0.to_string()),
|
2017-05-20 17:39:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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()
|
2018-12-07 00:59:05 +00:00
|
|
|
|| group.members().iter().any(|u| u == current_user.name()) {
|
2017-08-20 19:29:23 +00:00
|
|
|
style = colours.yours();
|
2017-05-20 17:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:59:05 +00:00
|
|
|
TextCell::paint(style, group.name().to_string_lossy().into())
|
2017-05-20 17:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-20 19:29:23 +00:00
|
|
|
pub trait Colours {
|
2017-08-26 22:53:47 +00:00
|
|
|
fn yours(&self) -> Style;
|
|
|
|
fn not_yours(&self) -> Style;
|
2017-08-20 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-20 17:39:37 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[allow(unused_results)]
|
|
|
|
pub mod test {
|
2017-08-26 22:53:47 +00:00
|
|
|
use super::Colours;
|
2017-05-20 17:39:37 +00:00
|
|
|
use fs::fields as f;
|
|
|
|
use output::cell::TextCell;
|
|
|
|
|
|
|
|
use users::{User, Group};
|
|
|
|
use users::mock::MockUsers;
|
|
|
|
use users::os::unix::GroupExt;
|
|
|
|
use ansi_term::Colour::*;
|
2017-08-20 19:29:23 +00:00
|
|
|
use ansi_term::Style;
|
2017-08-26 22:53:47 +00:00
|
|
|
|
|
|
|
|
2017-08-20 19:29:23 +00:00
|
|
|
struct TestColours;
|
2017-08-26 22:53:47 +00:00
|
|
|
|
2017-08-20 19:29:23 +00:00
|
|
|
impl Colours for TestColours {
|
2017-08-26 22:53:47 +00:00
|
|
|
fn yours(&self) -> Style { Fixed(80).normal() }
|
|
|
|
fn not_yours(&self) -> Style { Fixed(81).normal() }
|
2017-08-20 19:29:23 +00:00
|
|
|
}
|
2017-05-20 17:39:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn named() {
|
|
|
|
let mut users = MockUsers::with_current_uid(1000);
|
|
|
|
users.add_group(Group::new(100, "folk"));
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
2017-08-20 19:29:23 +00:00
|
|
|
let expected = TextCell::paint_str(Fixed(81).normal(), "folk");
|
|
|
|
assert_eq!(expected, group.render(&TestColours, &users))
|
2017-05-20 17:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unnamed() {
|
|
|
|
let users = MockUsers::with_current_uid(1000);
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
2017-08-20 19:29:23 +00:00
|
|
|
let expected = TextCell::paint_str(Fixed(81).normal(), "100");
|
|
|
|
assert_eq!(expected, group.render(&TestColours, &users));
|
2017-05-20 17:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn primary() {
|
|
|
|
let mut users = MockUsers::with_current_uid(2);
|
|
|
|
users.add_user(User::new(2, "eve", 100));
|
|
|
|
users.add_group(Group::new(100, "folk"));
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
2017-08-20 19:29:23 +00:00
|
|
|
let expected = TextCell::paint_str(Fixed(80).normal(), "folk");
|
|
|
|
assert_eq!(expected, group.render(&TestColours, &users))
|
2017-05-20 17:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn secondary() {
|
|
|
|
let mut users = MockUsers::with_current_uid(2);
|
|
|
|
users.add_user(User::new(2, "eve", 666));
|
|
|
|
|
|
|
|
let test_group = Group::new(100, "folk").add_member("eve");
|
|
|
|
users.add_group(test_group);
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
2017-08-20 19:29:23 +00:00
|
|
|
let expected = TextCell::paint_str(Fixed(80).normal(), "folk");
|
|
|
|
assert_eq!(expected, group.render(&TestColours, &users))
|
2017-05-20 17:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow() {
|
|
|
|
let group = f::Group(2_147_483_648);
|
2017-08-20 19:29:23 +00:00
|
|
|
let expected = TextCell::paint_str(Fixed(81).normal(), "2147483648");
|
|
|
|
assert_eq!(expected, group.render(&TestColours, &MockUsers::with_current_uid(0)));
|
2017-05-20 17:39:37 +00:00
|
|
|
}
|
|
|
|
}
|