tv_cli/table/
style.rs

1use super::cell::Align;
2
3#[derive(Debug, Clone, Copy)]
4pub enum Style {
5    Plain,
6    Ascii,
7    Sharp,
8    Rounded,
9    Markdown,
10}
11
12impl Style {
13    pub fn new(s: String) -> Self {
14        match s.to_lowercase().as_str() {
15            "plane" => Self::Plain, // deprecated
16            "plain" => Self::Plain,
17            "ascii" => Self::Ascii,
18            "sharp" => Self::Sharp,
19            "rounded" => Self::Rounded,
20            "markdown" => Self::Markdown,
21            _ => Self::Ascii,
22        }
23    }
24}
25
26pub struct Frame {
27    pub has_cover: bool,
28    pub border: String,
29    pub separator: String,
30    pub center: String,
31    pub top: String,
32    pub left: String,
33    pub bottom: String,
34    pub right: String,
35    pub top_left: String,
36    pub top_right: String,
37    pub bottom_left: String,
38    pub bottom_right: String,
39}
40
41impl Frame {
42    pub fn align_border(&self, align: &Align, width: usize) -> String {
43        match align {
44            Align::None => self.border.repeat(width),
45            Align::Left => format!(":{}", self.border.repeat(width - 1)),
46            Align::Center => format!(":{}:", self.border.repeat(width - 2)),
47            Align::Right => format!("{}:", self.border.repeat(width - 1)),
48        }
49    }
50}
51
52impl From<Style> for Frame {
53    fn from(style: Style) -> Self {
54        match style {
55            Style::Plain => Self {
56                has_cover: false,
57                border: "".into(),
58                separator: "\t".into(),
59                center: "".into(),
60                top: "".into(),
61                left: "".into(),
62                bottom: "".into(),
63                right: "".into(),
64                top_left: "".into(),
65                top_right: "".into(),
66                bottom_left: "".into(),
67                bottom_right: "".into(),
68            },
69            Style::Ascii => Self {
70                has_cover: true,
71                border: "-".into(),
72                separator: "|".into(),
73                center: "+".into(),
74                top: "+".into(),
75                left: "+".into(),
76                bottom: "+".into(),
77                right: "+".into(),
78                top_left: "+".into(),
79                top_right: "+".into(),
80                bottom_left: "+".into(),
81                bottom_right: "+".into(),
82            },
83            Style::Sharp => Self {
84                has_cover: true,
85                border: "─".into(),
86                separator: "│".into(),
87                center: "┼".into(),
88                top: "┬".into(),
89                left: "├".into(),
90                bottom: "┴".into(),
91                right: "┤".into(),
92                top_left: "┌".into(),
93                top_right: "┐".into(),
94                bottom_left: "└".into(),
95                bottom_right: "┘".into(),
96            },
97            Style::Rounded => Self {
98                has_cover: true,
99                border: "─".into(),
100                separator: "│".into(),
101                center: "┼".into(),
102                top: "┬".into(),
103                left: "├".into(),
104                bottom: "┴".into(),
105                right: "┤".into(),
106                top_left: "╭".into(),
107                top_right: "╮".into(),
108                bottom_left: "╰".into(),
109                bottom_right: "╯".into(),
110            },
111            Style::Markdown => Self {
112                has_cover: false,
113                border: "-".into(),
114                separator: "|".into(),
115                center: "|".into(),
116                top: "".into(),
117                left: "|".into(),
118                bottom: "".into(),
119                right: "|".into(),
120                top_left: "".into(),
121                top_right: "".into(),
122                bottom_left: "".into(),
123                bottom_right: "".into(),
124            },
125        }
126    }
127}