summaryrefslogtreecommitdiffstats
path: root/src/output/grid.rs
blob: 0fd0af407fe72b43991a84713e0e1d5c1a245f85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use colours::Colours;
use file::File;
use filetype::file_colour;

use term_grid as grid;


#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Grid {
    pub across: bool,
    pub console_width: usize,
    pub colours: Colours,
}

impl Grid {
    pub fn view(&self, files: &[File]) {
        let direction = if self.across { grid::Direction::LeftToRight }
                                  else { grid::Direction::TopToBottom };

        let mut grid = grid::Grid::new(grid::GridOptions {
            direction:  direction,
            filling:    grid::Filling::Spaces(2),
        });

        grid.reserve(files.len());

        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(),
            });
        }

        if let Some(display) = grid.fit_into_width(self.console_width) {
            print!("{}", display);
        }
        else {
            // 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));
            }
        }
    }
}