Skip to content

Commit 28b4b67

Browse files
committed
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.
1 parent 4507edd commit 28b4b67

File tree

4 files changed

+56
-56
lines changed

4 files changed

+56
-56
lines changed

src/options/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
//! if no aliases are being used!
6565
//!
6666
//! Finally, this isn’t just useful when options could override each other.
67-
//! Creating an alias `exal=exa --long --inode --header` then invoking `exal
67+
//! Creating an alias `exal="exa --long --inode --header"` then invoking `exal
6868
//! --grid --long` shouldn’t complain about `--long` being given twice when
6969
//! it’s clear what the user wants.
7070
@@ -75,7 +75,7 @@ use fs::dir_action::DirAction;
7575
use fs::filter::FileFilter;
7676
use output::{View, Mode, details, grid_details};
7777

78-
mod colours;
78+
mod style;
7979
mod dir_action;
8080
mod filter;
8181
mod view;

src/options/colours.rs renamed to src/options/style.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use style::Colours;
2+
use output::file_name::{FileStyle, Classify};
23

34
use options::{flags, Vars, Misfire};
45
use options::parser::MatchedFlags;
@@ -59,36 +60,70 @@ impl TerminalColours {
5960
}
6061

6162

62-
impl Colours {
63-
pub fn deduce<V, TW>(matches: &MatchedFlags, vars: &V, widther: TW) -> Result<Colours, Misfire>
63+
pub struct Style {
64+
pub colours: Colours,
65+
pub style: FileStyle,
66+
}
67+
68+
impl Style {
69+
70+
#[allow(trivial_casts)] // the "as Box<_>" stuff below warns about this for some reason
71+
pub fn deduce<V, TW>(matches: &MatchedFlags, vars: &V, widther: TW) -> Result<Self, Misfire>
6472
where TW: Fn() -> Option<usize>, V: Vars {
6573
use self::TerminalColours::*;
74+
use info::filetype::FileExtensions;
6675
use style::LSColors;
6776
use options::vars;
77+
use output::file_name::NoFileColours;
78+
79+
let classify = Classify::deduce(matches)?;
6880

6981
let tc = TerminalColours::deduce(matches)?;
7082
if tc == Never || (tc == Automatic && widther().is_none()) {
71-
return Ok(Colours::plain());
83+
return Ok(Style {
84+
colours: Colours::plain(),
85+
style: FileStyle { classify, exts: Box::new(NoFileColours) },
86+
});
7287
}
7388

7489
let scale = matches.has_where(|f| f.matches(&flags::COLOR_SCALE) || f.matches(&flags::COLOUR_SCALE))?;
7590
let mut colours = Colours::colourful(scale.is_some());
7691

7792
if let Some(lsc) = vars.get(vars::LS_COLORS) {
7893
let lsc = lsc.to_string_lossy();
79-
LSColors(lsc.as_ref()).each_pair(|pair| colours.set_ls(&pair));
94+
LSColors(lsc.as_ref()).each_pair(|pair| {
95+
colours.set_ls(&pair);
96+
});
8097
}
8198

8299
if let Some(exa) = vars.get(vars::EXA_COLORS) {
83100
let exa = exa.to_string_lossy();
84-
LSColors(exa.as_ref()).each_pair(|pair| colours.set_exa(&pair));
101+
LSColors(exa.as_ref()).each_pair(|pair| {
102+
colours.set_exa(&pair);
103+
});
85104
}
86105

87-
Ok(colours)
106+
let classify = Classify::deduce(matches)?;
107+
let exts = if colours.colourful { Box::new(FileExtensions) as Box<_> }
108+
else { Box::new(NoFileColours) as Box<_> };
109+
110+
let style = FileStyle { classify, exts };
111+
Ok(Style { colours, style })
88112
}
89113
}
90114

91115

116+
impl Classify {
117+
fn deduce(matches: &MatchedFlags) -> Result<Classify, Misfire> {
118+
let flagged = matches.has(&flags::CLASSIFY)?;
119+
120+
Ok(if flagged { Classify::AddFileIndicators }
121+
else { Classify::JustFilenames })
122+
}
123+
}
124+
125+
126+
92127
#[cfg(test)]
93128
mod terminal_test {
94129
use super::*;
@@ -174,7 +209,7 @@ mod colour_test {
174209
($name:ident: $inputs:expr, $widther:expr; $stricts:expr => $result:expr) => {
175210
#[test]
176211
fn $name() {
177-
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Colours::deduce(mf, &None, &$widther)) {
212+
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Style::deduce(mf, &None, &$widther).map(|s| s.colours)) {
178213
assert_eq!(result, $result);
179214
}
180215
}
@@ -183,7 +218,7 @@ mod colour_test {
183218
($name:ident: $inputs:expr, $widther:expr; $stricts:expr => err $result:expr) => {
184219
#[test]
185220
fn $name() {
186-
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Colours::deduce(mf, &None, &$widther)) {
221+
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Style::deduce(mf, &None, &$widther).map(|s| s.colours)) {
187222
assert_eq!(result.unwrap_err(), $result);
188223
}
189224
}
@@ -192,7 +227,7 @@ mod colour_test {
192227
($name:ident: $inputs:expr, $widther:expr; $stricts:expr => like $pat:pat) => {
193228
#[test]
194229
fn $name() {
195-
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Colours::deduce(mf, &None, &$widther)) {
230+
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| Style::deduce(mf, &None, &$widther).map(|s| s.colours)) {
196231
println!("Testing {:?}", result);
197232
match result {
198233
$pat => assert!(true),
@@ -245,7 +280,7 @@ mod customs_test {
245280

246281
let vars = MockVars { ls: $ls, exa: $exa };
247282

248-
for result in parse_for_test(&[], &[], Both, |mf| Colours::deduce(mf, &vars, || Some(80))) {
283+
for result in parse_for_test(&[], &[], Both, |mf| Style::deduce(mf, &vars, || Some(80)).map(|s| s.colours)) {
249284
assert_eq!(result.as_ref(), Ok(&c));
250285
}
251286
}

src/options/view.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use style::Colours;
2-
31
use output::{View, Mode, grid, details};
42
use output::grid_details::{self, RowThreshold};
53
use output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions};
6-
use output::file_name::{Classify, FileStyle, NoFileColours};
74
use output::time::TimeFormat;
85

96
use options::{flags, Misfire, Vars};
@@ -16,9 +13,10 @@ impl View {
1613

1714
/// Determine which view to use and all of that view’s arguments.
1815
pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<View, Misfire> {
16+
use options::style::Style;
17+
1918
let mode = Mode::deduce(matches, vars)?;
20-
let colours = Colours::deduce(matches, vars, || *TERM_WIDTH)?;
21-
let style = FileStyle::deduce(matches, &colours)?;
19+
let Style { colours, style } = Style::deduce(matches, vars, || *TERM_WIDTH)?;
2220
Ok(View { mode, colours, style })
2321
}
2422
}
@@ -335,30 +333,6 @@ impl TimeTypes {
335333
}
336334

337335

338-
impl FileStyle {
339-
340-
#[allow(trivial_casts)]
341-
fn deduce(matches: &MatchedFlags, colours: &Colours) -> Result<FileStyle, Misfire> {
342-
use info::filetype::FileExtensions;
343-
344-
let classify = Classify::deduce(matches)?;
345-
let exts = if colours.colourful { Box::new(FileExtensions) as Box<_> }
346-
else { Box::new(NoFileColours) as Box<_> };
347-
348-
Ok(FileStyle { classify, exts })
349-
}
350-
}
351-
352-
impl Classify {
353-
fn deduce(matches: &MatchedFlags) -> Result<Classify, Misfire> {
354-
let flagged = matches.has(&flags::CLASSIFY)?;
355-
356-
Ok(if flagged { Classify::AddFileIndicators }
357-
else { Classify::JustFilenames })
358-
}
359-
}
360-
361-
362336
// Gets, then caches, the width of the terminal that exa is running in.
363337
// This gets used multiple times above, with no real guarantee of order,
364338
// so it’s easier to just cache it the first time it runs.

src/style/colours.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl Colours {
195195

196196

197197
impl Colours {
198-
pub fn set_ls(&mut self, pair: &Pair) {
198+
pub fn set_ls(&mut self, pair: &Pair) -> bool {
199199
match pair.key {
200200
"di" => self.filekinds.directory = pair.to_style(),
201201
"ex" => self.filekinds.executable = pair.to_style(),
@@ -207,23 +207,13 @@ impl Colours {
207207
"ln" => self.filekinds.symlink = pair.to_style(),
208208
"or" => self.broken_arrow = pair.to_style(),
209209
"mi" => self.broken_filename = pair.to_style(),
210-
_ => {/* don’t change anything */},
210+
_ => return false,
211211
}
212+
true
212213
}
213214

214-
pub fn set_exa(&mut self, pair: &Pair) {
215+
pub fn set_exa(&mut self, pair: &Pair) -> bool {
215216
match pair.key {
216-
"di" => self.filekinds.directory = pair.to_style(),
217-
"ex" => self.filekinds.executable = pair.to_style(),
218-
"fi" => self.filekinds.normal = pair.to_style(),
219-
"pi" => self.filekinds.pipe = pair.to_style(),
220-
"so" => self.filekinds.socket = pair.to_style(),
221-
"bd" => self.filekinds.block_device = pair.to_style(),
222-
"cd" => self.filekinds.char_device = pair.to_style(),
223-
"ln" => self.filekinds.symlink = pair.to_style(),
224-
"or" => self.broken_arrow = pair.to_style(),
225-
"mi" => self.broken_filename = pair.to_style(),
226-
227217
"ur" => self.perms.user_read = pair.to_style(),
228218
"uw" => self.perms.user_write = pair.to_style(),
229219
"ux" => self.perms.user_execute_file = pair.to_style(),
@@ -265,8 +255,9 @@ impl Colours {
265255
"lp" => self.symlink_path = pair.to_style(),
266256
"cc" => self.control_char = pair.to_style(),
267257

268-
_ => {/* still don’t change anything */},
258+
_ => return false,
269259
}
260+
true
270261
}
271262
}
272263

0 commit comments

Comments
 (0)