Move common icons option to file style struct

All four of the view mode command-line argument parsers tested for the --icons option. Because it was common, the behaviour has been moved to the struct that handles file styles, meaning it can be parsed in one place.

This is a better place for it, as the icons are to do with the file name, not the view. It also means that the lines view has no options left for it, which is fitting.
This commit is contained in:
Benjamin Sago 2020-10-23 23:57:10 +01:00
parent 800c73ff24
commit a1869f208e
9 changed files with 26 additions and 59 deletions

View File

@ -268,14 +268,9 @@ impl<'args> Exa<'args> {
r.render(&mut self.writer)
}
(Mode::Grid(ref opts), None) => {
let opts = &opts.to_lines_options();
let r = lines::Render { files, theme, file_style, opts };
r.render(&mut self.writer)
}
(Mode::Lines(ref opts), _) => {
let r = lines::Render { files, theme, file_style, opts };
(Mode::Grid(_), None) |
(Mode::Lines, _) => {
let r = lines::Render { files, theme, file_style };
r.render(&mut self.writer)
}

View File

@ -6,8 +6,10 @@ use crate::output::file_name::{Options, Classify};
impl Options {
pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
Classify::deduce(matches)
.map(|classify| Self { classify })
let classify = Classify::deduce(matches)?;
let icons = matches.has(&flags::ICONS)?;
Ok(Self { classify, icons })
}
}

View File

