Extract table columns into a struct

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.
This commit is contained in:
Benjamin Sago 2017-08-09 22:25:16 +01:00
parent e98c765078
commit 6755ee6ae9
3 changed files with 34 additions and 15 deletions

View File

@ -136,7 +136,7 @@ impl Options {
pub fn should_scan_for_git(&self) -> bool { pub fn should_scan_for_git(&self) -> bool {
match self.view.mode { match self.view.mode {
Mode::Details(details::Options { table: Some(ref table), .. }) | 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, _ => false,
} }
} }

View File

@ -2,7 +2,7 @@ use std::env::var_os;
use output::Colours; use output::Colours;
use output::{View, Mode, grid, details}; 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::file_name::{Classify, FileStyle};
use output::time::TimeFormat; use output::time::TimeFormat;
@ -180,17 +180,26 @@ impl TerminalWidth {
impl TableOptions { impl TableOptions {
fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> { fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
Ok(TableOptions { let env = Environment::load_all();
env: Environment::load_all(), let time_format = TimeFormat::deduce(matches)?;
time_format: TimeFormat::deduce(matches)?, let size_format = SizeFormat::deduce(matches)?;
size_format: SizeFormat::deduce(matches)?, let extra_columns = Columns::deduce(matches)?;
time_types: TimeTypes::deduce(matches)?, Ok(TableOptions { env, time_format, size_format, extra_columns })
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)?, impl Columns {
}) fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
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 })
} }
} }

View File

@ -23,7 +23,17 @@ pub struct Options {
pub env: Environment, pub env: Environment,
pub size_format: SizeFormat, pub size_format: SizeFormat,
pub time_format: TimeFormat, 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, pub time_types: TimeTypes,
// The rest are just on/off
pub inode: bool, pub inode: bool,
pub links: bool, pub links: bool,
pub blocks: 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 { pub fn should_scan_for_git(&self) -> bool {
self.git self.git
} }
@ -286,7 +296,7 @@ pub struct Row {
impl<'a, 'f> Table<'a> { impl<'a, 'f> Table<'a> {
pub fn new(options: &'a Options, dir: Option<&'a Dir>, colours: &'a Colours) -> 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()); let widths = TableWidths::zero(colz.len());
Table { colours, widths, Table { colours, widths,
columns: colz, columns: colz,