Sfoglia il codice sorgente

Adapt the long grid view to the console width

Ben S 10 anni fa
parent
commit
08f3514d68
1 ha cambiato i file con 31 aggiunte e 20 eliminazioni
  1. 31 20
      src/output/grid_details.rs

+ 31 - 20
src/output/grid_details.rs

@@ -1,4 +1,3 @@
-use std::convert;
 use std::iter::repeat;
 
 use term_grid as grid;
@@ -17,44 +16,56 @@ pub struct GridDetails {
 
 impl GridDetails {
     pub fn view(&self, dir: Option<&Dir>, files: &[File]) {
+        let mut last_working_table = self.make_grid(1, dir, files);
 
-        let columns = 2;
+        for column_count in 2.. {
+            let grid = self.make_grid(column_count, dir, files);
 
+            if grid.fit_into_columns(column_count).width() <= self.grid.console_width {
+                last_working_table = grid;
+            }
+            else {
+                print!("{}", last_working_table.fit_into_columns(column_count - 1));
+                return;
+            }
+        }
+    }
+
+    pub fn make_grid(&self, column_count: usize, dir: Option<&Dir>, files: &[File]) -> grid::Grid {
         let make_table = || {
             let mut table = Table::with_options(self.details.colours, self.details.columns.for_dir(dir));
             if self.details.header { table.add_header() }
             table
         };
 
-        let mut tables: Vec<_> = repeat(()).map(|_| make_table()).take(columns).collect();
+        let mut tables: Vec<_> = repeat(()).map(|_| make_table()).take(column_count).collect();
 
         for (i, file) in files.iter().enumerate() {
-            tables[i % columns].add_file(file, 0, false, false);
+            tables[i % column_count].add_file(file, 0, false, false);
         }
 
-        let direction = if self.grid.across { grid::Direction::LeftToRight }
-                                       else { grid::Direction::TopToBottom };
+        let direction = grid::Direction::LeftToRight;
 
         let mut grid = grid::Grid::new(grid::GridOptions {
             direction:        direction,
-            separator_width:  2,
+            separator_width:  4,
         });
 
-        for table in tables {
-            for cell in table.print_table(false, false).into_iter() {
-                grid.add(cell.into());
-            }
-        }
+        let columns: Vec<_> = tables.iter().map(|t| t.print_table(false, false)).collect();
 
-        print!("{}", grid.fit_into_columns(columns));
-    }
-}
+        for row in 0 .. columns[0].len() {
+            for column in columns.iter() {
+                if row < column.len() {
+                    let cell = grid::Cell {
+                        contents: column[row].text.clone(),
+                        width:    column[row].length,
+                    };
 
-impl convert::From<Cell> for grid::Cell {
-    fn from(input: Cell) -> Self {
-        grid::Cell {
-            contents: input.text,
-            width:    input.length,
+                    grid.add(cell);
+                }
+            }
         }
+
+        grid
     }
 }