Procházet zdrojové kódy

Add --binary flag

This finally means that the list of columns is no longer fixed, which means it has to be generated somewhere. And guess where it got moved to? That's right, the options object! (see previous commits)
Ben S před 11 roky
rodič
revize
0dd74a022f
2 změnil soubory, kde provedl 16 přidání a 15 odebrání
  1. 2 3
      exa.rs
  2. 14 12
      options.rs

+ 2 - 3
exa.rs

@@ -45,17 +45,16 @@ fn exa(options: &Options, path: Path) {
 
     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()
-        .map(|f| columns.iter().map(|c| f.display(c)).collect())
+        .map(|f| options.columns.iter().map(|c| f.display(c)).collect())
         .collect();
 
     let lengths: Vec<Vec<uint>> = table.iter()
         .map(|row| row.iter().map(|col| colours::strip_formatting(col).len()).collect())
         .collect();
 
-    let maxes: Vec<uint> = range(0, columns.len())
+    let maxes: Vec<uint> = range(0, options.columns.len())
         .map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
         .collect();
 

+ 14 - 12
options.rs

@@ -13,6 +13,7 @@ pub struct Options {
     pub sortField: SortField,
     pub reverse: bool,
     pub dirs: Vec<String>,
+    pub columns: ~[Column],
 }
 
 impl SortField {
@@ -30,6 +31,7 @@ impl Options {
     pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> {
         let opts = ~[
             getopts::optflag("a", "all", "show dot-files"),
+            getopts::optflag("b", "binary", "use binary prefixes in file sizes"),
             getopts::optflag("r", "reverse", "reverse order of files"),
             getopts::optopt("s", "sort", "field to sort by", "WORD"),
         ];
@@ -40,11 +42,22 @@ impl Options {
                 showInvisibles: matches.opt_present("all"),
                 reverse: matches.opt_present("reverse"),
                 sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
-                dirs: matches.free,
+                dirs: matches.free.clone(),
+                columns: Options::columns(matches),
             })
         }
     }
 
+    fn columns(matches: getopts::Matches) -> ~[Column] {
+        return ~[
+            Permissions,
+            FileSize(matches.opt_present("binary")),
+            User,
+            Group,
+            FileName,
+        ];
+    }
+
     fn show(&self, f: &File) -> bool {
         if self.showInvisibles {
             true
@@ -74,15 +87,4 @@ impl Options {
 
         return files;
     }
-
-    pub fn columns(&self) -> ~[Column] {
-        return ~[
-            Permissions,
-            FileSize(false),
-            User,
-            Group,
-            FileName,
-        ];
-    }
-
 }