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

Move filtering to options object, too

I'd like to have as few public methods on it as possible.
Ben S 11 лет назад
Родитель
Сommit
573765b62b
2 измененных файлов с 7 добавлено и 5 удалено
  1. 2 2
      exa.rs
  2. 5 3
      options.rs

+ 2 - 2
exa.rs

@@ -43,11 +43,11 @@ fn exa(options: &Options, path: Path) {
         Err(e) => fail!("readdir: {}", e),
     };
 
-    let files: Vec<File> = options.transform_files(paths.iter().map(|path| File::from_path(path)).collect());
+    let unordered_files: Vec<File> = paths.iter().map(|path| File::from_path(path)).collect();
+    let files: Vec<&File> = options.transform_files(&unordered_files);
     let columns = options.columns();
 
     let table: Vec<Vec<String>> = files.iter()
-        .filter(|&f| options.show(f))
         .map(|f| columns.iter().map(|c| f.display(c)).collect())
         .collect();
 

+ 5 - 3
options.rs

@@ -45,7 +45,7 @@ impl Options {
         }
     }
 
-    pub fn show(&self, f: &File) -> bool {
+    fn show(&self, f: &File) -> bool {
         if self.showInvisibles {
             true
         } else {
@@ -53,8 +53,10 @@ impl Options {
         }
     }
 
-    pub fn transform_files<'a>(&self, unordered_files: Vec<File<'a>>) -> Vec<File<'a>> {
-        let mut files = unordered_files.clone();
+    pub fn transform_files<'a>(&self, unordered_files: &'a Vec<File<'a>>) -> Vec<&'a File<'a>> {
+        let mut files: Vec<&'a File<'a>> = unordered_files.iter()
+            .filter(|&f| self.show(f))
+            .collect();
 
         match self.sortField {
             Name => files.sort_by(|a, b| a.name.cmp(&b.name)),