2015-02-22 12:26:52 +00:00
|
|
|
use column::{Alignment, Column, Cell};
|
2015-02-22 12:55:13 +00:00
|
|
|
use xattr::Attribute;
|
2015-02-05 14:39:56 +00:00
|
|
|
use dir::Dir;
|
|
|
|
use file::{File, GREY};
|
2015-02-24 16:05:25 +00:00
|
|
|
use options::{Columns, FileFilter, RecurseOptions};
|
2015-02-05 14:39:56 +00:00
|
|
|
use users::OSUsers;
|
|
|
|
|
2015-02-10 16:08:10 +00:00
|
|
|
use locale;
|
2015-02-05 14:39:56 +00:00
|
|
|
use ansi_term::Style::Plain;
|
|
|
|
|
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-02-05 14:39:56 +00:00
|
|
|
#[derive(PartialEq, Debug, Copy)]
|
|
|
|
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-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-02-26 07:18:08 +00:00
|
|
|
let mut table = Table::with_columns(self.columns.for_dir(dir));
|
|
|
|
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);
|
|
|
|
table.print_table(self.xattr, self.recurse.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds files to the table - recursively, if the `recurse` option
|
|
|
|
/// is present.
|
|
|
|
fn add_files_to_table(&self, table: &mut Table, src: &[File], depth: usize) {
|
|
|
|
for (index, file) in src.iter().enumerate() {
|
2015-02-26 08:27:29 +00:00
|
|
|
table.add_file(file, depth, index == src.len() - 1);
|
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-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.
|
|
|
|
name: String,
|
|
|
|
|
|
|
|
/// 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-02-26 08:27:29 +00:00
|
|
|
/// A **Table** object gets built up by the view as it lists files and
|
|
|
|
/// directories.
|
2015-02-26 07:18:08 +00:00
|
|
|
struct Table {
|
|
|
|
columns: Vec<Column>,
|
2015-02-26 08:27:29 +00:00
|
|
|
users: OSUsers,
|
|
|
|
locale: UserLocale,
|
|
|
|
rows: Vec<Row>,
|
2015-02-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Table {
|
2015-02-26 08:27:29 +00:00
|
|
|
/// Create a new, empty Table object, setting the caching fields to their
|
|
|
|
/// empty states.
|
2015-02-26 07:18:08 +00:00
|
|
|
fn with_columns(columns: Vec<Column>) -> Table {
|
|
|
|
Table {
|
|
|
|
columns: columns,
|
|
|
|
users: OSUsers::empty_cache(),
|
|
|
|
locale: UserLocale::new(),
|
|
|
|
rows: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
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-02-26 07:18:08 +00:00
|
|
|
fn add_header(&mut self) {
|
|
|
|
let row = Row {
|
2015-02-26 08:27:29 +00:00
|
|
|
depth: 0,
|
|
|
|
cells: self.columns.iter().map(|c| Cell::paint(Plain.underline(), c.header())).collect(),
|
|
|
|
name: Plain.underline().paint("Name").to_string(),
|
|
|
|
last: false,
|
|
|
|
attrs: Vec::new(),
|
2015-02-26 07:18:08 +00:00
|
|
|
children: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rows.push(row);
|
|
|
|
}
|
|
|
|
|
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-02-26 07:18:08 +00:00
|
|
|
fn cells_for_file(&mut self, file: &File) -> Vec<Cell> {
|
|
|
|
self.columns.clone().iter()
|
|
|
|
.map(|c| file.display(c, &mut self.users, &self.locale))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// Get the cells for the given file, and add the result to the table.
|
|
|
|
fn add_file(&mut self, file: &File, depth: usize, last: bool) {
|
2015-02-26 07:18:08 +00:00
|
|
|
let row = Row {
|
2015-02-26 08:27:29 +00:00
|
|
|
depth: depth,
|
|
|
|
cells: self.cells_for_file(file),
|
|
|
|
name: file.file_name_view(),
|
|
|
|
last: last,
|
|
|
|
attrs: file.xattrs.clone(),
|
2015-02-26 07:18:08 +00:00
|
|
|
children: file.this.is_some(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rows.push(row)
|
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
/// Print the table to standard output, consuming it in the process.
|
2015-02-26 07:18:08 +00:00
|
|
|
fn print_table(self, xattr: bool, show_children: bool) {
|
2015-02-05 14:39:56 +00:00
|
|
|
let mut stack = Vec::new();
|
|
|
|
|
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-02-26 07:18:08 +00:00
|
|
|
let column_widths: Vec<usize> = range(0, self.columns.len())
|
|
|
|
.map(|n| self.rows.iter().map(|row| row.cells[n].length).max().unwrap_or(0))
|
|
|
|
.collect();
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
for row in self.rows.into_iter() {
|
2015-02-26 07:18:08 +00:00
|
|
|
for (n, width) in column_widths.iter().enumerate() {
|
|
|
|
let padding = width - row.cells[n].length;
|
|
|
|
print!("{} ", self.columns[n].alignment().pad_string(&row.cells[n].text, padding));
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
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-02-26 07:18:08 +00:00
|
|
|
print!("{}", GREY.paint(stack[i].ascii_art()));
|
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 {
|
|
|
|
print!(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 08:27:29 +00:00
|
|
|
// Print the name without worrying about padding.
|
2015-02-05 14:39:56 +00:00
|
|
|
print!("{}\n", row.name);
|
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();
|
|
|
|
println!("{}\t{}",
|
|
|
|
Alignment::Left.pad_string(name, width - name.len()),
|
|
|
|
attr.size()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
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-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 {
|
|
|
|
TreePart::Edge => "├──",
|
|
|
|
TreePart::Line => "│ ",
|
|
|
|
TreePart::Corner => "└──",
|
|
|
|
TreePart::Blank => " ",
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 18:14:56 +00:00
|
|
|
pub struct UserLocale {
|
2015-02-26 08:27:29 +00:00
|
|
|
pub time: locale::Time,
|
2015-02-10 18:14:56 +00:00
|
|
|
pub numeric: locale::Numeric,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserLocale {
|
|
|
|
pub fn new() -> UserLocale {
|
|
|
|
UserLocale {
|
2015-02-26 08:27:29 +00:00
|
|
|
time: locale::Time::load_user_locale().unwrap_or_else(|_| locale::Time::english()),
|
2015-02-12 22:33:01 +00:00
|
|
|
numeric: locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::english()),
|
2015-02-10 18:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default() -> UserLocale {
|
|
|
|
UserLocale {
|
2015-02-26 08:27:29 +00:00
|
|
|
time: locale::Time::english(),
|
2015-02-12 22:33:01 +00:00
|
|
|
numeric: locale::Numeric::english(),
|
2015-02-10 18:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|