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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::*;
use terminal_size::{terminal_size, Height, Width};

pub struct Pretty {
    entries: Vec<Entry>,
}

impl Pretty {
    pub fn new() -> Self {
        Self { entries: vec![] }
    }

    pub fn add(&mut self, entriy: Entry) {
        self.entries.push(entriy);
    }

    pub fn width() -> usize {
        let size = terminal_size();
        if let Some((Width(w), Height(_))) = size {
            w as usize
        } else {
            80
        }
    }

    pub fn border_style() -> &'static str {
        termion::style::Reset.as_ref()
    }
    pub fn border_color() -> &'static str {
        termion::color::White.fg_str()
    }

    pub fn print(&self) {
        let width = Self::width();

        print!("{}{}", termion::clear::All, termion::cursor::Goto(1, 1));
        if self.entries.is_empty() {
            println!();
            return;
        }

        let first = self.entries.first().unwrap();
        first.print(width, Position::First);

        if self.entries.len() > 2 {
            for entriy in self.entries[1..self.entries.len() - 1].iter() {
                entriy.print(width, Position::Middle);
            }
        }

        if self.entries.len() > 1 {
            let last = self.entries.last().unwrap();
            last.print(width, Position::Last);
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Position {
    First,
    Middle,
    Last,
}