cetus_utils/
render.rs

1use colored::Colorize;
2use tabled::{
3    object::Segment, style::Color, Alignment, Modify, Panel, Rotate, Style, Table, Tabled,
4};
5
6// Colors
7#[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}