From d93e168b4d1ba9f12fd49ef620b79438adae7f9a Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Wed, 5 Jul 2017 21:01:01 +0100 Subject: [PATCH] =?UTF-8?q?Move=20Environment=20to=20a=20table=E2=80=99s?= =?UTF-8?q?=20Options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- src/options/mod.rs | 4 ++-- src/options/view.rs | 9 +++++---- src/output/details.rs | 10 ++++------ src/output/grid_details.rs | 23 +++++++++-------------- src/output/table.rs | 20 +++++++++++++++----- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/options/mod.rs b/src/options/mod.rs index a881476..4cde0c8 100644 --- a/src/options/mod.rs +++ b/src/options/mod.rs @@ -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, } } diff --git a/src/options/view.rs b/src/options/view.rs index f0c7a07..48ea834 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -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 { Ok(TableOptions { + env: Environment::load_all(), size_format: SizeFormat::deduce(matches)?, time_types: TimeTypes::deduce(matches)?, inode: matches.opt_present("inode"), diff --git a/src/output/details.rs b/src/output/details.rs index 02587ff..09cf475 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -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, + pub table: Option, /// Whether to show a header line or not. pub header: bool, @@ -139,10 +139,8 @@ impl<'a> Render<'a> { pub fn render(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(); diff --git a/src/output/grid_details.rs b/src/output/grid_details.rs index bfa5d4e..788c3f2 100644 --- a/src/output/grid_details.rs +++ b/src/output/grid_details.rs @@ -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(&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::>(); - 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) { - 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) { + 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, drender: &DetailsRender) -> grid::Grid { + fn make_grid(&'a self, column_count: usize, options: &'a TableOptions, file_names: &[TextCell], rows: Vec, 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(); diff --git a/src/output/table.rs b/src/output/table.rs index 2893205..f5dce9d 100644 --- a/src/output/table.rs +++ b/src/output/table.rs @@ -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 is not that! + writeln!(f, "") + } +} + impl Options { pub fn should_scan_for_git(&self) -> bool { self.git @@ -266,7 +275,7 @@ fn determine_time_zone() -> TZResult { pub struct Table<'a> { - columns: &'a [Column], + columns: Vec, 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 {