mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-30 07:33:53 +00:00
Separate users into users and groups modules
This commit is contained in:
parent
070fc76a8d
commit
f2169faf94
128
src/output/groups.rs
Normal file
128
src/output/groups.rs
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
use users::{Users, Groups};
|
||||||
|
|
||||||
|
use fs::fields as f;
|
||||||
|
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 {
|
||||||
|
use users::os::unix::GroupExt;
|
||||||
|
|
||||||
|
let mut style = self.opts.colours.users.group_not_yours;
|
||||||
|
|
||||||
|
let users = self.env.users();
|
||||||
|
let group = match users.get_group_by_gid(group.0) {
|
||||||
|
Some(g) => (*g).clone(),
|
||||||
|
None => return TextCell::paint(style, group.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TextCell::paint(style, group.name().to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[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))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn primary() {
|
||||||
|
let columns = Columns::default().for_dir(None);
|
||||||
|
let mut details = Details::default();
|
||||||
|
details.colours.users.group_yours = Fixed(64).normal();
|
||||||
|
|
||||||
|
let mut users = MockUsers::with_current_uid(2);
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn secondary() {
|
||||||
|
let columns = Columns::default().for_dir(None);
|
||||||
|
let mut details = Details::default();
|
||||||
|
details.colours.users.group_yours = Fixed(31).normal();
|
||||||
|
|
||||||
|
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 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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));
|
||||||
|
}
|
||||||
|
}
|
@ -17,3 +17,4 @@ mod tree;
|
|||||||
pub mod file_name;
|
pub mod file_name;
|
||||||
mod escape;
|
mod escape;
|
||||||
mod users;
|
mod users;
|
||||||
|
mod groups;
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
use users::{Users, Groups};
|
use users::Users;
|
||||||
|
|
||||||
use fs::fields as f;
|
use fs::fields as f;
|
||||||
use output::cell::TextCell;
|
use output::cell::TextCell;
|
||||||
use output::details::Table;
|
use output::details::Table;
|
||||||
|
|
||||||
|
|
||||||
impl<'a, U: Users+Groups+'a> Table<'a, U> {
|
impl<'a, U: Users+'a> Table<'a, U> {
|
||||||
|
|
||||||
pub fn render_user(&self, user: f::User) -> TextCell {
|
pub fn render_user(&self, user: f::User) -> TextCell {
|
||||||
let users = self.env.users();
|
let users = self.env.users();
|
||||||
|
|
||||||
@ -19,51 +18,21 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
|
|||||||
else { self.opts.colours.users.user_someone_else };
|
else { self.opts.colours.users.user_someone_else };
|
||||||
TextCell::paint(style, user_name)
|
TextCell::paint(style, user_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_group(&self, group: f::Group) -> TextCell {
|
|
||||||
use users::os::unix::GroupExt;
|
|
||||||
|
|
||||||
let mut style = self.opts.colours.users.group_not_yours;
|
|
||||||
|
|
||||||
let users = self.env.users();
|
|
||||||
let group = match users.get_group_by_gid(group.0) {
|
|
||||||
Some(g) => (*g).clone(),
|
|
||||||
None => return TextCell::paint(style, group.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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TextCell::paint(style, group.name().to_owned())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
#[allow(unused_results)]
|
||||||
pub mod test {
|
pub mod test {
|
||||||
pub use output::details::{Table, Environment, Details};
|
use output::details::Details;
|
||||||
pub use output::details::test::new_table;
|
use output::details::test::new_table;
|
||||||
pub use std::sync::Mutex;
|
|
||||||
|
|
||||||
pub use fs::{File, fields as f};
|
use fs::fields as f;
|
||||||
pub use output::column::{Column, Columns};
|
use output::column::Columns;
|
||||||
pub use output::cell::TextCell;
|
use output::cell::TextCell;
|
||||||
|
|
||||||
pub use users::{User, Group, uid_t, gid_t};
|
use users::User;
|
||||||
pub use users::mock::MockUsers;
|
use users::mock::MockUsers;
|
||||||
pub use users::os::unix::{UserExt, GroupExt};
|
use ansi_term::Colour::*;
|
||||||
pub use datetime::fmt::DateFormat;
|
|
||||||
pub use ansi_term::Style;
|
|
||||||
pub use ansi_term::Colour::*;
|
|
||||||
|
|
||||||
mod users {
|
|
||||||
#![allow(unused_results)]
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn named() {
|
fn named() {
|
||||||
@ -135,88 +104,4 @@ pub mod test {
|
|||||||
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, table.render_user(user));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
mod groups {
|
|
||||||
#![allow(unused_results)]
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[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))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn primary() {
|
|
||||||
let columns = Columns::default().for_dir(None);
|
|
||||||
let mut details = Details::default();
|
|
||||||
details.colours.users.group_yours = Fixed(64).normal();
|
|
||||||
|
|
||||||
let mut users = MockUsers::with_current_uid(2);
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn secondary() {
|
|
||||||
let columns = Columns::default().for_dir(None);
|
|
||||||
let mut details = Details::default();
|
|
||||||
details.colours.users.group_yours = Fixed(31).normal();
|
|
||||||
|
|
||||||
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 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))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user