Move Environment to a table’s Options

This commit moves the Environment field from the Table to its Options, and properly gets rid of the name ‘columns’ from the last commit.

Having it in the Options is important, because it means it can be generated from some command-line options. Also, it reduces the number of arguments that need to be passed to Table::new; there would have been 4 with the inclusion of the Environment, but by moving some of the code into the function, we can avoid this (and any further arguments).
This commit is contained in:
Benjamin Sago 2017-07-05 21:01:01 +01:00
parent 268b7d52dc
commit d93e168b4d
5 changed files with 35 additions and 31 deletions

View File

@ -124,8 +124,8 @@ impl Options {
/// results will end up being displayed.
pub fn should_scan_for_git(&self) -> bool {
match self.view.mode {
Mode::Details(details::Options { columns: Some(ref cols), .. }) |
Mode::GridDetails(_, details::Options { columns: Some(ref cols), .. }) => cols.should_scan_for_git(),
Mode::Details(details::Options { table: Some(ref table), .. }) |
Mode::GridDetails(_, details::Options { table: Some(ref table), .. }) => table.should_scan_for_git(),
_ => false,
}
}

View File

@ -4,7 +4,7 @@ use getopts;
use output::Colours;
use output::{grid, details};
use output::table::{TimeTypes, SizeFormat, Options as TableOptions};
use output::table::{TimeTypes, Environment, SizeFormat, Options as TableOptions};
use output::file_name::Classify;
use options::Misfire;
use fs::feature::xattr;
@ -54,7 +54,7 @@ impl Mode {
}
else {
Ok(details::Options {
columns: Some(TableOptions::deduce(matches)?),
table: Some(TableOptions::deduce(matches)?),
header: matches.opt_present("header"),
xattr: xattr::ENABLED && matches.opt_present("extended"),
})
@ -94,7 +94,7 @@ impl Mode {
}
else if matches.opt_present("tree") {
let details = details::Options {
columns: None,
table: None,
header: false,
xattr: false,
};
@ -117,7 +117,7 @@ impl Mode {
if matches.opt_present("tree") {
let details = details::Options {
columns: None,
table: None,
header: false,
xattr: false,
};
@ -197,6 +197,7 @@ impl TerminalWidth {
impl TableOptions {
fn deduce(matches: &getopts::Matches) -> Result<Self, Misfire> {
Ok(TableOptions {
env: Environment::load_all(),
size_format: SizeFormat::deduce(matches)?,
time_types: TimeTypes::deduce(matches)?,
inode: matches.opt_present("inode"),

View File

@ -71,7 +71,7 @@ use output::colours::Colours;
use output::cell::TextCell;
use output::tree::{TreeTrunk, TreeParams, TreeDepth};
use output::file_name::{FileName, LinkStyle, Classify};
use output::table::{Table, Environment, Options as TableOptions, Row as TableRow};
use output::table::{Table, Options as TableOptions, Row as TableRow};
/// With the **Details** view, the output gets formatted into columns, with
@ -92,7 +92,7 @@ pub struct Options {
///
/// Directories themselves can pick which columns are *added* to this
/// list, such as the Git column.
pub columns: Option<TableOptions>,
pub table: Option<TableOptions>,
/// Whether to show a header line or not.
pub header: bool,
@ -139,10 +139,8 @@ impl<'a> Render<'a> {
pub fn render<W: Write>(self, w: &mut W) -> IOResult<()> {
let mut rows = Vec::new();
if let Some(ref columns) = self.opts.columns {
let env = Environment::load_all();
let colz = columns.for_dir(self.dir);
let mut table = Table::new(&colz, &self.colours, &env);
if let Some(ref table) = self.opts.table {
let mut table = Table::new(&table, self.dir, &self.colours);
if self.opts.header {
let header = table.header_row();

View File

@ -12,7 +12,7 @@ use output::colours::Colours;
use output::details::{Options as DetailsOptions, Row as DetailsRow, Render as DetailsRender};
use output::grid::Options as GridOptions;
use output::file_name::{FileName, LinkStyle, Classify};
use output::table::{Table, Column, Environment, Row as TableRow};
use output::table::{Table, Row as TableRow, Options as TableOptions};
use output::tree::{TreeParams, TreeDepth};
@ -41,16 +41,11 @@ impl<'a> Render<'a> {
pub fn render<W: Write>(&self, w: &mut W) -> IOResult<()> {
let columns_for_dir = match self.details.columns {
Some(ref cols) => cols.for_dir(self.dir),
None => Vec::new(),
};
let env = Environment::load_all();
let options = self.details.table.as_ref().expect("Details table options not given!");
let drender = self.clone().details();
let (first_table, _) = self.make_table(&env, &columns_for_dir, &drender);
let (first_table, _) = self.make_table(options, &drender);
let rows = self.files.iter()
.map(|file| first_table.row_for_file(file, file_has_xattrs(file)))
@ -60,10 +55,10 @@ impl<'a> Render<'a> {
.map(|file| FileName::new(file, LinkStyle::JustFilenames, self.classify, self.colours).paint().promote())
.collect::<Vec<TextCell>>();
let mut last_working_table = self.make_grid(&env, 1, &columns_for_dir, &file_names, rows.clone(), &drender);
let mut last_working_table = self.make_grid(1, options, &file_names, rows.clone(), &drender);
for column_count in 2.. {
let grid = self.make_grid(&env, column_count, &columns_for_dir, &file_names, rows.clone(), &drender);
let grid = self.make_grid(column_count, options, &file_names, rows.clone(), &drender);
let the_grid_fits = {
let d = grid.fit_into_columns(column_count);
@ -81,8 +76,8 @@ impl<'a> Render<'a> {
Ok(())
}
fn make_table<'t>(&'a self, env: &'a Environment, columns_for_dir: &'a [Column], drender: &DetailsRender) -> (Table<'a>, Vec<DetailsRow>) {
let mut table = Table::new(columns_for_dir, self.colours, env);
fn make_table<'t>(&'a self, options: &'a TableOptions, drender: &DetailsRender) -> (Table<'a>, Vec<DetailsRow>) {
let mut table = Table::new(options, self.dir, self.colours);
let mut rows = Vec::new();
if self.details.header {
@ -94,11 +89,11 @@ impl<'a> Render<'a> {
(table, rows)
}
fn make_grid(&'a self, env: &'a Environment, column_count: usize, columns_for_dir: &'a [Column], file_names: &[TextCell], rows: Vec<TableRow>, drender: &DetailsRender) -> grid::Grid {
fn make_grid(&'a self, column_count: usize, options: &'a TableOptions, file_names: &[TextCell], rows: Vec<TableRow>, drender: &DetailsRender) -> grid::Grid {
let mut tables = Vec::new();
for _ in 0 .. column_count {
tables.push(self.make_table(env.clone(), columns_for_dir, drender));
tables.push(self.make_table(options, drender));
}
let mut num_cells = rows.len();

View File

@ -1,4 +1,5 @@
use std::cmp::max;
use std::fmt;
use std::ops::Deref;
use std::sync::{Mutex, MutexGuard};
@ -18,8 +19,8 @@ use fs::{File, Dir, fields as f};
/// Options for displaying a table.
#[derive(Debug)]
pub struct Options {
pub env: Environment,
pub size_format: SizeFormat,
pub time_types: TimeTypes,
pub inode: bool,
@ -29,6 +30,14 @@ pub struct Options {
pub git: bool
}
impl fmt::Debug for Options {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
// I had to make other types derive Debug,
// and Mutex<UsersCache> is not that!
writeln!(f, "<table options>")
}
}
impl Options {
pub fn should_scan_for_git(&self) -> bool {
self.git
@ -266,7 +275,7 @@ fn determine_time_zone() -> TZResult<TimeZone> {
pub struct Table<'a> {
columns: &'a [Column],
columns: Vec<Column>,
colours: &'a Colours,
env: &'a Environment,
widths: TableWidths,
@ -278,9 +287,10 @@ pub struct Row {
}
impl<'a, 'f> Table<'a> {
pub fn new(columns: &'a [Column], colours: &'a Colours, env: &'a Environment) -> Table<'a> {
let widths = TableWidths::zero(columns.len());
Table { columns, colours, env, widths }
pub fn new(options: &'a Options, dir: Option<&'a Dir>, colours: &'a Colours) -> Table<'a> {
let colz = options.for_dir(dir);
let widths = TableWidths::zero(colz.len());
Table { columns: colz, colours, env: &options.env, widths }
}
pub fn widths(&self) -> &TableWidths {