2015-06-28 12:21:21 +00:00
|
|
|
use std::iter::repeat;
|
|
|
|
use std::string::ToString;
|
|
|
|
|
2015-05-09 22:57:18 +00:00
|
|
|
use colours::Colours;
|
2015-02-22 12:26:52 +00:00
|
|
|
use column::{Alignment, Column, Cell};
|
2015-02-05 14:39:56 +00:00
|
|
|
use dir::Dir;
|
2015-06-08 20:33:39 +00:00
|
|
|
use feature::Attribute;
|
2015-05-12 02:33:40 +00:00
|
|
|
use file::fields as f;
|
2015-06-08 20:33:39 +00:00
|
|
|
use file::File;
|
2015-05-11 22:28:01 +00:00
|
|
|
use options::{Columns, FileFilter, RecurseOptions, SizeFormat};
|
2015-05-16 20:02:28 +00:00
|
|
|
|
2015-05-12 02:36:47 +00:00
|
|
|
use ansi_term::{ANSIString, ANSIStrings, Style};
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-06-08 20:33:39 +00:00
|
|
|
use datetime::local::{LocalDateTime, DatePiece};
|
|
|
|
use datetime::format::{DateFormat};
|
|
|
|
use datetime::zoned::{VariableOffset, TimeZone};
|
|
|
|
|
2015-02-10 16:08:10 +00:00
|
|
|
use locale;
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-05-11 22:28:01 +00:00
|
|
|
use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
|
|
|
|
|
2015-06-08 20:33:39 +00:00
|
|
|
use users::{OSUsers, Users};
|
|
|
|
use users::mock::MockUsers;
|
|
|
|
|
|
|
|
use super::filename;
|
2015-06-04 14:15:39 +00:00
|
|
|
|
2015-05-11 22:28:01 +00:00
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// With the **Details** view, the output gets formatted into columns, with
|
|
|
|
/// each `Column` object showing some piece of information about the file,
|
|
|
|
/// such as its size, or its permissions.
|
|
|
|
///
|
|
|
|
/// To do this, the results have to be written to a table, instead of
|
|
|
|
/// displaying each file immediately. Then, the width of each column can be
|
|
|
|
/// calculated based on the individual results, and the fields are padded
|
|
|
|
/// during output.
|
|
|
|
///
|
|
|
|
/// Almost all the heavy lifting is done in a Table object, which handles the
|
|
|
|
/// columns for each row.
|
2015-05-16 20:02:28 +00:00
|
|
|
#[derive(PartialEq, Debug, Copy, Clone, Default)]
|
2015-02-05 14:39:56 +00:00
|
|
|
pub struct Details {
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// A Columns object that says which columns should be included in the
|
|
|
|
/// output in the general case. Directories themselves can pick which
|
|
|
|
/// columns are *added* to this list, such as the Git column.
|
2015-02-05 14:39:56 +00:00
|
|
|
pub columns: Columns,
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// Whether to recurse through directories with a tree view, and if so,
|
|
|
|
/// which options to use. This field is only relevant here if the `tree`
|
|
|
|
/// field of the RecurseOptions is `true`.
|
2015-02-26 07:26:04 +00:00
|
|
|
pub recurse: Option<(RecurseOptions, FileFilter)>,
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// Whether to show a header line or not.
|
|
|
|
pub header: bool,
|
|
|
|
|
|
|
|
/// Whether to show each file's extended attributes.
|
2015-02-22 12:55:13 +00:00
|
|
|
pub xattr: bool,
|
2015-05-09 22:57:18 +00:00
|
|
|
|
2015-05-16 15:10:58 +00:00
|
|
|
/// The colours to use to display information in the table, including the
|
|
|
|
/// colour of the tree view symbols.
|
2015-05-09 22:57:18 +00:00
|
|
|
pub colours: Colours,
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Details {
|
|
|
|
pub fn view(&self, dir: Option<&Dir>, files: &[File]) {
|
2015-02-26 08:27:29 +00:00
|
|
|
// First, transform the Columns object into a vector of columns for
|
|
|
|
// the current directory.
|
2015-05-12 02:00:18 +00:00
|
|
|
let mut table = Table::with_options(self.colours, self.columns.for_dir(dir));
|
2015-02-26 07:18:08 +00:00
|
|
|
if self.header { table.add_header() }
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// Then add files to the table and print it out.
|
2015-02-26 07:18:08 +00:00
|
|
|
self.add_files_to_table(&mut table, files, 0);
|
2015-06-28 12:21:21 +00:00
|
|
|
for cell in table.print_table(self.xattr, self.recurse.is_some()) {
|
|
|
|
println!("{}", cell.text);
|
|
|
|
}
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds files to the table - recursively, if the `recurse` option
|
|
|
|
/// is present.
|
2015-05-16 20:02:28 +00:00
|
|
|
fn add_files_to_table<U: Users>(&self, table: &mut Table<U>, src: &[File], depth: usize) {
|
2015-02-26 07:18:08 +00:00
|
|
|
for (index, file) in src.iter().enumerate() {
|
2015-06-28 12:21:21 +00:00
|
|
|
table.add_file(file, depth, index == src.len() - 1, true);
|
2015-02-26 07:18:08 +00:00
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// There are two types of recursion that exa supports: a tree
|
|
|
|
// view, which is dealt with here, and multiple listings, which is
|
|
|
|
// dealt with in the main module. So only actually recurse if we
|
|
|
|
// are in tree mode - the other case will be dealt with elsewhere.
|
2015-02-26 07:26:04 +00:00
|
|
|
if let Some((r, filter)) = self.recurse {
|
2015-02-26 07:18:08 +00:00
|
|
|
if r.tree == false || r.is_too_deep(depth) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// Use the filter to remove unwanted files *before* expanding
|
|
|
|
// them, so we don't examine any directories that wouldn't
|
|
|
|
// have their contents listed anyway.
|
2015-02-26 07:18:08 +00:00
|
|
|
if let Some(ref dir) = file.this {
|
|
|
|
let mut files = dir.files(true);
|
2015-02-26 07:26:04 +00:00
|
|
|
filter.transform_files(&mut files);
|
2015-02-26 07:18:08 +00:00
|
|
|
self.add_files_to_table(table, &files, depth + 1);
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-06-08 20:33:39 +00:00
|
|
|
|
2015-02-26 07:18:08 +00:00
|
|
|
struct Row {
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// Vector of cells to display.
|
|
|
|
cells: Vec<Cell>,
|
|
|
|
|
|
|
|
/// This file's name, in coloured output. The name is treated separately
|
|
|
|
/// from the other cells, as it never requires padding.
|
2015-06-28 12:21:21 +00:00
|
|
|
name: Cell,
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// How many directories deep into the tree structure this is. Directories
|
|
|
|
/// on top have depth 0.
|
|
|
|
depth: usize,
|
|
|
|
|
|
|
|
/// Vector of this file's extended attributes, if that feature is active.
|
|
|
|
attrs: Vec<Attribute>,
|
|
|
|
|
|
|
|
/// Whether this is the last entry in the directory. This flag is used
|
|
|
|
/// when calculating the tree view.
|
|
|
|
last: bool,
|
|
|
|
|
|
|
|
/// Whether this file is a directory and has any children. Also used when
|
|
|
|
/// calculating the tree view.
|
2015-02-26 07:42:58 +00:00
|
|
|
children: bool,
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 20:33:39 +00:00
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// A **Table** object gets built up by the view as it lists files and
|
|
|
|
/// directories.
|
2015-05-16 20:02:28 +00:00
|
|
|
pub struct Table<U> {
|
2015-05-11 22:28:01 +00:00
|
|
|
columns: Vec<Column>,
|
|
|
|
rows: Vec<Row>,
|
|
|
|
|
2015-05-12 02:07:16 +00:00
|
|
|
time: locale::Time,
|
|
|
|
numeric: locale::Numeric,
|
2015-06-04 14:15:39 +00:00
|
|
|
tz: VariableOffset,
|
2015-05-16 20:02:28 +00:00
|
|
|
users: U,
|
2015-05-12 02:07:16 +00:00
|
|
|
colours: Colours,
|
|
|
|
current_year: i64,
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 20:02:28 +00:00
|
|
|
impl Default for Table<MockUsers> {
|
|
|
|
fn default() -> Table<MockUsers> {
|
|
|
|
Table {
|
|
|
|
columns: Columns::default().for_dir(None),
|
|
|
|
rows: Vec::new(),
|
|
|
|
time: locale::Time::english(),
|
|
|
|
numeric: locale::Numeric::english(),
|
2015-06-04 14:15:39 +00:00
|
|
|
tz: VariableOffset::localtime().unwrap(),
|
2015-05-16 20:02:28 +00:00
|
|
|
users: MockUsers::with_current_uid(0),
|
|
|
|
colours: Colours::default(),
|
|
|
|
current_year: 1234,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Table<OSUsers> {
|
2015-05-16 15:10:58 +00:00
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// Create a new, empty Table object, setting the caching fields to their
|
|
|
|
/// empty states.
|
2015-06-28 12:21:21 +00:00
|
|
|
pub fn with_options(colours: Colours, columns: Vec<Column>) -> Table<OSUsers> {
|
2015-02-26 07:18:08 +00:00
|
|
|
Table {
|
|
|
|
columns: columns,
|
2015-05-12 02:07:16 +00:00
|
|
|
rows: Vec::new(),
|
|
|
|
|
|
|
|
time: locale::Time::load_user_locale().unwrap_or_else(|_| locale::Time::english()),
|
|
|
|
numeric: locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::english()),
|
2015-06-04 14:15:39 +00:00
|
|
|
tz: VariableOffset::localtime().unwrap(),
|
2015-05-12 02:07:16 +00:00
|
|
|
users: OSUsers::empty_cache(),
|
|
|
|
colours: colours,
|
|
|
|
current_year: LocalDateTime::now().year(),
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-16 20:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<U> Table<U> where U: Users {
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// Add a dummy "header" row to the table, which contains the names of all
|
|
|
|
/// the columns, underlined. This has dummy data for the cases that aren't
|
|
|
|
/// actually used, such as the depth or list of attributes.
|
2015-06-28 12:21:21 +00:00
|
|
|
pub fn add_header(&mut self) {
|
2015-02-26 07:18:08 +00:00
|
|
|
let row = Row {
|
2015-02-26 08:27:29 +00:00
|
|
|
depth: 0,
|
2015-05-10 11:50:20 +00:00
|
|
|
cells: self.columns.iter().map(|c| Cell::paint(self.colours.header, c.header())).collect(),
|
2015-06-28 12:21:21 +00:00
|
|
|
name: Cell::paint(self.colours.header, "Name"),
|
2015-02-26 08:27:29 +00:00
|
|
|
last: false,
|
|
|
|
attrs: Vec::new(),
|
2015-02-26 07:18:08 +00:00
|
|
|
children: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rows.push(row);
|
|
|
|
}
|
|
|
|
|
2015-05-12 01:54:34 +00:00
|
|
|
/// Get the cells for the given file, and add the result to the table.
|
2015-06-28 12:21:21 +00:00
|
|
|
pub fn add_file(&mut self, file: &File, depth: usize, last: bool, links: bool) {
|
2015-05-12 01:54:34 +00:00
|
|
|
let row = Row {
|
|
|
|
depth: depth,
|
|
|
|
cells: self.cells_for_file(file),
|
2015-06-28 12:21:21 +00:00
|
|
|
name: Cell { text: filename(file, &self.colours, links), length: file.file_name_width() },
|
2015-05-12 01:54:34 +00:00
|
|
|
last: last,
|
|
|
|
attrs: file.xattrs.clone(),
|
|
|
|
children: file.this.is_some(),
|
|
|
|
};
|
|
|
|
|
2015-05-12 02:14:56 +00:00
|
|
|
self.rows.push(row);
|
2015-05-12 01:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// Use the list of columns to find which cells should be produced for
|
|
|
|
/// this file, per-column.
|
2015-06-28 12:21:21 +00:00
|
|
|
pub fn cells_for_file(&mut self, file: &File) -> Vec<Cell> {
|
2015-02-26 07:18:08 +00:00
|
|
|
self.columns.clone().iter()
|
2015-05-11 22:28:01 +00:00
|
|
|
.map(|c| self.display(file, c))
|
2015-02-26 07:18:08 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2015-05-11 22:28:01 +00:00
|
|
|
fn display(&mut self, file: &File, column: &Column) -> Cell {
|
|
|
|
match *column {
|
2015-05-12 02:02:38 +00:00
|
|
|
Column::Permissions => self.render_permissions(file.permissions()),
|
|
|
|
Column::FileSize(fmt) => self.render_size(file.size(), fmt),
|
|
|
|
Column::Timestamp(t) => self.render_time(file.timestamp(t)),
|
|
|
|
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::GitStatus => self.render_git_status(file.git_status()),
|
2015-05-11 22:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_permissions(&self, permissions: f::Permissions) -> Cell {
|
2015-05-12 01:54:34 +00:00
|
|
|
let c = self.colours.perms;
|
|
|
|
let bit = |bit, chr: &'static str, style: Style| {
|
|
|
|
if bit { style.paint(chr) } else { self.colours.punctuation.paint("-") }
|
2015-02-26 07:18:08 +00:00
|
|
|
};
|
|
|
|
|
2015-05-12 01:54:34 +00:00
|
|
|
let file_type = match permissions.file_type {
|
2015-05-12 02:33:40 +00:00
|
|
|
f::Type::File => self.colours.filetypes.normal.paint("."),
|
|
|
|
f::Type::Directory => self.colours.filetypes.directory.paint("d"),
|
|
|
|
f::Type::Pipe => self.colours.filetypes.special.paint("|"),
|
|
|
|
f::Type::Link => self.colours.filetypes.symlink.paint("l"),
|
|
|
|
f::Type::Special => self.colours.filetypes.special.paint("?"),
|
2015-05-12 01:54:34 +00:00
|
|
|
};
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
let x_colour = if let f::Type::File = permissions.file_type { c.user_execute_file }
|
2015-05-16 13:30:01 +00:00
|
|
|
else { c.user_execute_other };
|
2015-05-12 01:54:34 +00:00
|
|
|
|
2015-05-16 13:30:01 +00:00
|
|
|
let mut columns = vec![
|
2015-05-12 01:54:34 +00:00
|
|
|
file_type,
|
|
|
|
bit(permissions.user_read, "r", c.user_read),
|
|
|
|
bit(permissions.user_write, "w", c.user_write),
|
|
|
|
bit(permissions.user_execute, "x", x_colour),
|
|
|
|
bit(permissions.group_read, "r", c.group_read),
|
|
|
|
bit(permissions.group_write, "w", c.group_write),
|
|
|
|
bit(permissions.group_execute, "x", c.group_execute),
|
|
|
|
bit(permissions.other_read, "r", c.other_read),
|
|
|
|
bit(permissions.other_write, "w", c.other_write),
|
|
|
|
bit(permissions.other_execute, "x", c.other_execute),
|
2015-05-16 13:30:01 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if permissions.attribute {
|
|
|
|
columns.push(c.attribute.paint("@"));
|
|
|
|
}
|
2015-05-12 01:54:34 +00:00
|
|
|
|
|
|
|
Cell {
|
2015-05-16 13:30:01 +00:00
|
|
|
text: ANSIStrings(&columns).to_string(),
|
|
|
|
length: columns.len(),
|
2015-05-12 01:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_links(&self, links: f::Links) -> Cell {
|
2015-05-12 01:54:34 +00:00
|
|
|
let style = if links.multiple { self.colours.links.multi_link_file }
|
|
|
|
else { self.colours.links.normal };
|
|
|
|
|
2015-05-12 02:07:16 +00:00
|
|
|
Cell::paint(style, &self.numeric.format_int(links.count))
|
2015-05-12 01:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_blocks(&self, blocks: f::Blocks) -> Cell {
|
2015-05-12 01:54:34 +00:00
|
|
|
match blocks {
|
2015-05-12 02:33:40 +00:00
|
|
|
f::Blocks::Some(blocks) => Cell::paint(self.colours.blocks, &blocks.to_string()),
|
|
|
|
f::Blocks::None => Cell::paint(self.colours.punctuation, "-"),
|
2015-05-12 01:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_inode(&self, inode: f::Inode) -> Cell {
|
2015-05-12 01:54:34 +00:00
|
|
|
Cell::paint(self.colours.inode, &inode.0.to_string())
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_size(&self, size: f::Size, size_format: SizeFormat) -> Cell {
|
|
|
|
if let f::Size::Some(offset) = size {
|
2015-05-12 02:00:18 +00:00
|
|
|
let result = match size_format {
|
2015-05-12 02:14:56 +00:00
|
|
|
SizeFormat::DecimalBytes => decimal_prefix(offset as f64),
|
|
|
|
SizeFormat::BinaryBytes => binary_prefix(offset as f64),
|
|
|
|
SizeFormat::JustBytes => return Cell::paint(self.colours.size.numbers, &self.numeric.format_int(offset)),
|
2015-05-12 01:54:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
2015-05-12 02:14:56 +00:00
|
|
|
Standalone(bytes) => Cell::paint(self.colours.size.numbers, &*bytes.to_string()),
|
|
|
|
Prefixed(prefix, n) => {
|
2015-05-12 02:07:16 +00:00
|
|
|
let number = if n < 10f64 { self.numeric.format_float(n, 1) } else { self.numeric.format_int(n as isize) };
|
2015-05-12 01:54:34 +00:00
|
|
|
let symbol = prefix.symbol();
|
|
|
|
|
|
|
|
Cell {
|
|
|
|
text: ANSIStrings( &[ self.colours.size.numbers.paint(&number[..]), self.colours.size.unit.paint(symbol) ]).to_string(),
|
|
|
|
length: number.len() + symbol.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Cell::paint(self.colours.punctuation, "-")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_time(&self, timestamp: f::Time) -> Cell {
|
2015-06-04 14:15:39 +00:00
|
|
|
let date = self.tz.at(LocalDateTime::at(timestamp.0));
|
2015-05-12 01:54:34 +00:00
|
|
|
|
2015-05-12 02:07:16 +00:00
|
|
|
let format = if date.year() == self.current_year {
|
2015-05-12 01:54:34 +00:00
|
|
|
DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
|
|
|
|
};
|
|
|
|
|
2015-06-04 14:15:39 +00:00
|
|
|
Cell::paint(self.colours.date, &format.format(&date, &self.time))
|
2015-05-12 01:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_git_status(&self, git: f::Git) -> Cell {
|
2015-05-12 01:54:34 +00:00
|
|
|
Cell {
|
2015-05-12 02:36:47 +00:00
|
|
|
text: ANSIStrings(&[ self.render_git_char(git.staged),
|
|
|
|
self.render_git_char(git.unstaged) ]).to_string(),
|
2015-05-12 01:54:34 +00:00
|
|
|
length: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:36:47 +00:00
|
|
|
fn render_git_char(&self, status: f::GitStatus) -> ANSIString {
|
|
|
|
match status {
|
|
|
|
f::GitStatus::NotModified => self.colours.punctuation.paint("-"),
|
|
|
|
f::GitStatus::New => self.colours.git.new.paint("N"),
|
|
|
|
f::GitStatus::Modified => self.colours.git.modified.paint("M"),
|
|
|
|
f::GitStatus::Deleted => self.colours.git.deleted.paint("D"),
|
|
|
|
f::GitStatus::Renamed => self.colours.git.renamed.paint("R"),
|
|
|
|
f::GitStatus::TypeChange => self.colours.git.typechange.paint("T"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_user(&mut self, user: f::User) -> Cell {
|
2015-05-12 02:07:16 +00:00
|
|
|
let user_name = match self.users.get_user_by_uid(user.0) {
|
2015-05-12 02:14:56 +00:00
|
|
|
Some(user) => user.name,
|
|
|
|
None => user.0.to_string(),
|
2015-05-12 01:54:34 +00:00
|
|
|
};
|
|
|
|
|
2015-05-12 02:07:16 +00:00
|
|
|
let style = if self.users.get_current_uid() == user.0 { self.colours.users.user_you }
|
2015-05-16 15:10:58 +00:00
|
|
|
else { self.colours.users.user_someone_else };
|
2015-05-12 01:54:34 +00:00
|
|
|
Cell::paint(style, &*user_name)
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:33:40 +00:00
|
|
|
fn render_group(&mut self, group: f::Group) -> Cell {
|
2015-05-12 01:54:34 +00:00
|
|
|
let mut style = self.colours.users.group_not_yours;
|
|
|
|
|
2015-05-12 02:07:16 +00:00
|
|
|
let group_name = match self.users.get_group_by_gid(group.0) {
|
2015-05-12 01:54:34 +00:00
|
|
|
Some(group) => {
|
2015-05-12 02:07:16 +00:00
|
|
|
let current_uid = self.users.get_current_uid();
|
|
|
|
if let Some(current_user) = self.users.get_user_by_uid(current_uid) {
|
2015-05-12 01:54:34 +00:00
|
|
|
if current_user.primary_group == group.gid || group.members.contains(¤t_user.name) {
|
|
|
|
style = self.colours.users.group_yours;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.name
|
|
|
|
},
|
|
|
|
None => group.0.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Cell::paint(style, &*group_name)
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// Print the table to standard output, consuming it in the process.
|
2015-06-28 12:21:21 +00:00
|
|
|
pub fn print_table(&self, xattr: bool, show_children: bool) -> Vec<Cell> {
|
2015-02-05 14:39:56 +00:00
|
|
|
let mut stack = Vec::new();
|
2015-06-28 12:21:21 +00:00
|
|
|
let mut cells = Vec::new();
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// Work out the list of column widths by finding the longest cell for
|
|
|
|
// each column, then formatting each cell in that column to be the
|
|
|
|
// width of that one.
|
2015-03-22 19:46:45 +00:00
|
|
|
let column_widths: Vec<usize> = (0 .. self.columns.len())
|
2015-02-26 07:18:08 +00:00
|
|
|
.map(|n| self.rows.iter().map(|row| row.cells[n].length).max().unwrap_or(0))
|
|
|
|
.collect();
|
|
|
|
|
2015-06-28 12:21:21 +00:00
|
|
|
for row in self.rows.iter() {
|
|
|
|
let mut cell = Cell::empty();
|
|
|
|
|
2015-02-26 07:18:08 +00:00
|
|
|
for (n, width) in column_widths.iter().enumerate() {
|
2015-06-28 12:21:21 +00:00
|
|
|
match self.columns[n].alignment() {
|
|
|
|
Alignment::Left => { cell.append(&row.cells[n]); cell.add_spaces(width - row.cells[n].length); }
|
|
|
|
Alignment::Right => { cell.add_spaces(width - row.cells[n].length); cell.append(&row.cells[n]); }
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.add_spaces(1);
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 12:21:21 +00:00
|
|
|
let mut filename = String::new();
|
|
|
|
let mut filename_length = 0;
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// A stack tracks which tree characters should be printed. It's
|
|
|
|
// necessary to maintain information about the previously-printed
|
|
|
|
// lines, as the output will change based on whether the
|
|
|
|
// *previous* entry was the last in its directory.
|
2015-02-26 07:18:08 +00:00
|
|
|
if show_children {
|
|
|
|
stack.resize(row.depth + 1, TreePart::Edge);
|
|
|
|
stack[row.depth] = if row.last { TreePart::Corner } else { TreePart::Edge };
|
2015-02-05 14:39:56 +00:00
|
|
|
|
|
|
|
for i in 1 .. row.depth + 1 {
|
2015-06-28 12:21:21 +00:00
|
|
|
filename.push_str(&*self.colours.punctuation.paint(stack[i].ascii_art()).to_string());
|
|
|
|
filename_length += 4;
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if row.children {
|
2015-02-26 07:18:08 +00:00
|
|
|
stack[row.depth] = if row.last { TreePart::Blank } else { TreePart::Line };
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// If any tree characters have been printed, then add an extra
|
|
|
|
// space, which makes the output look much better.
|
2015-02-05 14:39:56 +00:00
|
|
|
if row.depth != 0 {
|
2015-06-28 12:21:21 +00:00
|
|
|
filename.push(' ');
|
|
|
|
filename_length += 1;
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// Print the name without worrying about padding.
|
2015-06-28 12:21:21 +00:00
|
|
|
filename.push_str(&*row.name.text);
|
|
|
|
filename_length += row.name.length;
|
2015-02-24 16:05:25 +00:00
|
|
|
|
2015-02-26 07:18:08 +00:00
|
|
|
if xattr {
|
2015-02-22 12:26:52 +00:00
|
|
|
let width = row.attrs.iter().map(|a| a.name().len()).max().unwrap_or(0);
|
|
|
|
for attr in row.attrs.iter() {
|
|
|
|
let name = attr.name();
|
2015-06-28 12:21:21 +00:00
|
|
|
let spaces: String = repeat(" ").take(width - name.len()).collect();
|
|
|
|
filename.push_str(&*format!("\n{}{} {}", name, spaces, attr.size()))
|
2015-02-22 12:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-28 12:21:21 +00:00
|
|
|
|
|
|
|
cell.append(&Cell { text: filename, length: filename_length });
|
|
|
|
cells.push(cell);
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
2015-06-28 12:21:21 +00:00
|
|
|
|
|
|
|
cells
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-06-08 20:33:39 +00:00
|
|
|
|
2015-02-26 07:18:08 +00:00
|
|
|
#[derive(PartialEq, Debug, Clone)]
|
|
|
|
enum TreePart {
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// Rightmost column, *not* the last in the directory.
|
2015-02-26 07:18:08 +00:00
|
|
|
Edge,
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// Not the rightmost column, and the directory has not finished yet.
|
|
|
|
Line,
|
|
|
|
|
|
|
|
/// Rightmost column, and the last in the directory.
|
2015-02-26 07:18:08 +00:00
|
|
|
Corner,
|
2015-02-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// Not the rightmost column, and the directory *has* finished.
|
2015-02-26 07:18:08 +00:00
|
|
|
Blank,
|
|
|
|
}
|
2015-02-24 16:05:25 +00:00
|
|
|
|
2015-02-26 07:18:08 +00:00
|
|
|
impl TreePart {
|
|
|
|
fn ascii_art(&self) -> &'static str {
|
|
|
|
match *self {
|
2015-05-12 02:14:56 +00:00
|
|
|
TreePart::Edge => "├──",
|
|
|
|
TreePart::Line => "│ ",
|
|
|
|
TreePart::Corner => "└──",
|
|
|
|
TreePart::Blank => " ",
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 20:02:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod test {
|
|
|
|
pub use super::Table;
|
|
|
|
pub use file::File;
|
|
|
|
pub use file::fields as f;
|
|
|
|
|
|
|
|
pub use column::{Cell, Column};
|
|
|
|
|
|
|
|
pub use users::{User, Group, uid_t, gid_t};
|
|
|
|
pub use users::mock::MockUsers;
|
|
|
|
|
2015-05-29 19:39:45 +00:00
|
|
|
pub use ansi_term::Style;
|
2015-05-16 20:02:28 +00:00
|
|
|
pub use ansi_term::Colour::*;
|
|
|
|
|
|
|
|
pub fn newser(uid: uid_t, name: &str, group: gid_t) -> User {
|
|
|
|
User {
|
|
|
|
uid: uid,
|
|
|
|
name: name.to_string(),
|
|
|
|
primary_group: group,
|
|
|
|
home_dir: String::new(),
|
|
|
|
shell: String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These tests create a new, default Table object, then fill in the
|
|
|
|
// expected style in a certain way. This means we can check that the
|
2015-05-29 19:39:45 +00:00
|
|
|
// right style is being used, as otherwise, it would just be plain.
|
2015-05-16 20:02:28 +00:00
|
|
|
//
|
|
|
|
// Doing things with fields is way easier than having to fake the entire
|
|
|
|
// Metadata struct, which is what I was doing before!
|
|
|
|
|
|
|
|
mod users {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn named() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.user_you = Red.bold();
|
|
|
|
|
|
|
|
let mut users = MockUsers::with_current_uid(1000);
|
|
|
|
users.add_user(newser(1000, "enoch", 100));
|
|
|
|
table.users = users;
|
|
|
|
|
|
|
|
let user = f::User(1000);
|
|
|
|
let expected = Cell::paint(Red.bold(), "enoch");
|
|
|
|
assert_eq!(expected, table.render_user(user))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unnamed() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.user_you = Cyan.bold();
|
|
|
|
|
|
|
|
let users = MockUsers::with_current_uid(1000);
|
|
|
|
table.users = users;
|
|
|
|
|
|
|
|
let user = f::User(1000);
|
|
|
|
let expected = Cell::paint(Cyan.bold(), "1000");
|
|
|
|
assert_eq!(expected, table.render_user(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn different_named() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.user_someone_else = Green.bold();
|
|
|
|
table.users.add_user(newser(1000, "enoch", 100));
|
|
|
|
|
|
|
|
let user = f::User(1000);
|
|
|
|
let expected = Cell::paint(Green.bold(), "enoch");
|
|
|
|
assert_eq!(expected, table.render_user(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn different_unnamed() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.user_someone_else = Red.normal();
|
|
|
|
|
|
|
|
let user = f::User(1000);
|
|
|
|
let expected = Cell::paint(Red.normal(), "1000");
|
|
|
|
assert_eq!(expected, table.render_user(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.user_someone_else = Blue.underline();
|
|
|
|
|
|
|
|
let user = f::User(2_147_483_648);
|
|
|
|
let expected = Cell::paint(Blue.underline(), "2147483648");
|
|
|
|
assert_eq!(expected, table.render_user(user));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod groups {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn named() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.group_not_yours = Fixed(101).normal();
|
|
|
|
|
|
|
|
let mut users = MockUsers::with_current_uid(1000);
|
|
|
|
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
|
|
|
|
table.users = users;
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
|
|
|
let expected = Cell::paint(Fixed(101).normal(), "folk");
|
|
|
|
assert_eq!(expected, table.render_group(group))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unnamed() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.group_not_yours = Fixed(87).normal();
|
|
|
|
|
|
|
|
let users = MockUsers::with_current_uid(1000);
|
|
|
|
table.users = users;
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
|
|
|
let expected = Cell::paint(Fixed(87).normal(), "100");
|
|
|
|
assert_eq!(expected, table.render_group(group));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn primary() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.group_yours = Fixed(64).normal();
|
|
|
|
|
|
|
|
let mut users = MockUsers::with_current_uid(2);
|
|
|
|
users.add_user(newser(2, "eve", 100));
|
|
|
|
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
|
|
|
|
table.users = users;
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
|
|
|
let expected = Cell::paint(Fixed(64).normal(), "folk");
|
|
|
|
assert_eq!(expected, table.render_group(group))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn secondary() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.group_yours = Fixed(31).normal();
|
|
|
|
|
|
|
|
let mut users = MockUsers::with_current_uid(2);
|
|
|
|
users.add_user(newser(2, "eve", 666));
|
|
|
|
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![ "eve".to_string() ] });
|
|
|
|
table.users = users;
|
|
|
|
|
|
|
|
let group = f::Group(100);
|
|
|
|
let expected = Cell::paint(Fixed(31).normal(), "folk");
|
|
|
|
assert_eq!(expected, table.render_group(group))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow() {
|
|
|
|
let mut table = Table::default();
|
|
|
|
table.colours.users.group_not_yours = Blue.underline();
|
|
|
|
|
|
|
|
let group = f::Group(2_147_483_648);
|
|
|
|
let expected = Cell::paint(Blue.underline(), "2147483648");
|
|
|
|
assert_eq!(expected, table.render_group(group));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|