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
use ansi_term::{ANSIString, Color, Style};
use crate::generator::{
helper::{FlattenableSpan, FlattenableStyle},
Tag, TagConvertor,
};
impl<'a, C> From<Tag<'a, C>> for Style
where
C: TagConvertor<'a, Color = Color, Modifier = Style, Custom = Style>,
{
fn from(t: Tag<'a, C>) -> Self {
match t {
Tag::Fg(c) => Style::default().fg(c),
Tag::Bg(c) => Style::default().on(c),
Tag::Modifier(style) => style,
Tag::Custom(style) => style,
}
}
}
impl FlattenableStyle for Style {
fn patch(mut self, other: Self) -> Self {
if let Some(fg) = other.foreground {
self.foreground = Some(fg);
}
if let Some(bg) = other.background {
self.background = Some(bg);
}
if other.is_bold {
self.is_bold = true;
}
if other.is_dimmed {
self.is_dimmed = true;
}
if other.is_italic {
self.is_italic = true;
}
if other.is_underline {
self.is_underline = true;
}
if other.is_reverse {
self.is_reverse = true;
}
if other.is_blink {
self.is_blink = true;
}
if other.is_hidden {
self.is_hidden = true;
}
if other.is_strikethrough {
self.is_strikethrough = true;
}
self
}
}
impl<'a> FlattenableSpan<'a, Style> for ANSIString<'a> {
fn with_style(s: &'a str, style: Option<Style>) -> Self {
style.unwrap_or_default().paint(s)
}
}