Просмотр исходного кода

Similarly, turn Classify into an enum

Benjamin Sago 8 лет назад
Родитель
Сommit
e916097e0e
5 измененных файлов с 51 добавлено и 19 удалено
  1. 13 4
      src/options/view.rs
  2. 4 4
      src/output/details.rs
  3. 27 4
      src/output/file_name.rs
  4. 4 4
      src/output/grid.rs
  5. 3 3
      src/output/lines.rs

+ 13 - 4
src/options/view.rs

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

+ 4 - 4
src/output/details.rs

@@ -101,7 +101,7 @@ use output::colours::Colours;
 use output::column::{Alignment, Column, Columns, SizeFormat};
 use output::cell::{TextCell, TextCellContents, DisplayWidth};
 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
@@ -142,7 +142,7 @@ pub struct Details {
     pub colours: Colours,
 
     /// Whether to show a file type indiccator.
-    pub classify: bool,
+    pub classify: Classify,
 }
 
 /// The **environment** struct contains any data that could change between
@@ -310,7 +310,7 @@ impl Details {
             let row = Row {
                 depth:    depth,
                 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,
             };
 
@@ -444,7 +444,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
     }
 
     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) {

+ 27 - 4
src/output/file_name.rs

@@ -23,6 +23,9 @@ pub struct FileName<'a, 'dir: 'a> {
 
     /// How to handle displaying links.
     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 file’s name, painting it
     /// 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()) }
                                                        else { None };
         FileName {
@@ -38,6 +41,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
             colours: colours,
             target: target,
             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`,
     /// because for the last cell in a table, it doesn’t need to have its
     /// width calculated.
-    pub fn paint(&self, classify: bool) -> TextCellContents {
+    pub fn paint(&self) -> TextCellContents {
         let mut bits = Vec::new();
 
         if self.file.dir.is_none() {
@@ -75,7 +79,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
                     }
 
                     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() {
                             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() {
                 bits.push(Style::default().paint(class));
             }
@@ -213,3 +217,22 @@ pub enum LinkStyle {
     /// a broken link, and doing nothing if it can’t be followed.
     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
+    }
+}

+ 4 - 4
src/output/grid.rs

@@ -4,7 +4,7 @@ use term_grid as grid;
 
 use fs::File;
 use output::colours::Colours;
-use output::file_name::{FileName, LinkStyle};
+use output::file_name::{FileName, LinkStyle, Classify};
 
 
 #[derive(PartialEq, Debug, Copy, Clone)]
@@ -12,7 +12,7 @@ pub struct Grid {
     pub across: bool,
     pub console_width: usize,
     pub colours: Colours,
-    pub classify: bool,
+    pub classify: Classify,
 }
 
 impl Grid {
@@ -28,7 +28,7 @@ impl Grid {
         grid.reserve(files.len());
 
         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();
 
             grid.add(grid::Cell {
@@ -43,7 +43,7 @@ impl Grid {
         else {
             // File names too long for a grid - drop down to just listing them!
             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())?;
             }
             Ok(())

+ 3 - 3
src/output/lines.rs

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