Skip to main content

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 frameless() -> TableTheme {
75        let hline = HorizontalLine::inherit(Style::modern().remove_left().remove_right());
76        let theme = Style::modern()
77            .remove_left()
78            .remove_right()
79            .remove_bottom()
80            .remove_top()
81            .remove_horizontal()
82            .horizontals([(1, hline)]);
83
84        Self::new(theme, Style::modern())
85    }
86
87    pub fn with_love() -> TableTheme {
88        let theme = Style::empty()
89            .top('❤')
90            .bottom('❤')
91            .vertical('❤')
92            .horizontals([(1, HorizontalLine::new('❤').intersection('❤'))]);
93
94        let full_theme = Style::empty()
95            .top('❤')
96            .bottom('❤')
97            .vertical('❤')
98            .horizontal('❤')
99            .left('❤')
100            .right('❤')
101            .intersection_top('❤')
102            .corner_top_left('❤')
103            .corner_top_right('❤')
104            .intersection_bottom('❤')
105            .corner_bottom_left('❤')
106            .corner_bottom_right('❤')
107            .intersection_right('❤')
108            .intersection_left('❤')
109            .intersection('❤');
110
111        Self::new(theme, full_theme)
112    }
113
114    pub fn compact_double() -> TableTheme {
115        let hline = HorizontalLine::inherit(Style::extended())
116            .remove_left()
117            .remove_right();
118        let theme = Style::extended()
119            .remove_left()
120            .remove_right()
121            .remove_horizontal()
122            .horizontals([(1, hline)]);
123
124        Self::new(theme, Style::extended())
125    }
126
127    pub fn rounded() -> TableTheme {
128        let full = Style::modern()
129            .corner_top_left('╭')
130            .corner_top_right('╮')
131            .corner_bottom_left('╰')
132            .corner_bottom_right('╯');
133
134        Self::new(Style::rounded(), full)
135    }
136
137    pub fn reinforced() -> TableTheme {
138        let full = Style::modern()
139            .corner_top_left('┏')
140            .corner_top_right('┓')
141            .corner_bottom_left('┗')
142            .corner_bottom_right('┛');
143
144        Self::new(full.clone().remove_horizontal(), full)
145    }
146
147    pub fn heavy() -> TableTheme {
148        let theme = Style::empty()
149            .top('━')
150            .bottom('━')
151            .vertical('┃')
152            .left('┃')
153            .right('┃')
154            .intersection_top('┳')
155            .intersection_bottom('┻')
156            .corner_top_left('┏')
157            .corner_top_right('┓')
158            .corner_bottom_left('┗')
159            .corner_bottom_right('┛')
160            .horizontals([(1, HorizontalLine::full('━', '╋', '┣', '┫'))]);
161        let full = theme
162            .clone()
163            .remove_horizontals()
164            .horizontal('━')
165            .intersection_left('┣')
166            .intersection_right('┫')
167            .intersection('╋');
168
169        Self::new(theme, full)
170    }
171
172    pub fn single() -> TableTheme {
173        let full = Style::modern()
174            .corner_top_left('┌')
175            .corner_top_right('┐')
176            .corner_bottom_left('└')
177            .corner_bottom_right('┘');
178
179        Self::new(Style::sharp(), full)
180    }
181
182    pub fn double() -> TableTheme {
183        let hline = HorizontalLine::inherit(Style::extended());
184        let theme = Style::extended()
185            .remove_horizontal()
186            .horizontals([(1, hline)]);
187
188        Self::new(theme, Style::extended())
189    }
190
191    pub fn none() -> TableTheme {
192        Self::new(Style::blank(), Style::blank())
193    }
194
195    pub fn as_full(&self) -> &Theme {
196        &self.full
197    }
198
199    pub fn as_base(&self) -> &Theme {
200        &self.base
201    }
202}