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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::borrow::{Borrow, Cow};
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Font {
    /// 重置样式
    Reset,
    /// 字体加粗
    Bold,
    RBold,
    /// 下划线
    Underline,
    RUnderline,
    /// 斜体
    Italic,
    RItalic,
    /// 反色(前景色背景色交换)
    Reverse,
    RReverse,
    /// 删除线
    Delete,
    RDelete,
    /// 黑色字体
    Black,
    RBlack,
    /// 红色字体
    Red,
    RRed,
    /// 绿色字体
    Green,
    RGreen,
    /// 黄色字体
    Yellow,
    RYellow,
    /// 蓝色字体
    Blue,
    RBlue,
    /// 紫色字体
    Purple,
    RPurple,
    /// 青色字体
    Cyan,
    RCyan,
    /// 灰色字体
    Grey,
    RGrey,
    /// 黑色背景
    BgBlack,
    RBgBlack,
    /// 红色背景
    BgRed,
    RBgRed,
    /// 绿色背景
    BgGreen,
    RBgGreen,
    /// 黄色背景
    BgYellow,
    RBgYellow,
    /// 蓝色背景
    BgBlue,
    RBgBlue,
    /// 紫色背景
    BgPurple,
    RBgPurple,
    /// 青色背景
    BgCyan,
    RBgCyan,
    /// 灰色背景
    BgGrey,
    RBgGrey,
    /// 字体颜色 RGB
    Color(u8, u8, u8),
    RColor(u8, u8, u8),
    /// 背景颜色 RGB
    BgColor(u8, u8, u8),
    RBgColor(u8, u8, u8),
}

impl Font {
    pub fn as_str(&self) -> Cow<'static, str> {
        match self {
            Font::Reset => Cow::Borrowed("0"),
            Font::Bold => Cow::Borrowed("1"),
            Font::RBold => Cow::Borrowed("0;1"),
            Font::Italic => Cow::Borrowed("3"),
            Font::RItalic => Cow::Borrowed("0;3"),
            Font::Underline => Cow::Borrowed("4"),
            Font::RUnderline => Cow::Borrowed("0;4"),
            Font::Reverse => Cow::Borrowed("7"),
            Font::RReverse => Cow::Borrowed("0;7"),
            Font::Delete => Cow::Borrowed("9"),
            Font::RDelete => Cow::Borrowed("0;9"),
            Font::Black => Cow::Borrowed("30"),
            Font::RBlack => Cow::Borrowed("0;30"),
            Font::Red => Cow::Borrowed("31"),
            Font::RRed => Cow::Borrowed("0;31"),
            Font::Green => Cow::Borrowed("32"),
            Font::RGreen => Cow::Borrowed("0;32"),
            Font::Yellow => Cow::Borrowed("33"),
            Font::RYellow => Cow::Borrowed("0;33"),
            Font::Blue => Cow::Borrowed("34"),
            Font::RBlue => Cow::Borrowed("0;34"),
            Font::Purple => Cow::Borrowed("35"),
            Font::RPurple => Cow::Borrowed("0;35"),
            Font::Cyan => Cow::Borrowed("36"),
            Font::RCyan => Cow::Borrowed("0;36"),
            Font::Grey => Cow::Borrowed("37"),
            Font::RGrey => Cow::Borrowed("0;37"),
            Font::BgBlack => Cow::Borrowed("40"),
            Font::RBgBlack => Cow::Borrowed("0;40"),
            Font::BgRed => Cow::Borrowed("41"),
            Font::RBgRed => Cow::Borrowed("0;41"),
            Font::BgGreen => Cow::Borrowed("42"),
            Font::RBgGreen => Cow::Borrowed("0;42"),
            Font::BgYellow => Cow::Borrowed("43"),
            Font::RBgYellow => Cow::Borrowed("0;43"),
            Font::BgBlue => Cow::Borrowed("44"),
            Font::RBgBlue => Cow::Borrowed("0;44"),
            Font::BgPurple => Cow::Borrowed("45"),
            Font::RBgPurple => Cow::Borrowed("0;45"),
            Font::BgCyan => Cow::Borrowed("46"),
            Font::RBgCyan => Cow::Borrowed("0;46"),
            Font::BgGrey => Cow::Borrowed("47"),
            Font::RBgGrey => Cow::Borrowed("0;47"),
            Font::Color(r, g, b) => Cow::Owned(format!("38;2;{};{};{}", r, g, b)),
            Font::RColor(r, g, b) => Cow::Owned(format!("0;38;2;{};{};{}", r, g, b)),
            Font::BgColor(r, g, b) => Cow::Owned(format!("48;2;{};{};{}", r, g, b)),
            Font::RBgColor(r, g, b) => Cow::Owned(format!("0;48;2;{};{};{}", r, g, b)),
        }
    }
}
impl Display for Font {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "\x1b[{}m", self.as_str())
    }
}

#[test]
fn font_is_work() {
    assert_eq!("\x1b[30m", format!("{}", Font::Black));
    assert_eq!("\x1b[38;2;1;2;3m", format!("{}", Font::Color(1, 2, 3)));
}

/// 组合多种字体
/// # Example
/// ```
/// use color_string::fonts;
/// use color_string::Font::*;
/// let fonts = fonts!(Red, Bold, Underline, BgColor(1, 2, 3));
/// assert_eq!("\x1b[31;1;4;48;2;1;2;3m", fonts)
/// ```
#[macro_export]
macro_rules! fonts {
    ($ ($font:expr),*) => {{
        let mut s = String::new();
        $crate::wf!(&mut s, $($font),*);
        s
    }};
}

/// 写入多种字体
/// # Example
/// ```
/// use color_string::wf;
/// use color_string::Font::*;
/// let mut fonts = String::new();
/// wf!(&mut fonts, Red, Bold, Underline, BgColor(1, 2, 3));
/// println!("{} hello world! {}", fonts, Reset);
/// assert_eq!("\x1b[31;1;4;48;2;1;2;3m", fonts)
/// ```
#[macro_export]
macro_rules! wf {
    ($s:expr, $($font:expr),*) => {{
        use std::fmt::Write;
        $s.push_str("\x1b[");
                $(
            let s = $font.as_str();
            if s.starts_with("\x1b[") {
                write!($s, "{};", &s[2..s.len() - 1]).unwrap();
            }else {
                write!($s, "{};", s).unwrap();
            }
        )*
        $s.pop();
        $s.push('m');
    }};
}

pub trait FontTool {
    fn reset(&mut self) -> &Self;

    fn no_reset(self) -> Self;

    fn none_font(&self) -> Self;
}

impl FontTool for String {
    fn reset(&mut self) -> &Self {
        let reset = Font::Reset.to_string();
        if !self.ends_with(&reset) {
            self.push_str(&reset)
        }
        self
    }

    fn no_reset(mut self) -> Self {
        if self.ends_with("\x1b[0m") {
            self.truncate(self.len() - 4);
        }
        self
    }

    fn none_font(&self) -> Self {
        none_font(self)
    }
}

fn none_font(s: &str) -> String {
    let mut buf = String::new();
    let mut flag = false;
    for c in s.chars() {
        match c {
            '\x1b' => flag = true,
            'm' if flag => flag = false,
            _ if !flag => buf.push(c),
            _ => {}
        }
    }
    buf
}

#[test]
fn aaa() {
    use crate::cs;
    use crate::Colored;
    let s = cs!(Font::Red => "hello");
    println!("{s}");
    println!("{}", s.none_font());
    println!("{}456", "123".red().no_reset());
}