From 28b4b672d48a0714608d50777f469055b4bedb4a Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Sun, 3 Sep 2017 19:50:40 +0100 Subject: [PATCH] Move FileStyle to the same options file as Colours They are going to be deduced together (from the same environment variable) so it makes sense to put them in the same file first. --- src/options/mod.rs | 4 +- src/options/{colours.rs => style.rs} | 55 +++++++++++++++++++++++----- src/options/view.rs | 32 ++-------------- src/style/colours.rs | 21 +++-------- 4 files changed, 56 insertions(+), 56 deletions(-) rename src/options/{colours.rs => style.rs} (87%) diff --git a/src/options/mod.rs b/src/options/mod.rs index 074416d..97366d7 100644 --- a/src/options/mod.rs +++ b/src/options/mod.rs @@ -64,7 +64,7 @@ //! if no aliases are being used! //! //! Finally, this isn’t just useful when options could override each other. -//! Creating an alias `exal=”exa --long --inode --header”` then invoking `exal +//! Creating an alias `exal="exa --long --inode --header"` then invoking `exal //! --grid --long` shouldn’t complain about `--long` being given twice when //! it’s clear what the user wants. @@ -75,7 +75,7 @@ use fs::dir_action::DirAction; use fs::filter::FileFilter; use output::{View, Mode, details, grid_details}; -mod colours; +mod style; mod dir_action; mod filter; mod view; diff --git a/src/options/colours.rs b/src/options/style.rs similarity index 87% rename from src/options/colours.rs rename to src/options/style.rs index cbea537..2aede03 100644 --- a/src/options/colours.rs +++ b/src/options/style.rs @@ -1,4 +1,5 @@ use style::Colours; +use output::file_name::{FileStyle, Classify}; use options::{flags, Vars, Misfire}; use options::parser::MatchedFlags; @@ -59,16 +60,30 @@ impl TerminalColours { } -impl Colours { - pub fn deduce(matches: &MatchedFlags, vars: &V, widther: TW) -> Result +pub struct Style { + pub colours: Colours, + pub style: FileStyle, +} + +impl Style { + + #[allow(trivial_casts)] // the "as Box<_>" stuff below warns about this for some reason + pub fn deduce(matches: &MatchedFlags, vars: &V, widther: TW) -> Result where TW: Fn() -> Option, V: Vars { use self::TerminalColours::*; + use info::filetype::FileExtensions; use style::LSColors; use options::vars; + use output::file_name::NoFileColours; + + let classify = Classify::deduce(matches)?; let tc = TerminalColours::deduce(matches)?; if tc == Never || (tc == Automatic && widther().is_none()) { - return Ok(Colours::plain()); + return Ok(Style { + colours: Colours::plain(), + style: FileStyle { classify, exts: Box::new(NoFileColours) }, + }); } let scale = matches.has_where(|f| f.matches(&flags::COLOR_SCALE) || f.matches(&flags::COLOUR_SCALE))?; @@ -76,19 +91,39 @@ impl Colours { if let Some(lsc) = vars.get(vars::LS_COLORS) { let lsc = lsc.to_string_lossy(); - LSColors(lsc.as_ref()).each_pair(|pair| colours.set_ls(&pair)); + LSColors(lsc.as_ref()).each_pair(|pair| { + colours.set_ls(&pair); + }); } if let Some(exa) = vars.get(vars::EXA_COLORS) { let exa = exa.to_string_lossy(); - LSColors(exa.as_ref()).each_pair(|pair| colours.set_exa(&pair)); + LSColors(exa.as_ref()).each_pair(|pair| { + colours.set_exa(&pair); + }); } - Ok(colours) + let classify = Classify::deduce(matches)?; + let exts = if colours.colourful { Box::new(FileExtensions) as Box<_> } + else { Box::new(NoFileColours) as Box<_> }; + + let style = FileStyle { classify, exts }; + Ok(Style { colours, style }) } } +impl Classify { + fn deduce(matches: &MatchedFlags) -> Result { + let flagged = matches.has(&flags::CLASSIFY)?; + + Ok(if flagged { Classify::AddFileIndicators } + else { Classify::JustFilenames }) + } +} + + + #[cfg(test)] mod terminal_test { use super::*; @@ -174,7 +209,7 @@ mod colour_test { ($name:ident: $inputs:expr, $widther:expr; $stricts:expr => $result:expr) => { #[test] fn $name() { - for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Colours::deduce(mf, &None, &$widther)) { + for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Style::deduce(mf, &None, &$widther).map(|s| s.colours)) { assert_eq!(result, $result); } } @@ -183,7 +218,7 @@ mod colour_test { ($name:ident: $inputs:expr, $widther:expr; $stricts:expr => err $result:expr) => { #[test] fn $name() { - for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Colours::deduce(mf, &None, &$widther)) { + for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Style::deduce(mf, &None, &$widther).map(|s| s.colours)) { assert_eq!(result.unwrap_err(), $result); } } @@ -192,7 +227,7 @@ mod colour_test { ($name:ident: $inputs:expr, $widther:expr; $stricts:expr => like $pat:pat) => { #[test] fn $name() { - for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Colours::deduce(mf, &None, &$widther)) { + for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Style::deduce(mf, &None, &$widther).map(|s| s.colours)) { println!("Testing {:?}", result); match result { $pat => assert!(true), @@ -245,7 +280,7 @@ mod customs_test { let vars = MockVars { ls: $ls, exa: $exa }; - for result in parse_for_test(&[], &[], Both, |mf| Colours::deduce(mf, &vars, || Some(80))) { + for result in parse_for_test(&[], &[], Both, |mf| Style::deduce(mf, &vars, || Some(80)).map(|s| s.colours)) { assert_eq!(result.as_ref(), Ok(&c)); } } diff --git a/src/options/view.rs b/src/options/view.rs index 6aa3a6d..8d4520d 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -1,9 +1,6 @@ -use style::Colours; - use output::{View, Mode, grid, details}; use output::grid_details::{self, RowThreshold}; use output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions}; -use output::file_name::{Classify, FileStyle, NoFileColours}; use output::time::TimeFormat; use options::{flags, Misfire, Vars}; @@ -16,9 +13,10 @@ impl View { /// Determine which view to use and all of that view’s arguments. pub fn deduce(matches: &MatchedFlags, vars: &V) -> Result { + use options::style::Style; + let mode = Mode::deduce(matches, vars)?; - let colours = Colours::deduce(matches, vars, || *TERM_WIDTH)?; - let style = FileStyle::deduce(matches, &colours)?; + let Style { colours, style } = Style::deduce(matches, vars, || *TERM_WIDTH)?; Ok(View { mode, colours, style }) } } @@ -335,30 +333,6 @@ impl TimeTypes { } -impl FileStyle { - - #[allow(trivial_casts)] - fn deduce(matches: &MatchedFlags, colours: &Colours) -> Result { - use info::filetype::FileExtensions; - - let classify = Classify::deduce(matches)?; - let exts = if colours.colourful { Box::new(FileExtensions) as Box<_> } - else { Box::new(NoFileColours) as Box<_> }; - - Ok(FileStyle { classify, exts }) - } -} - -impl Classify { - fn deduce(matches: &MatchedFlags) -> Result { - let flagged = matches.has(&flags::CLASSIFY)?; - - Ok(if flagged { Classify::AddFileIndicators } - else { Classify::JustFilenames }) - } -} - - // Gets, then caches, the width of the terminal that exa is running in. // This gets used multiple times above, with no real guarantee of order, // so it’s easier to just cache it the first time it runs. diff --git a/src/style/colours.rs b/src/style/colours.rs index 25fc15a..fefd692 100644 --- a/src/style/colours.rs +++ b/src/style/colours.rs @@ -195,7 +195,7 @@ impl Colours { impl Colours { - pub fn set_ls(&mut self, pair: &Pair) { + pub fn set_ls(&mut self, pair: &Pair) -> bool { match pair.key { "di" => self.filekinds.directory = pair.to_style(), "ex" => self.filekinds.executable = pair.to_style(), @@ -207,23 +207,13 @@ impl Colours { "ln" => self.filekinds.symlink = pair.to_style(), "or" => self.broken_arrow = pair.to_style(), "mi" => self.broken_filename = pair.to_style(), - _ => {/* don’t change anything */}, + _ => return false, } + true } - pub fn set_exa(&mut self, pair: &Pair) { + pub fn set_exa(&mut self, pair: &Pair) -> bool { match pair.key { - "di" => self.filekinds.directory = pair.to_style(), - "ex" => self.filekinds.executable = pair.to_style(), - "fi" => self.filekinds.normal = pair.to_style(), - "pi" => self.filekinds.pipe = pair.to_style(), - "so" => self.filekinds.socket = pair.to_style(), - "bd" => self.filekinds.block_device = pair.to_style(), - "cd" => self.filekinds.char_device = pair.to_style(), - "ln" => self.filekinds.symlink = pair.to_style(), - "or" => self.broken_arrow = pair.to_style(), - "mi" => self.broken_filename = pair.to_style(), - "ur" => self.perms.user_read = pair.to_style(), "uw" => self.perms.user_write = pair.to_style(), "ux" => self.perms.user_execute_file = pair.to_style(), @@ -265,8 +255,9 @@ impl Colours { "lp" => self.symlink_path = pair.to_style(), "cc" => self.control_char = pair.to_style(), - _ => {/* still don’t change anything */}, + _ => return false, } + true } }