Similarly, turn Classify into an enum

This commit is contained in:
Benjamin Sago 2017-05-07 15:31:00 +01:00
parent 39381bfb49
commit e916097e0e
5 changed files with 51 additions and 19 deletions

View File

@ -4,8 +4,9 @@ use getopts;
use output::Colours; use output::Colours;
use output::{Grid, Details, GridDetails, Lines}; use output::{Grid, Details, GridDetails, Lines};
use options::{FileFilter, DirAction, Misfire};
use output::column::{Columns, TimeTypes, SizeFormat}; use output::column::{Columns, TimeTypes, SizeFormat};
use output::file_name::Classify;
use options::{FileFilter, DirAction, Misfire};
use term::dimensions; use term::dimensions;
use fs::feature::xattr; use fs::feature::xattr;
@ -58,7 +59,7 @@ impl View {
filter: filter.clone(), filter: filter.clone(),
xattr: xattr::ENABLED && matches.opt_present("extended"), xattr: xattr::ENABLED && matches.opt_present("extended"),
colours: colours, colours: colours,
classify: matches.opt_present("classify"), classify: Classify::deduce(matches),
}; };
Ok(details) Ok(details)
@ -87,8 +88,7 @@ impl View {
}; };
let other_options_scan = || { let other_options_scan = || {
let classify = matches.opt_present("classify"); let classify = Classify::deduce(matches);
let term_colours = TerminalColours::deduce(matches)?; let term_colours = TerminalColours::deduce(matches)?;
let term_width = TerminalWidth::deduce()?; let term_width = TerminalWidth::deduce()?;
@ -366,3 +366,12 @@ impl TerminalColours {
} }
} }
} }
impl Classify {
fn deduce(matches: &getopts::Matches) -> Classify {
if matches.opt_present("classify") { Classify::AddFileIndicators }
else { Classify::JustFilenames }
}
}

View File

@ -101,7 +101,7 @@ use output::colours::Colours;
use output::column::{Alignment, Column, Columns, SizeFormat}; use output::column::{Alignment, Column, Columns, SizeFormat};
use output::cell::{TextCell, TextCellContents, DisplayWidth}; use output::cell::{TextCell, TextCellContents, DisplayWidth};
use output::tree::TreeTrunk; use output::tree::TreeTrunk;
use output::file_name::{FileName, LinkStyle}; use output::file_name::{FileName, LinkStyle, Classify};
/// With the **Details** view, the output gets formatted into columns, with /// With the **Details** view, the output gets formatted into columns, with
@ -142,7 +142,7 @@ pub struct Details {
pub colours: Colours, pub colours: Colours,
/// Whether to show a file type indiccator. /// Whether to show a file type indiccator.
pub classify: bool, pub classify: Classify,
} }
/// The **environment** struct contains any data that could change between /// The **environment** struct contains any data that could change between
@ -310,7 +310,7 @@ impl Details {
let row = Row { let row = Row {
depth: depth, depth: depth,
cells: Some(egg.cells), cells: Some(egg.cells),
name: FileName::new(&egg.file, LinkStyle::FullLinkPaths, &self.colours).paint(self.classify).promote(), name: FileName::new(&egg.file, LinkStyle::FullLinkPaths, self.classify, &self.colours).paint().promote(),
last: index == num_eggs - 1, last: index == num_eggs - 1,
}; };
@ -444,7 +444,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
} }
pub fn filename(&self, file: File, links: LinkStyle) -> TextCellContents { pub fn filename(&self, file: File, links: LinkStyle) -> TextCellContents {
FileName::new(&file, links, &self.opts.colours).paint(self.opts.classify) FileName::new(&file, links, self.opts.classify, &self.opts.colours).paint()
} }
pub fn add_file_with_cells(&mut self, cells: Vec<TextCell>, name_cell: TextCell, depth: usize, last: bool) { pub fn add_file_with_cells(&mut self, cells: Vec<TextCell>, name_cell: TextCell, depth: usize, last: bool) {

View File

@ -23,6 +23,9 @@ pub struct FileName<'a, 'dir: 'a> {
/// How to handle displaying links. /// How to handle displaying links.
link_style: LinkStyle, link_style: LinkStyle,
/// Whether to append file class characters to file names.
classify: Classify,
} }
@ -30,7 +33,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
/// Create a new `FileName` that prints the given files name, painting it /// Create a new `FileName` that prints the given files name, painting it
/// with the remaining arguments. /// with the remaining arguments.
pub fn new(file: &'a File<'dir>, link_style: LinkStyle, colours: &'a Colours) -> FileName<'a, 'dir> { pub fn new(file: &'a File<'dir>, link_style: LinkStyle, classify: Classify, colours: &'a Colours) -> FileName<'a, 'dir> {
let target = if file.is_link() { Some(file.link_target()) } let target = if file.is_link() { Some(file.link_target()) }
else { None }; else { None };
FileName { FileName {
@ -38,6 +41,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
colours: colours, colours: colours,
target: target, target: target,
link_style: link_style, link_style: link_style,
classify: classify,
} }
} }
@ -48,7 +52,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
/// This method returns some `TextCellContents`, rather than a `TextCell`, /// This method returns some `TextCellContents`, rather than a `TextCell`,
/// because for the last cell in a table, it doesnt need to have its /// because for the last cell in a table, it doesnt need to have its
/// width calculated. /// width calculated.
pub fn paint(&self, classify: bool) -> TextCellContents { pub fn paint(&self) -> TextCellContents {
let mut bits = Vec::new(); let mut bits = Vec::new();
if self.file.dir.is_none() { if self.file.dir.is_none() {
@ -75,7 +79,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
} }
if !target.name.is_empty() { if !target.name.is_empty() {
let target = FileName::new(&target, LinkStyle::FullLinkPaths, self.colours); let target = FileName::new(&target, LinkStyle::FullLinkPaths, Classify::JustFilenames, self.colours);
for bit in target.coloured_file_name() { for bit in target.coloured_file_name() {
bits.push(bit); bits.push(bit);
} }
@ -94,7 +98,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
}, },
} }
} }
else if classify { else if let Classify::AddFileIndicators = self.classify {
if let Some(class) = self.classify_char() { if let Some(class) = self.classify_char() {
bits.push(Style::default().paint(class)); bits.push(Style::default().paint(class));
} }
@ -213,3 +217,22 @@ pub enum LinkStyle {
/// a broken link, and doing nothing if it cant be followed. /// a broken link, and doing nothing if it cant be followed.
FullLinkPaths, FullLinkPaths,
} }
/// Whether to append file class characters to the file names.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum Classify {
/// Just display the file names, without any characters.
JustFilenames,
/// Add a character after the file name depending on what class of file
/// it is.
AddFileIndicators,
}
impl Default for Classify {
fn default() -> Classify {
Classify::JustFilenames
}
}

