d_stuff/
pretty.rs

1use super::*;
2use terminal_size::{terminal_size, Height, Width};
3
4pub struct Pretty {
5    entries: Vec<Entry>,
6}
7
8impl Pretty {
9    pub fn new() -> Self {
10        Self { entries: vec![] }
11    }
12
13    pub fn add(&mut self, entriy: Entry) {
14        self.entries.push(entriy);
15    }
16
17    pub fn width() -> usize {
18        let size = terminal_size();
19        if let Some((Width(w), Height(_))) = size {
20            w as usize
21        } else {
22            80
23        }
24    }
25
26    pub fn border_style() -> &'static str {
27        termion::style::Reset.as_ref()
28    }
29    pub fn border_color() -> &'static str {
30        termion::color::White.fg_str()
31    }
32
33    pub fn print(&self) {
34        let width = Self::width();
35
36        print!("{}{}", termion::clear::All, termion::cursor::Goto(1, 1));
37
38        if let Some((first, others)) = self.entries.split_first() {
39            first.print(width, true, others.is_empty());
40
41            if let Some((last, others)) = others.split_last() {
42                for entry in others {
43                    entry.print(width, false, false);
44                }
45                last.print(width, false, true);
46            }
47        } else {
48            println!();
49        }
50    }
51}