2017-07-02 00:02:17 +00:00
|
|
|
use std::cmp::max;
|
2017-07-03 16:40:05 +00:00
|
|
|
use std::ops::Deref;
|
2017-07-02 00:02:17 +00:00
|
|
|
use std::sync::{Mutex, MutexGuard};
|
|
|
|
|
|
|
|
use datetime::TimeZone;
|
|
|
|
use zoneinfo_compiled::{CompiledData, Result as TZResult};
|
|
|
|
|
|
|
|
use locale;
|
|
|
|
|
|
|
|
use users::UsersCache;
|
|
|
|
|
|
|
|
use output::cell::TextCell;
|
|
|
|
use output::colours::Colours;
|
|
|
|
use output::column::{Alignment, Column};
|
2017-07-03 07:45:14 +00:00
|
|
|
use output::time::TimeFormat;
|
2017-07-02 00:02:17 +00:00
|
|
|
|
|
|
|
use fs::{File, fields as f};
|
|
|
|
|
|
|
|
|
|
|
|
/// The **environment** struct contains any data that could change between
|
|
|
|
/// running instances of exa, depending on the user's computer's configuration.
|
|
|
|
///
|
|
|
|
/// Any environment field should be able to be mocked up for test runs.
|
|
|
|
pub struct Environment {
|
|
|
|
|
|
|
|
/// Localisation rules for formatting numbers.
|
|
|
|
numeric: locale::Numeric,
|
|
|
|
|
2017-07-03 07:45:14 +00:00
|
|
|
/// Rules for formatting timestamps.
|
|
|
|
time_format: TimeFormat,
|
2017-07-02 00:02:17 +00:00
|
|
|
|
|
|
|
/// The computer's current time zone. This gets used to determine how to
|
|
|
|
/// offset files' timestamps.
|
|
|
|
tz: Option<TimeZone>,
|
|
|
|
|
|
|
|
/// Mapping cache of user IDs to usernames.
|
|
|
|
users: Mutex<UsersCache>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Environment {
|
|
|
|
pub fn lock_users(&self) -> MutexGuard<UsersCache> {
|
|
|
|
self.users.lock().unwrap()
|
|
|
|
}
|
|
|
|
|
2017-07-05 07:21:24 +00:00
|
|
|
pub fn load_all() -> Self {
|
2017-07-03 07:45:14 +00:00
|
|
|
let tz = match determine_time_zone() {
|
|
|
|
Ok(t) => Some(t),
|
|
|
|
Err(ref e) => {
|
|
|
|
println!("Unable to determine time zone: {}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2017-07-02 00:02:17 +00:00
|
|
|
|
2017-07-03 07:45:14 +00:00
|
|
|
let time_format = TimeFormat::deduce();
|
2017-07-02 00:02:17 +00:00
|
|
|
|
|
|
|
let numeric = locale::Numeric::load_user_locale()
|
|
|
|
.unwrap_or_else(|_| locale::Numeric::english());
|
|
|
|
|
2017-07-03 07:45:14 +00:00
|
|
|
let users = Mutex::new(UsersCache::new());
|
2017-07-02 00:02:17 +00:00
|
|
|
|
2017-07-03 07:45:14 +00:00
|
|
|
Environment { tz, time_format, numeric, users }
|
2017-07-02 00:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn determine_time_zone() -> TZResult<TimeZone> {
|
|
|
|
TimeZone::from_file("/etc/localtime")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Table<'a> {
|
|
|
|
columns: &'a [Column],
|
|
|
|
colours: &'a Colours,
|
|
|
|
env: &'a Environment,
|
2017-07-03 16:40:05 +00:00
|
|
|
widths: TableWidths,
|
2017-07-02 00:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Row {
|
|
|
|
cells: Vec<TextCell>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'f> Table<'a> {
|
|
|
|
pub fn new(columns: &'a [Column], colours: &'a Colours, env: &'a Environment) -> Table<'a> {
|
2017-07-03 16:40:05 +00:00
|
|
|
let widths = TableWidths::zero(columns.len());
|
2017-07-02 00:02:17 +00:00
|
|
|
Table { columns, colours, env, widths }
|
|
|
|
}
|
|
|
|
|
2017-07-03 19:21:33 +00:00
|
|
|
pub fn widths(&self) -> &TableWidths {
|
|
|
|
&self.widths
|
2017-07-02 00:02:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 16:40:05 +00:00
|
|
|
pub fn header_row(&self) -> Row {
|
|
|
|
let cells = self.columns.iter()
|
|
|
|
.map(|c| TextCell::paint_str(self.colours.header, c.header()))
|
|
|
|
.collect();
|
2017-07-02 00:02:17 +00:00
|
|
|
|
|
|
|
Row { cells }
|
|
|
|
}
|
|
|
|
|
Only get an Env if one’s being used, also mutexes
This commit ties a table’s Environment to the fact that it contains columns.
Previously, the Details view would get its Environment, and then use those fields to actually display the details in the table: except for the case where we’re only displaying a tree, when it would just be ignored, instead.
This was caused by the “no columns” case using a Vec of no Columns behind the scenes, rather than disabling the table entirely; much like how a tap isn’t a zero-length swipe, the code should have been updated to reflect this. Now, the Environment is only created if it’s going to be used.
Also, fix a double-mutex-lock: the mutable Table had to be accessed under a lock, but the table contained a UsersCache, which *also* had to be accessed under a lock. This was changed so that the table is only updated *after* the threads have all been joined, so there’s no need for any lock at all. May fix #141, but not sure.
2017-07-03 16:04:37 +00:00
|
|
|
pub fn row_for_file(&self, file: &File, xattrs: bool) -> Row {
|
|
|
|
let cells = self.columns.iter()
|
|
|
|
.map(|c| self.display(file, c, xattrs))
|
|
|
|
.collect();
|
2017-07-02 00:02:17 +00:00
|
|
|
|
|
|
|
Row { cells }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_widths(&mut self, row: &Row) {
|
2017-07-03 16:40:05 +00:00
|
|
|
self.widths.add_widths(row)
|
2017-07-02 00:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn permissions_plus(&self, file: &File, xattrs: bool) -> f::PermissionsPlus {
|
|
|
|
f::PermissionsPlus {
|
|
|
|
file_type: file.type_char(),
|
|
|
|
permissions: file.permissions(),
|
|
|
|
xattrs: xattrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn display(&self, file: &File, column: &Column, xattrs: bool) -> TextCell {
|
|
|
|
use output::column::TimeType::*;
|
|
|
|
|
|
|
|
match *column {
|
2017-07-03 07:45:14 +00:00
|
|
|
Column::Permissions => self.permissions_plus(file, xattrs).render(&self.colours),
|
|
|
|
Column::FileSize(fmt) => file.size().render(&self.colours, fmt, &self.env.numeric),
|
|
|
|
Column::HardLinks => file.links().render(&self.colours, &self.env.numeric),
|
|
|
|
Column::Inode => file.inode().render(&self.colours),
|
|
|
|
Column::Blocks => file.blocks().render(&self.colours),
|
|
|
|
Column::User => file.user().render(&self.colours, &*self.env.lock_users()),
|
|
|
|
Column::Group => file.group().render(&self.colours, &*self.env.lock_users()),
|
|
|
|
Column::GitStatus => file.git_status().render(&self.colours),
|
|
|
|
|
|
|
|
Column::Timestamp(Modified) => file.modified_time().render(&self.colours, &self.env.tz, &self.env.time_format),
|
|
|
|
Column::Timestamp(Created) => file.created_time().render( &self.colours, &self.env.tz, &self.env.time_format),
|
|
|
|
Column::Timestamp(Accessed) => file.accessed_time().render(&self.colours, &self.env.tz, &self.env.time_format),
|
2017-07-02 00:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn render(&self, row: Row) -> TextCell {
|
|
|
|
let mut cell = TextCell::default();
|
|
|
|
|
|
|
|
for (n, (this_cell, width)) in row.cells.into_iter().zip(self.widths.iter()).enumerate() {
|
|
|
|
let padding = width - *this_cell.width;
|
|
|
|
|
|
|
|
match self.columns[n].alignment() {
|
|
|
|
Alignment::Left => { cell.append(this_cell); cell.add_spaces(padding); }
|
|
|
|
Alignment::Right => { cell.add_spaces(padding); cell.append(this_cell); }
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.add_spaces(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
cell
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 16:40:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct TableWidths(Vec<usize>);
|
|
|
|
|
|
|
|
impl Deref for TableWidths {
|
|
|
|
type Target = [usize];
|
|
|
|
|
|
|
|
fn deref<'a>(&'a self) -> &'a Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TableWidths {
|
|
|
|
pub fn zero(count: usize) -> TableWidths {
|
|
|
|
TableWidths(vec![ 0; count ])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_widths(&mut self, row: &Row) {
|
|
|
|
for (old_width, cell) in self.0.iter_mut().zip(row.cells.iter()) {
|
|
|
|
*old_width = max(*old_width, *cell.width);
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 19:21:33 +00:00
|
|
|
|
|
|
|
pub fn total(&self) -> usize {
|
|
|
|
self.0.len() + self.0.iter().sum::<usize>()
|
|
|
|
}
|
2017-07-03 16:40:05 +00:00
|
|
|
}
|