View File

@ -4,7 +4,7 @@ use term_grid as grid;
use fs::File; use fs::File;
use output::colours::Colours; use output::colours::Colours;
use output::file_name::{FileName, LinkStyle}; use output::file_name::{FileName, LinkStyle, Classify};
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(PartialEq, Debug, Copy, Clone)]
@ -12,7 +12,7 @@ pub struct Grid {
pub across: bool, pub across: bool,
pub console_width: usize, pub console_width: usize,
pub colours: Colours, pub colours: Colours,
pub classify: bool, pub classify: Classify,
} }
impl Grid { impl Grid {
@ -28,7 +28,7 @@ impl Grid {
grid.reserve(files.len()); grid.reserve(files.len());
for file in files.iter() { for file in files.iter() {
let filename = FileName::new(file, LinkStyle::JustFilenames, &self.colours).paint(self.classify); let filename = FileName::new(file, LinkStyle::JustFilenames, self.classify, &self.colours).paint();
let width = filename.width(); let width = filename.width();
grid.add(grid::Cell { grid.add(grid::Cell {
@ -43,7 +43,7 @@ impl Grid {
else { else {
// File names too long for a grid - drop down to just listing them! // File names too long for a grid - drop down to just listing them!
for file in files.iter() { for file in files.iter() {
let name_cell = FileName::new(file, LinkStyle::JustFilenames, &self.colours).paint(self.classify); let name_cell = FileName::new(file, LinkStyle::JustFilenames, self.classify, &self.colours).paint();
writeln!(w, "{}", name_cell.strings())?; writeln!(w, "{}", name_cell.strings())?;
} }
Ok(()) Ok(())

View File

@ -4,21 +4,21 @@ use ansi_term::ANSIStrings;
use fs::File; use fs::File;
use output::file_name::{FileName, LinkStyle}; use output::file_name::{FileName, LinkStyle, Classify};
use super::colours::Colours; use super::colours::Colours;
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub struct Lines { pub struct Lines {
pub colours: Colours, pub colours: Colours,
pub classify: bool, pub classify: Classify,
} }
/// The lines view literally just displays each file, line-by-line. /// The lines view literally just displays each file, line-by-line.
impl Lines { impl Lines {
pub fn view<W: Write>(&self, files: Vec<File>, w: &mut W) -> IOResult<()> { pub fn view<W: Write>(&self, files: Vec<File>, w: &mut W) -> IOResult<()> {
for file in files { for file in files {
let name_cell = FileName::new(&file, LinkStyle::FullLinkPaths, &self.colours).paint(self.classify); let name_cell = FileName::new(&file, LinkStyle::FullLinkPaths, self.classify, &self.colours).paint();
writeln!(w, "{}", ANSIStrings(&name_cell))?; writeln!(w, "{}", ANSIStrings(&name_cell))?;
} }
Ok(()) Ok(())