diff options
author | Ben S <ogham@bsago.me> | 2015-06-28 16:25:59 +0100 |
---|---|---|
committer | Ben S <ogham@bsago.me> | 2015-06-28 16:25:59 +0100 |
commit | 08f3514d686d92fd93cef96d9c52d055f009c5a6 (patch) | |
tree | 7793230f1e03c8a2ee5a1d62e424c4dbc9e869a3 /src/output | |
parent | ccdf9ff4a679e00298f7e0fd9bd68318ded20cb8 (diff) |
Adapt the long grid view to the console width
Diffstat (limited to 'src/output')
-rw-r--r-- | src/output/grid_details.rs | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/src/output/grid_details.rs b/src/output/grid_details.rs index 93e498a..b5e8cf5 100644 --- a/src/output/grid_details.rs +++ b/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 } } |