Skip to content

Commit

Permalink
Move FileStyle to the same options file as Colours
Browse files Browse the repository at this point in the history
They are going to be deduced together (from the same environment variable) so it makes sense to put them in the same file first.
  • Loading branch information
ogham committed Sep 3, 2017
1 parent 4507edd commit 28b4b67
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
55 changes: 45 additions & 10 deletions src/options/colours.rs → src/options/style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use style::Colours;
use output::file_name::{FileStyle, Classify};

use options::{flags, Vars, Misfire};
use options::parser::MatchedFlags;
Expand Down Expand Up @@ -59,36 +60,70 @@ impl TerminalColours {
}


impl Colours {
pub fn deduce<V, TW>(matches: &MatchedFlags, vars: &V, widther: TW) -> Result<Colours, Misfire>
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<V, TW>(matches: &MatchedFlags, vars: &V, widther: TW) -> Result<Self, Misfire>
where TW: Fn() -> Option<usize>, 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))?;
let mut colours = Colours::colourful(scale.is_some());

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<Classify, Misfire> {
let flagged = matches.has(&flags::CLASSIFY)?;

Ok(if flagged { Classify::AddFileIndicators }
else { Classify::JustFilenames })
}
}



#[cfg(test)]
mod terminal_test {
use super::*;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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),
Expand Down Expand Up @@ -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));
}
}
Expand Down
32 changes: 3 additions & 29 deletions src/options/view.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,9 +13,10 @@ impl View {

/// Determine which view to use and all of that view’s arguments.
pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<View, Misfire> {
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 })
}
}
Expand Down Expand Up @@ -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.
// 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.
Expand Down
21 changes: 6 additions & 15 deletions src/style/colours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 28b4b67

Please sign in to comment.