2015-05-09 22:57:18 +00:00
|
|
|
use colours::Colours;
|
2015-02-05 14:39:56 +00:00
|
|
|
use file::File;
|
2015-05-09 22:57:18 +00:00
|
|
|
use filetype::file_colour;
|
2015-06-08 20:33:39 +00:00
|
|
|
|
2015-06-23 09:54:57 +00:00
|
|
|
use term_grid as grid;
|
2015-02-05 14:39:56 +00:00
|
|
|
|
|
|
|
|
2015-04-03 22:14:49 +00:00
|
|
|
#[derive(PartialEq, Debug, Copy, Clone)]
|
2015-02-05 14:39:56 +00:00
|
|
|
pub struct Grid {
|
|
|
|
pub across: bool,
|
|
|
|
pub console_width: usize,
|
2015-05-09 22:57:18 +00:00
|
|
|
pub colours: Colours,
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Grid {
|
2015-06-23 09:54:57 +00:00
|
|
|
pub fn view(&self, files: &[File]) {
|
|
|
|
let direction = if self.across { grid::Direction::LeftToRight }
|
|
|
|
else { grid::Direction::TopToBottom };
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-06-23 09:54:57 +00:00
|
|
|
let mut grid = grid::Grid::new(grid::GridOptions {
|
|
|
|
direction: direction,
|
|
|
|
separator_width: 2,
|
|
|
|
});
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-06-23 09:54:57 +00:00
|
|
|
grid.reserve(files.len());
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-06-23 09:54:57 +00:00
|
|
|
for file in files.iter() {
|
|
|
|
grid.add(grid::Cell {
|
|
|
|
contents: file_colour(&self.colours, file).paint(&file.name).to_string(),
|
|
|
|
width: file.file_name_width(),
|
|
|
|
});
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 09:54:57 +00:00
|
|
|
if let Some(display) = grid.fit_into_width(self.console_width) {
|
|
|
|
print!("{}", display);
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-06-23 09:54:57 +00:00
|
|
|
// File names too long for a grid - drop down to just listing them!
|
|
|
|
for file in files.iter() {
|
|
|
|
println!("{}", file_colour(&self.colours, file).paint(&file.name));
|
|
|
|
}
|
2015-02-05 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|