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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Apply a style to terminal text using the "Set Graphics Mode"
//! escape sequence.
//!
//! The easiest way to use this is to use the style! macro.
//!
//! ```
//! # #[macro_use]
//! use dinglebit_terminal::style;
//! fn main() {
//!     let s = style!("{}, {}!", "Hello", "world")
//!                 .black()
//!                 .underline()
//!                 .to_string();
//!     assert_eq!(s, "\x1b[30;4mHello, world!\x1b[0m");
//! }
//! ```

use crate::consts::*;
use std::fmt;

/// format! the given values and create a Style that can then be used
/// to style the text.
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # use dinglebit_terminal::style;
/// # fn main() {
///     let s = style!("{}, {}!", "Hello", "world")
///                 .black()
///                 .underline()
///                 .to_string();
///     assert_eq!(s, "\x1b[30;4mHello, world!\x1b[0m");
/// # }
/// ```
#[macro_export]
macro_rules! style {
    ($($arg:tt)*) => ($crate::style::Style::new(format!($($arg)*)))
}

/// A Stylized text that can be output to a terminal.
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # use dinglebit_terminal::style::Style;
/// # fn main() {
///     let s = Style::new(format!("{}, {}!", "Hello", "world"))
///                 .black()
///                 .underline()
///                 .to_string();
///     assert_eq!(s, "\x1b[30;4mHello, world!\x1b[0m");
/// # }
/// ```
pub struct Style {
    text: String,
    values: Vec<u8>,
}

impl Style {
    /// Create a new style whose text is the given text.
    pub fn new(text: String) -> Self {
        Self {
            text: text,
            values: Vec::new(),
        }
    }

    /// Apply a bold value to the escape sequence.
    pub fn bold(&mut self) -> &mut Self {
        self.values.push(OP_BOLD);
        self
    }

    /// Apply a faint value to the escape sequence.
    pub fn faint(&mut self) -> &mut Self {
        self.values.push(OP_FAINT);
        self
    }

    /// Apply an italic value to the escape sequence.
    pub fn italic(&mut self) -> &mut Self {
        self.values.push(OP_ITALIC);
        self
    }

    /// Apply an underline value to the escape sequence.
    pub fn underline(&mut self) -> &mut Self {
        self.values.push(OP_UNDERLINE);
        self
    }

    /// Apply a slow blink value to the escape sequence.
    pub fn slow_blink(&mut self) -> &mut Self {
        self.values.push(OP_SLOW_BLINK);
        self
    }

    /// Apply a fast blink value to the escape sequence.
    pub fn fast_blink(&mut self) -> &mut Self {
        self.values.push(OP_FAST_BLINK);
        self
    }

    /// Apply a reverse value to the escape sequence.
    pub fn reverse(&mut self) -> &mut Self {
        self.values.push(OP_REVERSE);
        self
    }

    /// Apply a conceal value to the escape sequence.
    pub fn conceal(&mut self) -> &mut Self {
        self.values.push(OP_CONCEAL);
        self
    }

    /// Apply a strikethrough value to the escape sequence.
    pub fn strikethrough(&mut self) -> &mut Self {
        self.values.push(OP_STRIKETHROUGH);
        self
    }

    /// Apply the foreground color black value to the escape sequence.
    pub fn black(&mut self) -> &mut Self {
        self.values.push(FG_BLACK);
        self
    }

    /// Apply the foreground color red value to the escape sequence.
    pub fn red(&mut self) -> &mut Self {
        self.values.push(FG_RED);
        self
    }

    /// Apply the foreground color green value to the escape sequence.
    pub fn green(&mut self) -> &mut Self {
        self.values.push(FG_GREEN);
        self
    }

    /// Apply the foreground color yellow value to the escape
    /// sequence.
    pub fn yellow(&mut self) -> &mut Self {
        self.values.push(FG_YELLOW);
        self
    }

    /// Apply the foreground color blue value to the escape sequence.
    pub fn blue(&mut self) -> &mut Self {
        self.values.push(FG_BLUE);
        self
    }

    /// Apply the foreground color magenta value to the escape
    /// sequence.
    pub fn magenta(&mut self) -> &mut Self {
        self.values.push(FG_MAGENTA);
        self
    }

