From 6755ee6ae9eab7f808fcb9a0ae87ce22980cc95e Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Wed, 9 Aug 2017 22:25:16 +0100 Subject: [PATCH] Extract table columns into a struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The table Options struct is roughly half runtime configuration and half flags to select which columns to display The column fields might as well be in their own struct, and now that the ‘for_dir’ function doesn’t use SizeFormat, it can be moved to Columns. --- src/options/mod.rs | 2 +- src/options/view.rs | 33 +++++++++++++++++++++------------ src/output/table.rs | 14 ++++++++++++-- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/options/mod.rs b/src/options/mod.rs index 7b258d5..8dae7c2 100644 --- a/src/options/mod.rs +++ b/src/options/mod.rs @@ -136,7 +136,7 @@ impl Options { pub fn should_scan_for_git(&self) -> bool { match self.view.mode { Mode::Details(details::Options { table: Some(ref table), .. }) | - Mode::GridDetails(_, details::Options { table: Some(ref table), .. }) => table.should_scan_for_git(), + Mode::GridDetails(_, details::Options { table: Some(ref table), .. }) => table.extra_columns.should_scan_for_git(), _ => false, } } diff --git a/src/options/view.rs b/src/options/view.rs index b03adb0..5324d1f 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -2,7 +2,7 @@ use std::env::var_os; use output::Colours; use output::{View, Mode, grid, details}; -use output::table::{TimeTypes, Environment, SizeFormat, Options as TableOptions}; +use output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions}; use output::file_name::{Classify, FileStyle}; use output::time::TimeFormat; @@ -180,17 +180,26 @@ impl TerminalWidth { impl TableOptions { fn deduce(matches: &MatchedFlags) -> Result { - Ok(TableOptions { - env: Environment::load_all(), - time_format: TimeFormat::deduce(matches)?, - size_format: SizeFormat::deduce(matches)?, - time_types: TimeTypes::deduce(matches)?, - inode: matches.has(&flags::INODE)?, - links: matches.has(&flags::LINKS)?, - blocks: matches.has(&flags::BLOCKS)?, - group: matches.has(&flags::GROUP)?, - git: cfg!(feature="git") && matches.has(&flags::GIT)?, - }) + let env = Environment::load_all(); + let time_format = TimeFormat::deduce(matches)?; + let size_format = SizeFormat::deduce(matches)?; + let extra_columns = Columns::deduce(matches)?; + Ok(TableOptions { env, time_format, size_format, extra_columns }) + } +} + + +impl Columns { + fn deduce(matches: &MatchedFlags) -> Result { + let time_types = TimeTypes::deduce(matches)?; + let git = cfg!(feature="git") && matches.has(&flags::GIT)?; + + let blocks = matches.has(&flags::BLOCKS)?; + let group = matches.has(&flags::GROUP)?; + let inode = matches.has(&flags::INODE)?; + let links = matches.has(&flags::LINKS)?; + + Ok(Columns { time_types, git, blocks, group, inode, links }) } } diff --git a/src/output/table.rs b/src/output/table.rs index d7722d1..3e3cfc3 100644 --- a/src/output/table.rs +++ b/src/output/table.rs @@ -23,7 +23,17 @@ pub struct Options { pub env: Environment, pub size_format: SizeFormat, pub time_format: TimeFormat, + pub extra_columns: Columns, +} + +/// Extra columns to display in the table. +#[derive(PartialEq, Debug)] +pub struct Columns { + + /// At least one of these timestamps will be shown. pub time_types: TimeTypes, + + // The rest are just on/off pub inode: bool, pub links: bool, pub blocks: bool, @@ -39,7 +49,7 @@ impl fmt::Debug for Options { } } -impl Options { +impl Columns { pub fn should_scan_for_git(&self) -> bool { self.git } @@ -286,7 +296,7 @@ pub struct Row { impl<'a, 'f> Table<'a> { pub fn new(options: &'a Options, dir: Option<&'a Dir>, colours: &'a Colours) -> Table<'a> { - let colz = options.for_dir(dir); + let colz = options.extra_columns.for_dir(dir); let widths = TableWidths::zero(colz.len()); Table { colours, widths, columns: colz,