-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
224 lines (198 loc) · 6.3 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! macros for building and styling text for tui.
/// styles text into a span with the bold modifier set. The argument must evaluate to something
/// that implements [`Into<Span>`](ratatui::text::Span)
#[macro_export]
macro_rules! bold {
($e:expr) => {{
let mut s = ::ratatui::text::Span::from($e);
s.style = s.style.add_modifier(::ratatui::style::Modifier::BOLD);
s
}};
}
/// styles text into a span with the italic modifier set. The argument must evaluate to something
/// that implements [`Into<Span>`](ratatui::text::Span)
#[macro_export]
macro_rules! italic {
($e:expr) => {{
let mut s = ::ratatui::text::Span::from($e);
s.style = s.style.add_modifier(::ratatui::style::Modifier::ITALIC);
s
}};
}
/// styles text into a span with the underlined modifier set. The argument must evaluate to something
/// that implements [`Into<Span>`](ratatui::text::Span)
#[macro_export]
macro_rules! underlined {
($e:expr) => {{
let mut s = ::ratatui::text::Span::from($e);
s.style = s.style.add_modifier(::ratatui::style::Modifier::UNDERLINED);
s
}};
}
/// styles text into a span with the foreground set. The first argument must evaluate to something
/// that implements [`Into<Span>`](ratatui::text::Span), and the second a [`Color`](ratatui::style::Color)
#[macro_export]
macro_rules! fg {
($t:expr, $c: expr) => {{
let mut s = ::ratatui::text::Span::from($t);
s.style = s.style.fg($c);
s
}};
}
/// Styles text into a span with the background set. The first argument must evaluate to something
/// that implements [`Into<Span>`](ratatui::text::Span), and the second a [Color](ratatui::style::Color)
#[macro_export]
macro_rules! bg {
($t:expr, $c: expr) => {{
let mut s = ::ratatui::text::Span::from($t);
s.style = s.style.bg($c);
s
}};
}
/// Trait to allow all the overloading of the add_lines method
/// This is a helper to simplify the [text!](crate::text!) macro, and should not be used directly.
pub trait AddLines<T> {
fn add_lines(&mut self, to_add: T);
}
impl<'a> AddLines<&'a str> for ::ratatui::text::Text<'a> {
fn add_lines(&mut self, to_add: &'a str) {
self.lines.push(to_add.into());
}
}
impl<'a> AddLines<String> for ::ratatui::text::Text<'a> {
fn add_lines(&mut self, to_add: String) {
self.lines.push(to_add.into());
}
}
impl<'a> AddLines<::ratatui::text::Span<'a>> for ::ratatui::text::Text<'a> {
fn add_lines(&mut self, to_add: ::ratatui::text::Span<'a>) {
self.lines.push(to_add.into());
}
}
impl<'a> AddLines<::ratatui::text::Spans<'a>> for ::ratatui::text::Text<'a> {
fn add_lines(&mut self, to_add: ::ratatui::text::Spans<'a>) {
self.lines.push(to_add);
}
}
impl<'a> AddLines<Vec<::ratatui::text::Spans<'a>>> for ::ratatui::text::Text<'a> {
fn add_lines(&mut self, mut to_add: Vec<::ratatui::text::Spans<'a>>) {
self.lines.append(&mut to_add);
}
}
/// Create a [`Vec<Spans>`](ratatui::text::Spans) from lines of a string separated by '\n'
#[macro_export]
macro_rules! split {
($e:expr) => {{
$e.lines()
.map(|l| ::ratatui::text::Spans::from(l))
.collect::<Vec<::ratatui::text::Spans>>()
}};
}
/// Create a single [Spans](ratatui::text::Spans) from many
/// [Span](ratatui::text::Span) structs. Useful with [`text!`](crate::text!)
/// for having multiple stylings in a single line
#[macro_export]
macro_rules! line {
($($e:expr),* $(,)?) => {{
let mut res = ::ratatui::text::Spans::default();
$(res.0.push(::ratatui::text::Span::from($e));)*;
res
}};
}
/// Creates a `Vec<Spans>` from each line of the enclosed block
#[macro_export]
macro_rules! text {
($t:expr) => {
res.push(Spans::from($t));
};
($($t:expr);* $(;)?) => {{
use $crate::text_macros::AddLines;
let mut res = ::ratatui::text::Text::default();
$(res.add_lines($t);)*
res
}};
}
#[cfg(test)]
mod tests {
use ratatui::{
style::{Modifier, Style},
text::{Span, Spans, Text},
};
#[test]
fn bold() {
let expected = Span::styled("foo", Style::default().add_modifier(Modifier::BOLD));
let test = bold!("foo");
assert_eq!(expected, test);
}
#[test]
fn italic() {
let expected = Span::styled("foo", Style::default().add_modifier(Modifier::ITALIC));
let test = italic!("foo");
assert_eq!(expected, test);
}
#[test]
fn underline() {
let expected = Span::styled("foo", Style::default().add_modifier(Modifier::UNDERLINED));
let test = underlined!("foo");
assert_eq!(expected, test);
}
#[test]
fn bold_italic() {
let expected = Span::styled(
"foo",
Style::default()
.add_modifier(Modifier::BOLD)
.add_modifier(Modifier::ITALIC),
);
let test = bold!(italic!("foo"));
assert_eq!(expected, test);
}
#[test]
fn text() {
let mut expected = Text::from(vec![
Spans::from(Span::styled(
"foo",
Style::default().add_modifier(Modifier::ITALIC),
)),
Spans::from(Span::styled(
"bar",
Style::default().add_modifier(Modifier::UNDERLINED),
)),
Spans::from("baz"),
]);
let test = text! {
italic!("foo");
underlined!("bar");
"baz";
};
assert_eq!(expected, test);
let test = text! {
italic!("foo");
underlined!("bar");
"baz"
};
assert_eq!(expected, test);
let test = text! {
italic!("foo");
underlined!("bar");
"baz";
"a\nb";
split!("q\nr")
};
expected.lines.push(Spans::from("a\nb"));
expected.lines.push(Spans::from("q"));
expected.lines.push(Spans::from("r"));
assert_eq!(expected, test);
}
#[test]
fn text_single_line() {
let expected = Text::from(vec![Spans::from(Span::styled(
"foo",
Style::default().add_modifier(Modifier::ITALIC),
))]);
let test = text! {
italic!("foo");
};
assert_eq!(expected, test);
}
}