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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use colored::Colorize;
use tabled::{
    object::Segment, style::Color, Alignment, Modify, Panel, Rotate, Style, Table, Tabled,
};

// Colors
#[allow(dead_code)]
pub fn blue() -> Option<Color> {
    Some(Color::try_from(" ".blue().to_string()).unwrap())
}

#[allow(dead_code)]
pub fn cyan() -> Option<Color> {
    Some(Color::try_from(" ".cyan().to_string()).unwrap())
}

#[allow(dead_code)]
pub fn green() -> Option<Color> {
    Some(Color::try_from(" ".green().to_string()).unwrap())
}

#[allow(dead_code)]
pub fn red() -> Option<Color> {
    Some(Color::try_from(" ".red().to_string()).unwrap())
}

#[allow(dead_code)]
pub fn render_list<I, T>(iter: I, panel: &str, color: Option<Color>) -> String
where
    I: IntoIterator<Item = T>,
    T: Tabled,
{
    let color = if color.is_some() {
        color.unwrap()
    } else {
        cyan().unwrap()
    };
    let style = Style::rounded()
        .top_left_corner('┌')
        .top_right_corner('┐')
        .bottom_left_corner('└')
        .bottom_right_corner('┘');
    let data = Table::new(iter)
        .with(style)
        .with(color)
        .with(Modify::new(Segment::all()).with(Alignment::center()))
        .to_string();
    if !panel.is_empty() {
        format!("- {}\n{}", panel, data)
    } else {
        data
    }
}

#[allow(dead_code)]
pub fn render_object<I>(object: I, panel: &str, color: Option<Color>) -> String
where
    I: Tabled,
{
    let color = if color.is_some() {
        color.unwrap()
    } else {
        cyan().unwrap()
    };
    let panel = format!(" {}", panel);
    Table::new(vec![object])
        .with(Rotate::Left)
        .with(Rotate::Bottom)
        .with(Style::modern().off_horizontal())
        .with(color)
        .with(Panel(panel.as_str(), 0))
        .with(Modify::new(Segment::all()).with(Alignment::left()))
        .to_string()
}

/// Display bump
pub fn display_bump(bump: &[u8; 1]) -> String {
    format!("{}", bump[0])
}

#[cfg(test)]

mod render_test {
    use crate::render::*;
    use tabled::Tabled;

    #[derive(Tabled)]
    struct Person {
        name: String,
        phone: String,
        age: u32,
    }

    #[test]
    fn test_render_list() {
        let mut list = vec![];
        list.push(Person {
            name: "Murphy".to_string(),
            phone: "18600000001".to_string(),
            age: 20,
        });
        list.push(Person {
            name: "Currie".to_string(),
            phone: "18600000002".to_string(),
            age: 21,
        });
        println!("{}", render_list(list, "aaa", green()));
    }

    #[test]
    fn test_render_object() {
        let person = Person {
            name: "Murphy".to_string(),
            phone: "18600000001".to_string(),
            age: 20,
        };
        println!("{}", render_object(person, "Person", red()));
    }
}