@ -1,7 +1,7 @@
use crate::fs::feature::xattr;
use crate::options::{flags, OptionsError, Vars};
use crate::options::parser::MatchedFlags;
use crate::output::{View, Mode, TerminalWidth, grid, details, lines};
use crate::output::{View, Mode, TerminalWidth, grid, details};
use crate::output::grid_details::{self, RowThreshold};
use crate::output::file_name::Options as FileStyle;
use crate::output::table::{TimeTypes, SizeFormat, Columns, Options as TableOptions};
@ -73,8 +73,7 @@ impl Mode {
if flag.matches(&flags::ONE_LINE) {
let _ = matches.has(&flags::ONE_LINE)?;
let lines = lines::Options::deduce(matches)?;
return Ok(Self::Lines(lines));
return Ok(Self::Lines);
}
let grid = grid::Options::deduce(matches)?;
@ -105,19 +104,10 @@ impl Mode {
}
impl lines::Options {
fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
let lines = lines::Options { icons: matches.has(&flags::ICONS)? };
Ok(lines)
}
}
impl grid::Options {
fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
let grid = grid::Options {
across: matches.has(&flags::ACROSS)?,
icons: matches.has(&flags::ICONS)?,
};
Ok(grid)
@ -131,7 +121,6 @@ impl details::Options {
table: None,
header: false,
xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
icons: matches.has(&flags::ICONS)?,
};
Ok(details)
@ -151,7 +140,6 @@ impl details::Options {
table: Some(TableOptions::deduce(matches, vars)?),
header: matches.has(&flags::HEADER)?,
xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
icons: matches.has(&flags::ICONS)?,
})
}
}
@ -354,7 +342,7 @@ mod test {
static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES, &flags::TIME_STYLE,
&flags::TIME, &flags::MODIFIED, &flags::CHANGED,
&flags::CREATED, &flags::ACCESSED, &flags::ICONS,
&flags::CREATED, &flags::ACCESSED,
&flags::HEADER, &flags::GROUP, &flags::INODE, &flags::GIT,
&flags::LINKS, &flags::BLOCKS, &flags::LONG, &flags::LEVEL,
&flags::GRID, &flags::ACROSS, &flags::ONE_LINE, &flags::TREE ];
@ -532,7 +520,6 @@ mod test {
use super::*;
use crate::output::grid::Options as GridOptions;
use crate::output::lines::Options as LineOptions;
// Default
@ -543,12 +530,10 @@ mod test {
test!(grid: Mode <- ["--grid"], None; Both => like Ok(Mode::Grid(GridOptions { across: false, .. })));
test!(across: Mode <- ["--across"], None; Both => like Ok(Mode::Grid(GridOptions { across: true, .. })));
test!(gracross: Mode <- ["-xG"], None; Both => like Ok(Mode::Grid(GridOptions { across: true, .. })));
test!(icons: Mode <- ["--icons"], None; Both => like Ok(Mode::Grid(GridOptions { icons: true, .. })));
// Lines views
test!(lines: Mode <- ["--oneline"], None; Both => like Ok(Mode::Lines(LineOptions { .. })));
test!(prima: Mode <- ["-1"], None; Both => like Ok(Mode::Lines(LineOptions { .. })));
test!(line_icon: Mode <- ["-1", "--icons"], None; Both => like Ok(Mode::Lines(LineOptions { icons: true })));
test!(lines: Mode <- ["--oneline"], None; Both => like Ok(Mode::Lines));
test!(prima: Mode <- ["-1"], None; Both => like Ok(Mode::Lines));
// Details views
test!(long: Mode <- ["--long"], None; Both => like Ok(Mode::Details(_)));
@ -585,7 +570,7 @@ mod test {
test!(just_git_2: Mode <- ["--git"], None; Complain => err OptionsError::Useless(&flags::GIT, false, &flags::LONG));
// Contradictions and combinations
test!(lgo: Mode <- ["--long", "--grid", "--oneline"], None; Both => like Ok(Mode::Lines(_)));
test!(lgo: Mode <- ["--long", "--grid", "--oneline"], None; Both => like Ok(Mode::Lines));
test!(lgt: Mode <- ["--long", "--grid", "--tree"], None; Both => like Ok(Mode::Details(_)));
test!(tgl: Mode <- ["--tree", "--grid", "--long"], None; Both => like Ok(Mode::GridDetails(_)));
test!(tlg: Mode <- ["--tree", "--long", "--grid"], None; Both => like Ok(Mode::GridDetails(_)));

View File

@ -106,9 +106,6 @@ pub struct Options {
/// Whether to show each files extended attributes.
pub xattr: bool,
/// Whether icons mode is enabled.
pub icons: bool,
}
@ -269,7 +266,7 @@ impl<'a> Render<'a> {
}
};
let icon = if self.opts.icons { Some(painted_icon(file, self.theme)) }
let icon = if self.file_style.icons { Some(painted_icon(file, self.theme)) }
else { None };
let egg = Egg { table_row, xattrs, errors, dir, file, icon };

View File

@ -16,7 +16,8 @@ pub struct Options {
/// Whether to append file class characters to file names.
pub classify: Classify,
// todo: put icons here
/// Whether to prepend icon characters before file names.
pub icons: bool,
}
impl Options {
@ -142,6 +143,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
if ! target.name.is_empty() {
let target_options = Options {
classify: Classify::JustFilenames,
icons: false,
};
let target = FileName {

View File

@ -6,14 +6,12 @@ use crate::fs::File;
use crate::output::cell::DisplayWidth;
use crate::output::file_name::Options as FileStyle;
use crate::output::icons::painted_icon;
use crate::output::lines;
use crate::theme::Theme;
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Options {
pub across: bool,
pub icons: bool,
}
impl Options {
@ -21,12 +19,6 @@ impl Options {
if self.across { tg::Direction::LeftToRight }
else { tg::Direction::TopToBottom }
}
pub fn to_lines_options(self) -> lines::Options {
lines::Options {
icons: self.icons
}
}
}
@ -48,12 +40,12 @@ impl<'a> Render<'a> {
grid.reserve(self.files.len());
for file in &self.files {
let icon = if self.opts.icons { Some(painted_icon(file, self.theme)) }
let icon = if self.file_style.icons { Some(painted_icon(file, self.theme)) }
else { None };
let filename = self.file_style.for_file(file, self.theme).paint();
let width = if self.opts.icons { DisplayWidth::from(2) + filename.width() }
let width = if self.file_style.icons { DisplayWidth::from(2) + filename.width() }
else { filename.width() };
grid.add(tg::Cell {
@ -70,7 +62,7 @@ impl<'a> Render<'a> {
// This isnt *quite* the same as the lines view, which also
// displays full link paths.
for file in &self.files {
if self.opts.icons {
if self.file_style.icons {
write!(w, "{}", painted_icon(file, self.theme))?;
}

View File

@ -156,7 +156,7 @@ impl<'a> Render<'a> {
let file_names = self.files.iter()
.map(|file| {
if self.details.icons {
if self.file_style.icons {
let mut icon_cell = TextCell::default();
icon_cell.push(ANSIGenericString::from(painted_icon(file, self.theme)), 2);
let file_cell = self.file_style.for_file(file, self.theme).paint().promote();

View File

@ -9,24 +9,18 @@ use crate::output::icons::painted_icon;
use crate::theme::Theme;
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Options {
pub icons: bool,
}
/// The lines view literally just displays each file, line-by-line.
pub struct Render<'a> {
pub files: Vec<File<'a>>,
pub theme: &'a Theme,
pub file_style: &'a FileStyle,
pub opts: &'a Options,
}
impl<'a> Render<'a> {
pub fn render<W: Write>(&self, w: &mut W) -> io::Result<()> {
for file in &self.files {
let name_cell = self.render_file(file);
if self.opts.icons {
if self.file_style.icons {
// Create a TextCell for the icon then append the text to it
let mut cell = TextCell::default();
let icon = painted_icon(file, self.theme);

View File

@ -32,7 +32,7 @@ pub enum Mode {
Grid(grid::Options),
Details(details::Options),
GridDetails(grid_details::Options),
Lines(lines::Options),
Lines,
}