    /// Apply the foreground color cyan value to the escape sequence.
    pub fn cyan(&mut self) -> &mut Self {
        self.values.push(FG_CYAN);
        self
    }

    /// Apply the foreground color white value to the escape sequence.
    pub fn white(&mut self) -> &mut Self {
        self.values.push(FG_WHITE);
        self
    }

    /// Apply the foreground color gray value to the escape sequence.
    pub fn gray(&mut self) -> &mut Self {
        self.values.push(FG_GRAY);
        self
    }

    /// Apply the foreground color bright red value to the escape
    /// sequence.
    pub fn bright_red(&mut self) -> &mut Self {
        self.values.push(FG_BRIGHT_RED);
        self
    }

    /// Apply the foreground color bright green value to the escape
    /// sequence.
    pub fn bright_green(&mut self) -> &mut Self {
        self.values.push(FG_BRIGHT_GREEN);
        self
    }

    /// Apply the foreground color bright yellow value to the escape
    /// sequence.
    pub fn bright_yellow(&mut self) -> &mut Self {
        self.values.push(FG_BRIGHT_YELLOW);
        self
    }

    /// Apply the foreground color bright blue value to the escape
    /// sequence.
    pub fn bright_blue(&mut self) -> &mut Self {
        self.values.push(FG_BRIGHT_BLUE);
        self
    }

    /// Apply the foreground color bright magenta value to the escape
    /// sequence.
    pub fn bright_magenta(&mut self) -> &mut Self {
        self.values.push(FG_BRIGHT_MAGENTA);
        self
    }

    /// Apply the foreground color bright cyan value to the escape
    /// sequence.
    pub fn bright_cyan(&mut self) -> &mut Self {
        self.values.push(FG_BRIGHT_CYAN);
        self
    }

    /// Apply the foreground color bright white value to the escape
    /// sequence.
    pub fn bright_white(&mut self) -> &mut Self {
        self.values.push(FG_BRIGHT_WHITE);
        self
    }

    /// Apply the background color black value to the escape sequence.
    pub fn bg_black(&mut self) -> &mut Self {
        self.values.push(BG_BLACK);
        self
    }

    /// Apply the background color red value to the escape sequence.
    pub fn bg_red(&mut self) -> &mut Self {
        self.values.push(BG_RED);
        self
    }

    /// Apply the background color green value to the escape sequence.
    pub fn bg_green(&mut self) -> &mut Self {
        self.values.push(BG_GREEN);
        self
    }

    /// Apply the background color yellow value to the escape
    /// sequence.
    pub fn bg_yellow(&mut self) -> &mut Self {
        self.values.push(BG_YELLOW);
        self
    }

    /// Apply the background color blue value to the escape sequence.
    pub fn bg_blue(&mut self) -> &mut Self {
        self.values.push(BG_BLUE);
        self
    }

    /// Apply the background color magenta value to the escape
    /// sequence.
    pub fn bg_magenta(&mut self) -> &mut Self {
        self.values.push(BG_MAGENTA);
        self
    }

    /// Apply the background color cyan value to the escape sequence.
    pub fn bg_cyan(&mut self) -> &mut Self {
        self.values.push(BG_CYAN);
        self
    }

    /// Apply the background color white value to the escape sequence.
    pub fn bg_white(&mut self) -> &mut Self {
        self.values.push(BG_WHITE);
        self
    }

    /// Apply the background color gray value to the escape sequence.
    pub fn bg_gray(&mut self) -> &mut Self {
        self.values.push(BG_GRAY);
        self
    }

    /// Apply the background color bright red value to the escape
    /// sequence.
    pub fn bg_bright_red(&mut self) -> &mut Self {
        self.values.push(BG_BRIGHT_RED);
        self
    }

    /// Apply the background color bright green value to the escape
    /// sequence.
    pub fn bg_bright_green(&mut self) -> &mut Self {
        self.values.push(BG_BRIGHT_GREEN);
        self
    }

    /// Apply the background color bright yellow value to the escape
    /// sequence.
    pub fn bg_bright_yellow(&mut self) -> &mut Self {
        self.values.push(BG_BRIGHT_YELLOW);
        self
    }

