tiny_table/table/
style.rs1use crate::color::Color;
2
3use super::ANSI_RESET;
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub enum StyleAction {
10 Color(Color),
12 OnColor(Color),
14 Clear,
16 Normal,
18 Bold,
20 Dimmed,
22 Italic,
24 Underline,
26 Blink,
28 Reversed,
30 Hidden,
32 Strikethrough,
34}
35
36#[macro_export]
55macro_rules! impl_style_methods {
56 ($ty:ty, $push:expr) => {
57 impl $ty {
58 fn push_style(self, action: $crate::table::style::StyleAction) -> Self {
59 ($push)(self, action)
60 }
61
62 pub fn color(self, color: $crate::color::Color) -> Self {
64 self.push_style($crate::table::style::StyleAction::Color(color))
65 }
66
67 pub fn on_color(self, color: $crate::color::Color) -> Self {
69 self.push_style($crate::table::style::StyleAction::OnColor(color))
70 }
71
72 pub fn clear(self) -> Self {
74 self.push_style($crate::table::style::StyleAction::Clear)
75 }
76
77 pub fn normal(self) -> Self {
79 self.push_style($crate::table::style::StyleAction::Normal)
80 }
81
82 pub fn bold(self) -> Self {
84 self.push_style($crate::table::style::StyleAction::Bold)
85 }
86
87 pub fn dimmed(self) -> Self {
89 self.push_style($crate::table::style::StyleAction::Dimmed)
90 }
91
92 pub fn italic(self) -> Self {
94 self.push_style($crate::table::style::StyleAction::Italic)
95 }
96
97 pub fn underline(self) -> Self {
99 self.push_style($crate::table::style::StyleAction::Underline)
100 }
101
102 pub fn blink(self) -> Self {
104 self.push_style($crate::table::style::StyleAction::Blink)
105 }
106
107 pub fn reversed(self) -> Self {
109 self.push_style($crate::table::style::StyleAction::Reversed)
110 }
111
112 pub fn hidden(self) -> Self {
114 self.push_style($crate::table::style::StyleAction::Hidden)
115 }
116
117 pub fn strikethrough(self) -> Self {
119 self.push_style($crate::table::style::StyleAction::Strikethrough)
120 }
121
122 pub fn black(self) -> Self {
124 self.color($crate::color::Color::Black)
125 }
126
127 pub fn red(self) -> Self {
129 self.color($crate::color::Color::Red)
130 }
131
132 pub fn green(self) -> Self {
134 self.color($crate::color::Color::Green)
135 }
136
137 pub fn yellow(self) -> Self {
139 self.color($crate::color::Color::Yellow)
140 }
141
142 pub fn blue(self) -> Self {
144 self.color($crate::color::Color::Blue)
145 }
146
147 pub fn magenta(self) -> Self {
149 self.color($crate::color::Color::Magenta)
150 }
151
152 pub fn cyan(self) -> Self {
154 self.color($crate::color::Color::Cyan)
155 }
156
157 pub fn white(self) -> Self {
159 self.color($crate::color::Color::White)
160 }
161
162 pub fn bright_black(self) -> Self {
164 self.color($crate::color::Color::BrightBlack)
165 }
166
167 pub fn bright_red(self) -> Self {
169 self.color($crate::color::Color::BrightRed)
170 }
171
172 pub fn bright_green(self) -> Self {
174 self.color($crate::color::Color::BrightGreen)
175 }
176
177 pub fn bright_yellow(self) -> Self {
179 self.color($crate::color::Color::BrightYellow)
180 }
181
182 pub fn bright_blue(self) -> Self {
184 self.color($crate::color::Color::BrightBlue)
185 }
186
187 pub fn bright_magenta(self) -> Self {
189 self.color($crate::color::Color::BrightMagenta)
190 }
191
192 pub fn bright_cyan(self) -> Self {
194 self.color($crate::color::Color::BrightCyan)
195 }
196
197 pub fn bright_white(self) -> Self {
199 self.color($crate::color::Color::BrightWhite)
200 }
201
202 pub fn purple(self) -> Self {
204 self.color($crate::color::Color::Magenta)
205 }
206
207 pub fn bright_purple(self) -> Self {
209 self.color($crate::color::Color::BrightMagenta)
210 }
211
212 pub fn ansi_color(self, color: impl Into<u8>) -> Self {
214 self.push_style($crate::table::style::StyleAction::Color(
215 $crate::color::Color::AnsiColor(color.into()),
216 ))
217 }
218
219 pub fn truecolor(self, red: u8, green: u8, blue: u8) -> Self {
221 self.push_style($crate::table::style::StyleAction::Color(
222 $crate::color::Color::TrueColor {
223 r: red,
224 g: green,
225 b: blue,
226 },
227 ))
228 }
229
230 pub fn on_black(self) -> Self {
232 self.push_style($crate::table::style::StyleAction::OnColor(
233 $crate::color::Color::Black,
234 ))
235 }
236
237 pub fn on_red(self) -> Self {
239 self.push_style($crate::table::style::StyleAction::OnColor(
240 $crate::color::Color::Red,
241 ))
242 }
243
244 pub fn on_green(self) -> Self {
246 self.push_style($crate::table::style::StyleAction::OnColor(
247 $crate::color::Color::Green,
248 ))
249 }
250
251 pub fn on_yellow(self) -> Self {
253 self.push_style($crate::table::style::StyleAction::OnColor(
254 $crate::color::Color::Yellow,
255 ))
256 }
257
258 pub fn on_blue(self) -> Self {
260 self.push_style($crate::table::style::StyleAction::OnColor(
261 $crate::color::Color::Blue,
262 ))
263 }
264
265 pub fn on_magenta(self) -> Self {
267 self.push_style($crate::table::style::StyleAction::OnColor(
268 $crate::color::Color::Magenta,
269 ))
270 }
271
272 pub fn on_cyan(self) -> Self {
274 self.push_style($crate::table::style::StyleAction::OnColor(
275 $crate::color::Color::Cyan,
276 ))
277 }
278
279 pub fn on_white(self) -> Self {
281 self.push_style($crate::table::style::StyleAction::OnColor(
282 $crate::color::Color::White,
283 ))
284 }
285
286 pub fn on_bright_black(self) -> Self {
288 self.push_style($crate::table::style::StyleAction::OnColor(
289 $crate::color::Color::BrightBlack,
290 ))
291 }
292
293 pub fn on_bright_red(self) -> Self {
295 self.push_style($crate::table::style::StyleAction::OnColor(
296 $crate::color::Color::BrightRed,
297 ))
298 }
299
300 pub fn on_bright_green(self) -> Self {
302 self.push_style($crate::table::style::StyleAction::OnColor(
303 $crate::color::Color::BrightGreen,
304 ))
305 }
306
307 pub fn on_bright_yellow(self) -> Self {
309 self.push_style($crate::table::style::StyleAction::OnColor(
310 $crate::color::Color::BrightYellow,
311 ))
312 }
313
314 pub fn on_bright_blue(self) -> Self {
316 self.push_style($crate::table::style::StyleAction::OnColor(
317 $crate::color::Color::BrightBlue,
318 ))
319 }
320
321 pub fn on_bright_magenta(self) -> Self {
323 self.push_style($crate::table::style::StyleAction::OnColor(
324 $crate::color::Color::BrightMagenta,
325 ))
326 }
327
328 pub fn on_bright_cyan(self) -> Self {
330 self.push_style($crate::table::style::StyleAction::OnColor(
331 $crate::color::Color::BrightCyan,
332 ))
333 }
334
335 pub fn on_bright_white(self) -> Self {
337 self.push_style($crate::table::style::StyleAction::OnColor(
338 $crate::color::Color::BrightWhite,
339 ))
340 }
341
342 pub fn custom_color(self, color: impl Into<$crate::color::CustomColor>) -> Self {
344 let color = color.into();
345 self.push_style($crate::table::style::StyleAction::Color(
346 $crate::color::Color::TrueColor {
347 r: color.r,
348 g: color.g,
349 b: color.b,
350 },
351 ))
352 }
353
354 pub fn on_custom_color(self, color: impl Into<$crate::color::CustomColor>) -> Self {
356 let color = color.into();
357 self.push_style($crate::table::style::StyleAction::OnColor(
358 $crate::color::Color::TrueColor {
359 r: color.r,
360 g: color.g,
361 b: color.b,
362 },
363 ))
364 }
365
366 pub fn on_ansi_color(self, color: impl Into<u8>) -> Self {
368 self.push_style($crate::table::style::StyleAction::OnColor(
369 $crate::color::Color::AnsiColor(color.into()),
370 ))
371 }
372
373 pub fn on_truecolor(self, red: u8, green: u8, blue: u8) -> Self {
375 self.push_style($crate::table::style::StyleAction::OnColor(
376 $crate::color::Color::TrueColor {
377 r: red,
378 g: green,
379 b: blue,
380 },
381 ))
382 }
383 }
384 };
385}
386
387pub fn apply_style_actions(content: &str, actions: &[StyleAction]) -> String {
401 if content.is_empty() || actions.is_empty() {
402 return content.to_string();
403 }
404
405 let mut prefix = String::new();
406 let mut has_style = false;
407
408 for action in actions {
409 match action {
410 StyleAction::Color(color) => {
411 prefix.push_str(&color.ansi_fg());
412 has_style = true;
413 }
414 StyleAction::OnColor(color) => {
415 prefix.push_str(&color.ansi_bg());
416 has_style = true;
417 }
418 StyleAction::Clear | StyleAction::Normal => {
419 prefix.clear();
420 has_style = false;
421 }
422 StyleAction::Bold => {
423 prefix.push_str("\x1b[1m");
424 has_style = true;
425 }
426 StyleAction::Dimmed => {
427 prefix.push_str("\x1b[2m");
428 has_style = true;
429 }
430 StyleAction::Italic => {
431 prefix.push_str("\x1b[3m");
432 has_style = true;
433 }
434 StyleAction::Underline => {
435 prefix.push_str("\x1b[4m");
436 has_style = true;
437 }
438 StyleAction::Blink => {
439 prefix.push_str("\x1b[5m");
440 has_style = true;
441 }
442 StyleAction::Reversed => {
443 prefix.push_str("\x1b[7m");
444 has_style = true;
445 }
446 StyleAction::Hidden => {
447 prefix.push_str("\x1b[8m");
448 has_style = true;
449 }
450 StyleAction::Strikethrough => {
451 prefix.push_str("\x1b[9m");
452 has_style = true;
453 }
454 }
455 }
456
457 if !has_style {
458 return content.to_string();
459 }
460
461 format!("{}{}{}", prefix, content, ANSI_RESET)
462}