瀏覽代碼

Move sorting and reversing into options object

Ben S 11 年之前
父節點
當前提交
1dbe20c8c5
共有 3 個文件被更改,包括 48 次插入38 次删除
  1. 2 7
      exa.rs
  2. 25 14
      file.rs
  3. 21 17
      options.rs

+ 2 - 7
exa.rs

@@ -43,12 +43,7 @@ fn exa(options: &Options, path: Path) {
         Err(e) => fail!("readdir: {}", e),
     };
 
-    let mut files = paths.iter().map(|path| File::from_path(path)).collect();
-    options.sort(&mut files);
-    if options.reverse {
-        files.reverse();
-    }
-
+    let files: Vec<File> = options.transform_files(paths.iter().map(|path| File::from_path(path)).collect());
     let columns = options.columns();
 
     let table: Vec<Vec<String>> = files.iter()
@@ -57,7 +52,7 @@ fn exa(options: &Options, path: Path) {
         .collect();
 
     let lengths: Vec<Vec<uint>> = table.iter()
-        .map(|row| row.iter().map(|col| colours::strip_formatting(col).len() ).collect())
+        .map(|row| row.iter().map(|col| colours::strip_formatting(col).len()).collect())
         .collect();
 
     let maxes: Vec<uint> = range(0, columns.len())

+ 25 - 14
file.rs

@@ -120,23 +120,34 @@ impl<'a> File<'a> {
         let bits = self.stat.perm;
         return format!("{}{}{}{}{}{}{}{}{}{}",
             self.type_char(),
-            bit(bits, io::UserRead, "r", Yellow.bold()),
-            bit(bits, io::UserWrite, "w", Red.bold()),
-            bit(bits, io::UserExecute, "x", Green.bold().underline()),
-            bit(bits, io::GroupRead, "r", Yellow.normal()),
-            bit(bits, io::GroupWrite, "w", Red.normal()),
-            bit(bits, io::GroupExecute, "x", Green.normal()),
-            bit(bits, io::OtherRead, "r", Yellow.normal()),
-            bit(bits, io::OtherWrite, "w", Red.normal()),
-            bit(bits, io::OtherExecute, "x", Green.normal()),
+            File::bit(bits, io::UserRead, "r", Yellow.bold()),
+            File::bit(bits, io::UserWrite, "w", Red.bold()),
+            File::bit(bits, io::UserExecute, "x", Green.bold().underline()),
+            File::bit(bits, io::GroupRead, "r", Yellow.normal()),
+            File::bit(bits, io::GroupWrite, "w", Red.normal()),
+            File::bit(bits, io::GroupExecute, "x", Green.normal()),
+            File::bit(bits, io::OtherRead, "r", Yellow.normal()),
+            File::bit(bits, io::OtherWrite, "w", Red.normal()),
+            File::bit(bits, io::OtherExecute, "x", Green.normal()),
        );
     }
+
+    fn bit(bits: io::FilePermission, bit: io::FilePermission, other: &'static str, style: Style) -> String {
+        if bits.contains(bit) {
+            style.paint(other.as_slice())
+        } else {
+            Black.bold().paint("-".as_slice())
+        }
+    }
 }
 
-fn bit(bits: io::FilePermission, bit: io::FilePermission, other: &'static str, style: Style) -> String {
-    if bits.contains(bit) {
-        style.paint(other.as_slice())
-    } else {
-        Black.bold().paint("-".as_slice())
+impl<'a> Clone for File<'a> {
+    fn clone(&self) -> File<'a> {
+        return File {
+            path: self.path,
+            stat: self.stat,
+            name: self.name.clone(),
+            ext:  self.ext.clone(),
+        };
     }
 }

+ 21 - 17
options.rs

@@ -16,7 +16,7 @@ pub struct Options {
 }
 
 impl SortField {
-    pub fn from_word(word: String) -> SortField {
+    fn from_word(word: String) -> SortField {
         match word.as_slice() {
             "name" => Name,
             "size" => Size,
@@ -24,18 +24,6 @@ impl SortField {
             _ => fail!("Invalid sorting order"),
         }
     }
-
-    fn sort(&self, files: &mut Vec<File>) {
-        match *self {
-            Name => files.sort_by(|a, b| a.name.cmp(&b.name)),
-            Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
-            Extension => files.sort_by(|a, b| {
-                let exts = a.ext.cmp(&b.ext);
-                let names = a.name.cmp(&b.name);
-                lexical_ordering(exts, names)
-            }),
-        }
-    }
 }
 
 impl Options {
@@ -57,10 +45,6 @@ impl Options {
         }
     }
 
-    pub fn sort(&self, files: &mut Vec<File>) {
-        self.sortField.sort(files);
-    }
-
     pub fn show(&self, f: &File) -> bool {
         if self.showInvisibles {
             true
@@ -69,6 +53,26 @@ impl Options {
         }
     }
 
+    pub fn transform_files<'a>(&self, unordered_files: Vec<File<'a>>) -> Vec<File<'a>> {
+        let mut files = unordered_files.clone();
+
+        match self.sortField {
+            Name => files.sort_by(|a, b| a.name.cmp(&b.name)),
+            Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
+            Extension => files.sort_by(|a, b| {
+                let exts = a.ext.cmp(&b.ext);
+                let names = a.name.cmp(&b.name);
+                lexical_ordering(exts, names)
+            }),
+        }
+
+        if self.reverse {
+            files.reverse();
+        }
+
+        return files;
+    }
+
     pub fn columns(&self) -> ~[Column] {
         return ~[
             Permissions,