tui_markup/generator/ansi/
span.rs

1use ansi_term::{ANSIString, Color, Style};
2
3use crate::generator::{
4    helper::{FlattenableSpan, FlattenableStyle},
5    Tag, TagConvertor,
6};
7
8impl<'a, C> From<Tag<'a, C>> for Style
9where
10    C: TagConvertor<'a, Color = Color, Modifier = Style, Custom = Style>,
11{
12    fn from(t: Tag<'a, C>) -> Self {
13        match t {
14            Tag::Fg(c) => Style::default().fg(c),
15            Tag::Bg(c) => Style::default().on(c),
16            Tag::Modifier(style) | Tag::Custom(style) => style,
17        }
18    }
19}
20
21impl FlattenableStyle for Style {
22    fn patch(mut self, other: Self) -> Self {
23        if let Some(fg) = other.foreground {
24            self.foreground = Some(fg);
25        }
26        if let Some(bg) = other.background {
27            self.background = Some(bg);
28        }
29        if other.is_bold {
30            self.is_bold = true;
31        }
32        if other.is_dimmed {
33            self.is_dimmed = true;
34        }
35        if other.is_italic {
36            self.is_italic = true;
37        }
38        if other.is_underline {
39            self.is_underline = true;
40        }
41        if other.is_reverse {
42            self.is_reverse = true;
43        }
44        if other.is_blink {
45            self.is_blink = true;
46        }
47        if other.is_hidden {
48            self.is_hidden = true;
49        }
50        if other.is_strikethrough {
51            self.is_strikethrough = true;
52        }
53
54        self
55    }
56}
57
58impl<'a> FlattenableSpan<'a, Style> for ANSIString<'a> {
59    fn with_style(s: &'a str, style: Option<Style>) -> Self {
60        style.unwrap_or_default().paint(s)
61    }
62}