Procházet zdrojové kódy

Extract method for making a cell from its contents

Benjamin Sago před 8 roky
rodič
revize
cac80410c9
4 změnil soubory, kde provedl 16 přidání a 20 odebrání
  1. 11 0
      src/output/cell.rs
  2. 4 18
      src/output/details.rs
  3. 0 1
      src/output/grid.rs
  4. 1 1
      src/output/grid_details.rs

+ 11 - 0
src/output/cell.rs

@@ -160,10 +160,21 @@ impl TextCellContents {
         ANSIStrings(&self.0)
     }
 
+    /// Calculates the width that a cell with these contents would take up, by
+    /// counting the number of characters in each unformatted ANSI string.
     pub fn width(&self) -> DisplayWidth {
         let foo = self.0.iter().map(|anstr| anstr.chars().count()).sum();
         DisplayWidth(foo)
     }
+
+    /// Promotes these contents to a full cell containing them alongside
+    /// their calculated width.
+    pub fn promote(self) -> TextCell {
+        TextCell {
+            width: self.width(),
+            contents: self,
+        }
+    }
 }
 
 

+ 4 - 18
src/output/details.rs

@@ -99,7 +99,7 @@ use fs::feature::xattr::{Attribute, FileAttributes};
 use options::{FileFilter, RecurseOptions};
 use output::colours::Colours;
 use output::column::{Alignment, Column, Columns, SizeFormat};
-use output::cell::{TextCell, DisplayWidth};
+use output::cell::{TextCell, TextCellContents, DisplayWidth};
 use output::tree::TreeTrunk;
 use output::file_name::FileName;
 
@@ -307,18 +307,10 @@ impl Details {
             let mut files = Vec::new();
             let mut errors = egg.errors;
 
-            let filename = FileName::new(&egg.file, &self.colours).paint(true, self.classify);
-            let width = filename.width();
-
-            let name = TextCell {
-                contents: filename,
-                width:    width,
-            };
-
             let row = Row {
                 depth:    depth,
                 cells:    Some(egg.cells),
-                name:     name,
+                name:     FileName::new(&egg.file, &self.colours).paint(true, self.classify).promote(),
                 last:     index == num_eggs - 1,
             };
 
@@ -451,14 +443,8 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
         self.rows.push(row);
     }
 
-    pub fn filename_cell(&self, file: File, links: bool) -> TextCell {
-        let filename = FileName::new(&file, &self.opts.colours).paint(links, self.opts.classify);
-        let width = filename.width();
-
-        TextCell {
-            contents: filename,
-            width:    width,
-        }
+    pub fn filename(&self, file: File, links: bool) -> TextCellContents {
+        FileName::new(&file, &self.opts.colours).paint(links, self.opts.classify)
     }
 
     pub fn add_file_with_cells(&mut self, cells: Vec<TextCell>, name_cell: TextCell, depth: usize, last: bool) {

+ 0 - 1
src/output/grid.rs

@@ -3,7 +3,6 @@ use std::io::{Write, Result as IOResult};
 use term_grid as grid;
 
 use fs::File;
-use output::DisplayWidth;
 use output::colours::Colours;
 use output::file_name::FileName;
 

+ 1 - 1
src/output/grid_details.rs

@@ -45,7 +45,7 @@ impl GridDetails {
                               .collect::<Vec<_>>();
 
             let file_names = files.into_iter()
-                                  .map(|file| first_table.filename_cell(file, false))
+                                  .map(|file| first_table.filename(file, false).promote())
                                   .collect::<Vec<_>>();
 
             (cells, file_names)