hyper_scripter/list/
grid.rs

1use super::get_screen_width;
2use term_grid::{Cell, Direction, Filling, Grid as LibGrid, GridOptions};
3
4pub struct Grid {
5    capacity: usize,
6    grid: LibGrid,
7    term_width: Option<usize>,
8}
9
10impl Grid {
11    pub fn new(capacity: usize) -> Self {
12        let mut grid = LibGrid::new(GridOptions {
13            direction: Direction::TopToBottom,
14            filling: Filling::Spaces(2),
15        });
16        grid.reserve(capacity);
17        Self {
18            grid,
19            capacity,
20            term_width: None,
21        }
22    }
23    pub fn add(&mut self, contents: String, width: usize) {
24        let cell = Cell { contents, width };
25        self.grid.add(cell);
26    }
27    pub fn clear(&mut self) {
28        *self = Grid::new(self.capacity);
29    }
30    pub fn fit_into_screen<'a>(&'a mut self) -> impl std::fmt::Display + 'a {
31        if self.term_width.is_none() {
32            let w = get_screen_width();
33            self.term_width = Some(w as usize);
34        }
35
36        let width = self.term_width.unwrap();
37        if width > 0 {
38            if let Some(grid_display) = self.grid.fit_into_width(width) {
39                return grid_display;
40            }
41        }
42        self.grid.fit_into_columns(1)
43    }
44}