Adapt the long grid view to the console width

This commit is contained in:
Ben S 2015-06-28 16:25:59 +01:00
parent ccdf9ff4a6
commit 08f3514d68

View File

@ -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();
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,
};
grid.add(cell);
}
}
}
print!("{}", grid.fit_into_columns(columns));
}
}
impl convert::From<Cell> for grid::Cell {
fn from(input: Cell) -> Self {
grid::Cell {
contents: input.text,
width: input.length,
}
grid
}
}