    /// Apply the background color bright blue value to the escape
    /// sequence.
    pub fn bg_bright_blue(&mut self) -> &mut Self {
        self.values.push(BG_BRIGHT_BLUE);
        self
    }

    /// Apply the background color bright magenta value to the escape
    /// sequence.
    pub fn bg_bright_magenta(&mut self) -> &mut Self {
        self.values.push(BG_BRIGHT_MAGENTA);
        self
    }

    /// Apply the background color bright cyan value to the escape
    /// sequence.
    pub fn bg_bright_cyan(&mut self) -> &mut Self {
        self.values.push(BG_BRIGHT_CYAN);
        self
    }

    /// Apply the background color bright white value to the escape
    /// sequence.
    pub fn bg_bright_white(&mut self) -> &mut Self {
        self.values.push(BG_BRIGHT_WHITE);
        self
    }
}

impl fmt::Display for Style {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}{}", sgm!(self.values.clone()), self.text, reset!())
    }
}

#[cfg(test)]
mod tests {
    pub use super::*;
    pub use crate::consts::*;

    macro_rules! style_test {
        ( $( $name:ident: $exp:expr ),* ) => {
            $(
                #[test]
                fn $name() {
                    let mut s = Style::new("test".to_string());
                    s.$name();
                    assert_eq!($exp, s.values);
                }
            )*
        }
    }

    style_test! {
        bold: vec![OP_BOLD],
        faint: vec![OP_FAINT],
        italic: vec![OP_ITALIC],
        underline: vec![OP_UNDERLINE],
        slow_blink: vec![OP_SLOW_BLINK],
        fast_blink: vec![OP_FAST_BLINK],
        reverse: vec![OP_REVERSE],
        conceal: vec![OP_CONCEAL],
        strikethrough: vec![OP_STRIKETHROUGH],
        black: vec![FG_BLACK],
        red: vec![FG_RED],
        green: vec![FG_GREEN],
        yellow: vec![FG_YELLOW],
        blue: vec![FG_BLUE],
        magenta: vec![FG_MAGENTA],
        cyan: vec![FG_CYAN],
        white: vec![FG_WHITE],
        gray: vec![FG_GRAY],
        bright_red: vec![FG_BRIGHT_RED],
        bright_green: vec![FG_BRIGHT_GREEN],
        bright_yellow: vec![FG_BRIGHT_YELLOW],
        bright_blue: vec![FG_BRIGHT_BLUE],
        bright_magenta: vec![FG_BRIGHT_MAGENTA],
        bright_cyan: vec![FG_BRIGHT_CYAN],
        bright_white: vec![FG_BRIGHT_WHITE],
        bg_black: vec![BG_BLACK],
        bg_red: vec![BG_RED],
        bg_green: vec![BG_GREEN],
        bg_yellow: vec![BG_YELLOW],
        bg_blue: vec![BG_BLUE],
        bg_magenta: vec![BG_MAGENTA],
        bg_cyan: vec![BG_CYAN],
        bg_white: vec![BG_WHITE],
        bg_gray: vec![BG_GRAY],
        bg_bright_red: vec![BG_BRIGHT_RED],
        bg_bright_green: vec![BG_BRIGHT_GREEN],
        bg_bright_yellow: vec![BG_BRIGHT_YELLOW],
        bg_bright_blue: vec![BG_BRIGHT_BLUE],
        bg_bright_magenta: vec![BG_BRIGHT_MAGENTA],
        bg_bright_cyan: vec![BG_BRIGHT_CYAN],
        bg_bright_white: vec![BG_BRIGHT_WHITE]
    }

    #[test]
    fn multi_style() {
        let mut s = style!("test");
        s.bold().underline().red().bg_green();
        assert_eq!(s.values, vec![OP_BOLD, OP_UNDERLINE, FG_RED, BG_GREEN]);
        assert_eq!(
            format!(
                "\x1b[{};{};{};{}mtest\x1b[0m",
                OP_BOLD, OP_UNDERLINE, FG_RED, BG_GREEN
            ),
            s.to_string()
        );
    }
}