nu_table/
table_theme.rs

1use tabled::settings::{
2    style::{HorizontalLine, Style},
3    themes::Theme,
4};
5
6#[derive(Debug, Clone)]
7pub struct TableTheme {
8    base: Theme,
9    full: Theme,
10}
11
12impl TableTheme {
13    fn new(base: impl Into<Theme>, full: impl Into<Theme>) -> Self {
14        Self {
15            base: base.into(),
16            full: full.into(),
17        }
18    }
19
20    pub fn basic() -> TableTheme {
21        Self::new(Style::ascii(), Style::ascii())
22    }
23
24    pub fn thin() -> TableTheme {
25        Self::new(Style::modern(), Style::modern())
26    }
27
28    pub fn light() -> TableTheme {
29        let mut theme = Theme::from_style(Style::blank());
30        theme.insert_horizontal_line(1, HorizontalLine::new('─').intersection('─'));
31
32        Self::new(theme, Style::modern())
33    }
34
35    pub fn psql() -> TableTheme {
36        Self::new(Style::psql(), Style::psql())
37    }
38
39    pub fn markdown() -> TableTheme {
40        Self::new(Style::markdown(), Style::markdown())
41    }
42
43    pub fn dots() -> TableTheme {
44        let theme = Style::dots().remove_horizontal();
45
46        Self::new(theme, Style::dots())
47    }
48
49    pub fn restructured() -> TableTheme {
50        Self::new(Style::re_structured_text(), Style::re_structured_text())
51    }
52
53    pub fn ascii_rounded() -> TableTheme {
54        Self::new(Style::ascii_rounded(), Style::ascii_rounded())
55    }
56
57    pub fn basic_compact() -> TableTheme {
58        let theme = Style::ascii().remove_horizontal();
59
60        Self::new(theme, Style::ascii())
61    }
62
63    pub fn compact() -> TableTheme {
64        let hline = HorizontalLine::inherit(Style::modern().remove_left().remove_right());
65        let theme = Style::modern()
66            .remove_left()
67            .remove_right()
68            .remove_horizontal()
69            .horizontals([(1, hline)]);
70
71        Self::new(theme, Style::modern())
72    }
73
74    pub fn with_love() -> TableTheme {
75        let theme = Style::empty()
76            .top('❤')
77            .bottom('❤')
78            .vertical('❤')
79            .horizontals([(1, HorizontalLine::new('❤').intersection('❤'))]);
80
81        let full_theme = Style::empty()
82            .top('❤')
83            .bottom('❤')
84            .vertical('❤')
85            .horizontal('❤')
86            .left('❤')
87            .right('❤')
88            .intersection_top('❤')
89            .corner_top_left('❤')
90            .corner_top_right('❤')
91            .intersection_bottom('❤')
92            .corner_bottom_left('❤')
93            .corner_bottom_right('❤')
94            .intersection_right('❤')
95            .intersection_left('❤')
96            .intersection('❤');
97
98        Self::new(theme, full_theme)
99    }
100
101    pub fn compact_double() -> TableTheme {
102        let hline = HorizontalLine::inherit(Style::extended())
103            .remove_left()
104            .remove_right();
105        let theme = Style::extended()
106            .remove_left()
107            .remove_right()
108            .remove_horizontal()
109            .horizontals([(1, hline)]);
110
111        Self::new(theme, Style::extended())
112    }
113
114    pub fn rounded() -> TableTheme {
115        let full = Style::modern()
116            .corner_top_left('╭')
117            .corner_top_right('╮')
118            .corner_bottom_left('╰')
119            .corner_bottom_right('╯');
120
121        Self::new(Style::rounded(), full)
122    }
123
124    pub fn reinforced() -> TableTheme {
125        let full = Style::modern()
126            .corner_top_left('┏')
127            .corner_top_right('┓')
128            .corner_bottom_left('┗')
129            .corner_bottom_right('┛');
130
131        Self::new(full.clone().remove_horizontal(), full)
132    }
133
134    pub fn heavy() -> TableTheme {
135        let theme = Style::empty()
136            .top('━')
137            .bottom('━')
138            .vertical('┃')
139            .left('┃')
140            .right('┃')
141            .intersection_top('┳')
142            .intersection_bottom('┻')
143            .corner_top_left('┏')
144            .corner_top_right('┓')
145            .corner_bottom_left('┗')
146            .corner_bottom_right('┛')
147            .horizontals([(1, HorizontalLine::full('━', '╋', '┣', '┫'))]);
148        let full = theme
149            .clone()
150            .remove_horizontals()
151            .horizontal('━')
152            .intersection_left('┣')
153            .intersection_right('┫')
154            .intersection('╋');
155
156        Self::new(theme, full)
157    }
158
159    pub fn single() -> TableTheme {
160        let full = Style::modern()
161            .corner_top_left('┌')
162            .corner_top_right('┐')
163            .corner_bottom_left('└')
164            .corner_bottom_right('┘');
165
166        Self::new(Style::sharp(), full)
167    }
168
169    pub fn none() -> TableTheme {
170        Self::new(Style::blank(), Style::blank())
171    }
172
173    pub fn as_full(&self) -> &Theme {
174        &self.full
175    }
176
177    pub fn as_base(&self) -> &Theme {
178        &self.base
179    }
180}