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.
This commit is contained in:
Benjamin Sago 2017-09-03 19:50:40 +01:00
parent 4507edd734
commit 28b4b672d4
4 changed files with 56 additions and 56 deletions

View File

@ -64,7 +64,7 @@
//! if no aliases are being used! //! if no aliases are being used!
//! //!
//! Finally, this isnt just useful when options could override each other. //! Finally, this isnt 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` shouldnt complain about `--long` being given twice when //! --grid --long` shouldnt complain about `--long` being given twice when
//! its clear what the user wants. //! its clear what the user wants.
@ -75,7 +75,7 @@ use fs::dir_action::DirAction;
use fs::filter::FileFilter; use fs::filter::FileFilter;
use output::{View, Mode, details, grid_details}; use output::{View, Mode, details, grid_details};
mod colours; mod style;
mod dir_action; mod dir_action;
mod filter; mod filter;
mod view; mod view;

View File

@ -1,4 +1,5 @@
use style::Colours; use style::Colours;
use output::file_name::{FileStyle, Classify};
use options::{flags, Vars, Misfire}; use options::{flags, Vars, Misfire};
use options::parser::MatchedFlags; use options::parser::MatchedFlags;
@ -59,16 +60,30 @@ impl TerminalColours {
} }
impl Colours { pub struct Style {
pub fn deduce<V, TW>(matches: &MatchedFlags, vars: &V, widther: TW) -> Result<Colours, Misfire> 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<V, TW>(matches: &MatchedFlags, vars: &V, widther: TW) -> Result<Self, Misfire>
where TW: Fn() -> Option<usize>, V: Vars { where TW: Fn() -> Option<usize>, V: Vars {
use self::TerminalColours::*; use self::TerminalColours::*;
use info::filetype::FileExtensions;
use style::LSColors; use style::LSColors;
use options::vars; use options::vars;
use output::file_name::NoFileColours;
let classify = Classify::deduce(matches)?;
let tc = TerminalColours::deduce(matches)?; let tc = TerminalColours::deduce(matches)?;
if tc == Never || (tc == Automatic && widther().is_none()) { 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))?; 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) { if let Some(lsc) = vars.get(vars::LS_COLORS) {
let lsc = lsc.to_string_lossy(); 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) { if let Some(exa) = vars.get(vars::EXA_COLORS) {
let exa = exa.to_string_lossy(); 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<Classify, Misfire> {
let flagged = matches.has(&flags::CLASSIFY)?;
Ok(if flagged { Classify::AddFileIndicators }
else { Classify::JustFilenames })
}
}
#[cfg(test)] #[cfg(test)]
mod terminal_test { mod terminal_test {
use super::*; use super::*;
@ -174,7 +209,7 @@ mod colour_test {
($name:ident: $inputs:expr, $widther:expr; $stricts:expr => $result:expr) => { ($name:ident: $inputs:expr, $widther:expr; $stricts:expr => $result:expr) => {
#[test] #[test]
fn $name() { 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); assert_eq!(result, $result);
} }
} }
@ -183,7 +218,7 @@ mod colour_test {
($name:ident: $inputs:expr, $widther:expr; $stricts:expr => err $result:expr) => { ($name:ident: $inputs:expr, $widther:expr; $stricts:expr => err $result:expr) => {
#[test] #[test]
fn $name() { 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); assert_eq!(result.unwrap_err(), $result);
} }
} }
@ -192,7 +227,7 @@ mod colour_test {
($name:ident: $inputs:expr, $widther:expr; $stricts:expr => like $pat:pat) => { ($name:ident: $inputs:expr, $widther:expr; $stricts:expr => like $pat:pat) => {
#[test] #[test]
fn $name() { 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); println!("Testing {:?}", result);
match result { match result {
$pat => assert!(true), $pat => assert!(true),
@ -245,7 +280,7 @@ mod customs_test {
let vars = MockVars { ls: $ls, exa: $exa }; 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)); assert_eq!(result.as_ref(), Ok(&c));
} }
} }

View File

@ -1,9 +1,6 @@
use style::Colours;
use output::{View, Mode, grid, details}; use output::{View, Mode, grid, details};
use output::grid_details::{self, RowThreshold}; use output::grid_details::{self, RowThreshold};
use output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions}; use output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions};
use output::file_name::{Classify, FileStyle, NoFileColours};
use output::time::TimeFormat; use output::time::TimeFormat;
use options::{flags, Misfire, Vars}; use options::{flags, Misfire, Vars};
@ -16,9 +13,10 @@ impl View {
/// Determine which view to use and all of that views arguments. /// Determine which view to use and all of that views arguments.
pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<View, Misfire> { pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<View, Misfire> {
use options::style::Style;
let mode = Mode::deduce(matches, vars)?; let mode = Mode::deduce(matches, vars)?;
let colours = Colours::deduce(matches, vars, || *TERM_WIDTH)?; let Style { colours, style } = Style::deduce(matches, vars, || *TERM_WIDTH)?;
let style = FileStyle::deduce(matches, &colours)?;
Ok(View { mode, colours, style }) Ok(View { mode, colours, style })
} }
} }
@ -335,30 +333,6 @@ impl TimeTypes {
} }
impl FileStyle {
#[allow(trivial_casts)]
fn deduce(matches: &MatchedFlags, colours: &Colours) -> Result<FileStyle, Misfire> {
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<Classify, Misfire> {
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. // 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, // This gets used multiple times above, with no real guarantee of order,
// so its easier to just cache it the first time it runs. // so its easier to just cache it the first time it runs.

View File

@ -195,7 +195,7 @@ impl Colours {
impl Colours { impl Colours {
pub fn set_ls(&mut self, pair: &Pair) { pub fn set_ls(&mut self, pair: &Pair) -> bool {
match pair.key { match pair.key {
"di" => self.filekinds.directory = pair.to_style(), "di" => self.filekinds.directory = pair.to_style(),
"ex" => self.filekinds.executable = pair.to_style(), "ex" => self.filekinds.executable = pair.to_style(),
@ -207,23 +207,13 @@ impl Colours {
"ln" => self.filekinds.symlink = pair.to_style(), "ln" => self.filekinds.symlink = pair.to_style(),
"or" => self.broken_arrow = pair.to_style(), "or" => self.broken_arrow = pair.to_style(),
"mi" => self.broken_filename = pair.to_style(), "mi" => self.broken_filename = pair.to_style(),
_ => {/* dont 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 { 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(), "ur" => self.perms.user_read = pair.to_style(),
"uw" => self.perms.user_write = pair.to_style(), "uw" => self.perms.user_write = pair.to_style(),
"ux" => self.perms.user_execute_file = 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(), "lp" => self.symlink_path = pair.to_style(),
"cc" => self.control_char = pair.to_style(), "cc" => self.control_char = pair.to_style(),
_ => {/* still dont change anything */}, _ => return false,
} }
true
} }
} }