1use colored::Colorize;
2use tabled::{
3 object::Segment, style::Color, Alignment, Modify, Panel, Rotate, Style, Table, Tabled,
4};
5
6#[allow(dead_code)]
8pub fn blue() -> Option<Color> {
9 Some(Color::try_from(" ".blue().to_string()).unwrap())
10}
11
12#[allow(dead_code)]
13pub fn cyan() -> Option<Color> {
14 Some(Color::try_from(" ".cyan().to_string()).unwrap())
15}
16
17#[allow(dead_code)]
18pub fn green() -> Option<Color> {
19 Some(Color::try_from(" ".green().to_string()).unwrap())
20}
21
22#[allow(dead_code)]
23pub fn red() -> Option<Color> {
24 Some(Color::try_from(" ".red().to_string()).unwrap())
25}
26
27#[allow(dead_code)]
28pub fn render_list<I, T>(iter: I, panel: &str, color: Option<Color>) -> String
29where
30 I: IntoIterator<Item = T>,
31 T: Tabled,
32{
33 let color = if color.is_some() {
34 color.unwrap()
35 } else {
36 cyan().unwrap()
37 };
38 let style = Style::rounded()
39 .top_left_corner('┌')
40 .top_right_corner('┐')
41 .bottom_left_corner('└')
42 .bottom_right_corner('┘');
43 let data = Table::new(iter)
44 .with(style)
45 .with(color)
46 .with(Modify::new(Segment::all()).with(Alignment::center()))
47 .to_string();
48 if !panel.is_empty() {
49 format!("- {}\n{}", panel, data)
50 } else {
51 data
52 }
53}
54
55#[allow(dead_code)]
56pub fn render_object<I>(object: I, panel: &str, color: Option<Color>) -> String
57where
58 I: Tabled,
59{
60 let color = if color.is_some() {
61 color.unwrap()
62 } else {
63 cyan().unwrap()
64 };
65 let panel = format!(" {}", panel);
66 Table::new(vec![object])
67 .with(Rotate::Left)
68 .with(Rotate::Bottom)
69 .with(Style::modern().off_horizontal())
70 .with(color)
71 .with(Panel(panel.as_str(), 0))
72 .with(Modify::new(Segment::all()).with(Alignment::left()))
73 .to_string()
74}
75
76pub fn display_bump(bump: &[u8; 1]) -> String {
78 format!("{}", bump[0])
79}
80
81#[cfg(test)]
82
83mod render_test {
84 use crate::render::*;
85 use tabled::Tabled;
86
87 #[derive(Tabled)]
88 struct Person {
89 name: String,
90 phone: String,
91 age: u32,
92 }
93
94 #[test]
95 fn test_render_list() {
96 let mut list = vec![];
97 list.push(Person {
98 name: "Murphy".to_string(),
99 phone: "18600000001".to_string(),
100 age: 20,
101 });
102 list.push(Person {
103 name: "Currie".to_string(),
104 phone: "18600000002".to_string(),
105 age: 21,
106 });
107 println!("{}", render_list(list, "aaa", green()));
108 }
109
110 #[test]
111 fn test_render_object() {
112 let person = Person {
113 name: "Murphy".to_string(),
114 phone: "18600000001".to_string(),
115 age: 20,
116 };
117 println!("{}", render_object(person, "Person", red()));
118 }
119}