Selaa lähdekoodia

Add --across parameter

Ben S 11 vuotta sitten
vanhempi
sitoutus
240cbf7b41
3 muutettua tiedostoa jossa 18 lisäystä ja 9 poistoa
  1. 1 1
      README.md
  2. 14 6
      src/exa.rs
  3. 3 2
      src/options.rs

+ 1 - 1
README.md

@@ -23,7 +23,7 @@ Options
 - **-r**, **--reverse**: reverse sort order
 - **-s**, **--sort=(name, size, ext)**: field to sort by
 - **-S**, **--blocks**: show number of file system blocks
-
+- **-x**, **--across**: sort multi-column view entries across
 
 Installation
 ------------

+ 14 - 6
src/exa.rs

@@ -51,7 +51,7 @@ fn exa(opts: &Options) {
                 if print_dir_names { println!("{}:", dir_name); }
                 match opts.view {
                     Lines(ref cols) => lines_view(opts, cols, dir),
-                    Grid => grid_view(opts, dir),
+                    Grid(bool) => grid_view(opts, bool, dir),
                 }
             }
             Err(e) => {
@@ -62,26 +62,34 @@ fn exa(opts: &Options) {
     }
 }
 
-fn grid_view(options: &Options, dir: Dir) {
+fn grid_view(options: &Options, across: bool, dir: Dir) {
     let unsorted_files = dir.files();
     let files: Vec<&File> = options.transform_files(&unsorted_files);
     
     let max_column_length = files.iter().map(|f| f.name.len()).max().unwrap();
     let console_width = 80;
     let num_columns = (console_width + 1) / (max_column_length + 1);
+    let count = files.len();
 
-    let mut num_rows = files.len() / num_columns;
-    if files.len() % num_columns != 0 {
+    let mut num_rows = count / num_columns;
+    if count % num_columns != 0 {
         num_rows += 1;
     }
     
     for y in range(0, num_rows) {
         for x in range(0, num_columns) {
-            if y * num_columns + x >= files.len() {
+            let num = if across {
+                y * num_columns + x
+            }
+            else {
+                y + num_rows * x
+            };
+            
+            if num >= count {
                 continue;
             }
             
-            let file = files.get(y * num_columns + x);
+            let file = files.get(num);
             let file_name = file.name.clone();
             let styled_name = file.file_colour().paint(file_name.as_slice());
             print!("{}", Left.pad_string(&styled_name, max_column_length - file_name.len() + 1));

+ 3 - 2
src/options.rs

@@ -22,7 +22,7 @@ impl SortField {
 
 pub enum View {
     Lines(Vec<Column>),
-    Grid,
+    Grid(bool),
 }
 
 pub struct Options {
@@ -48,6 +48,7 @@ impl Options {
             getopts::optflag("r", "reverse", "reverse order of files"),
             getopts::optopt("s", "sort", "field to sort by", "WORD"),
             getopts::optflag("S", "blocks", "show number of file system blocks"),
+            getopts::optflag("x", "across", "sort multi-column view entries across"),
         ];
 
         match getopts::getopts(args.tail(), opts) {
@@ -68,7 +69,7 @@ impl Options {
             Lines(Options::columns(matches))
         }
         else {
-            Grid
+            Grid(matches.opt_present("across"))
         }
     }