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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::borrow::Cow;

pub const ATTR_RESET: &str = "\x1b[0m";
pub const BOLD: &str = "\x1b[1m";
pub const UNDERLINE: &str = "\x1b[4m";
pub const REVERSED: &str = "\x1b[7m";

// Escape sequences change the color of the terminal
pub const RESET: &str = "\x1b[m";
pub const BLACK: &str = "\x1b[30m";
pub const BLUE: &str = "\x1b[94m";
pub const CYAN: &str = "\x1b[96m";
pub const GRAY: &str = "\x1b[37m";
pub const GREEN: &str = "\x1b[92m";
pub const MAGENTA: &str = "\x1b[95m";
pub const RED: &str = "\x1b[91m";
pub const WHITE: &str = "\x1b[97m";
pub const YELLOW: &str = "\x1b[93m";
// custom colors when use `pretty`
pub const CUSTOM_RED: &str = "\x1b[38;2;255;76;76m";
pub const CUSTOM_BLUE: &str = "\x1b[38;2;76;76;255m";
pub const CUSTOM_GRAY: &str = "\x1b[38;2;231;231;235m";
pub const CUSTOM_CYAN: &str = "\x1b[38;2;76;255;255m";
pub const CUSTOM_MAGENTA: &str = "\x1b[38;2;165;76;255m";
pub const CUSTOM_GREEN: &str = "\x1b[38;2;76;255;76m";
pub const CUSTOM_YELLOW: &str = "\x1b[38;2;255;255;76m";

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
pub enum Color {
    Reset,
    Black,
    Blue,
    Cyan,
    Gray,
    Green,
    Magenta,
    Red,
    White,
    Yellow,
    CustomRed,
    CustomBlue,
    CustomGray,
    CustomCyan,
    CustomMagenta,
    CustomGreen,
    CustomYellow,
}

impl Color {
    pub fn as_str(&self) -> &'static str {
        match self {
            Color::Reset => RESET,
            Color::Black => BLACK,
            Color::Blue => BLUE,
            Color::Cyan => CYAN,
            Color::Gray => GRAY,
            Color::Green => GREEN,
            Color::Magenta => MAGENTA,
            Color::Red => RED,
            Color::Yellow => YELLOW,
            Color::White => WHITE,
            Color::CustomRed => CUSTOM_RED,
            Color::CustomBlue => CUSTOM_BLUE,
            Color::CustomGray => CUSTOM_GRAY,
            Color::CustomCyan => CUSTOM_CYAN,
            Color::CustomMagenta => CUSTOM_MAGENTA,
            Color::CustomGreen => CUSTOM_GREEN,
            Color::CustomYellow => CUSTOM_YELLOW,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
pub enum Attribute {
    Reset,
    Underline,
    Bold,
    Reversed,
}

impl Attribute {
    pub fn as_str(&self) -> &'static str {
        match self {
            Attribute::Reset => ATTR_RESET,
            Attribute::Underline => UNDERLINE,
            Attribute::Bold => BOLD,
            Attribute::Reversed => REVERSED,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ThemeColors {
    pub error: Color,
    pub warning: Color,
    pub exception: Color,
    pub gutter: Color,
    pub hint: Color,
    pub accent: Color,
}

#[cfg(not(feature = "pretty"))]
pub const COLORS: ThemeColors = ThemeColors {
    error: Color::Red,
    warning: Color::Yellow,
    exception: Color::Magenta,
    gutter: Color::Cyan,
    hint: Color::Green,
    accent: Color::White,
};

#[cfg(feature = "pretty")]
pub const COLORS: ThemeColors = ThemeColors {
    error: Color::CustomRed,
    warning: Color::CustomYellow,
    exception: Color::CustomMagenta,
    gutter: Color::CustomCyan,
    hint: Color::CustomGreen,
    accent: Color::CustomGray,
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Characters {
    hat: char,    // error
    wave: char,   // exception
    line: char,   // warning and left bottom line
    vbar: char,   // gutter separator
    lbot: char,   // left bottom curve
    vbreak: char, // gutter omission
    lbrac: char,  // error kind modifier left bracket
    rbrac: char,  // error kind modifier right bracket
}

impl Characters {
    pub fn mark(&self, kind: &str) -> String {
        let mark = match kind {
            "Error" => self.hat,
            "Warning" => self.line,
            "Exception" => self.wave,
            invalid => panic!("In Characters, Invalid parameter: {invalid}"),
        };
        mark.to_string()
    }

    pub fn gutters(&self) -> (char, char) {
        (self.vbreak, self.vbar)
    }

    // "`- "
    #[cfg(not(feature = "unicode"))]
    pub fn left_bottom_line(&self) -> String {
        format!("{}{} ", self.lbot, self.line)
    }

    // `╰─ `
    #[cfg(feature = "unicode")]
    pub fn left_bottom_line(&self) -> String {
        format!("{}{} ", self.lbot, self.line)
    }

    // "|- "
    #[cfg(not(feature = "unicode"))]
    pub fn left_cross(&self) -> String {
        format!("{}{} ", self.vbar, self.line)
    }

    // "│─ "
    #[cfg(feature = "unicode")]
    pub fn left_cross(&self) -> String {
        format!("{}{} ", self.vbar, self.line)
    }

    // kind[padded error number]
    #[cfg(not(feature = "pretty"))]
    pub fn error_kind_format(&self, kind: &str, err_num: usize) -> String {
        const PADDING: usize = 4;
        format!("{kind}{}#{err_num:>0PADDING$}{}", self.lbrac, self.rbrac,)
    }

    #[cfg(feature = "pretty")]
    pub fn error_kind_format(&self, kind: &str, err_num: usize) -> String {
        const PADDING: usize = 4;
        let emoji = if kind == "Error" {
            "🚫"
        } else if kind == "Warning" {
            "⚠"
        } else {
            "😱"
        };
        format!(
            "{emoji} {kind}{}#{err_num:>0PADDING$}{}",
            self.lbrac, self.rbrac,
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Theme {
    pub colors: ThemeColors,
    pub characters: Characters,
}

impl Theme {
    pub const fn characters(&self) -> (Color, &Characters) {
        (self.colors.gutter, &self.characters)
    }

    pub const fn error(&self) -> (Color, char) {
        (self.colors.error, self.characters.hat)
    }

    pub const fn warning(&self) -> (Color, char) {
        (self.colors.warning, self.characters.line)
    }

    pub const fn exception(&self) -> (Color, char) {
        (self.colors.exception, self.characters.wave)
    }

    pub const fn hint(&self) -> (Color, char) {
        (self.colors.hint, self.characters.wave)
    }
}

pub const THEME: Theme = Theme {
    colors: COLORS,
    characters: CHARS,
};

#[cfg(not(feature = "unicode"))]
pub const CHARS: Characters = Characters {
    hat: '-',
    line: '-',
    vbar: '|',
    wave: '~',
    lbot: '`',
    vbreak: ':',
    lbrac: '[',
    rbrac: ']',
};

#[cfg(feature = "unicode")]
pub const CHARS: Characters = Characters {
    hat: '-',
    line: '─',
    vbar: '│',
    wave: '~',
    lbot: '╰',
    vbreak: '·',
    lbrac: '[',
    rbrac: ']',
};

///
/// `StyledStr` is for const color and attribute &str.
/// It is an immutable string.
/// # Example
/// ```
/// # use erg_common::style::{Color, Attribute, StyledStr};
/// const URL: StyledStr = StyledStr::new(
///    "https://github.com/erg-lang/erg",
///    Some(Color::White),
///    Some(Attribute::Underline),
/// );
/// ```
#[derive(Debug)]
pub struct StyledStr<'a> {
    text: &'a str,
    color: Option<Color>,
    attribute: Option<Attribute>,
}

impl<'a> StyledStr<'a> {
    pub const fn new(text: &'a str, color: Option<Color>, attribute: Option<Attribute>) -> Self {
        Self {
            text,
            color,
            attribute,
        }
    }
}

impl std::fmt::Display for StyledStr<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (self.color, self.attribute) {
            (None, None) => todo!(),
            (None, Some(attr)) => write!(f, "{}{}{}", attr.as_str(), self.text, ATTR_RESET),
            (Some(color), None) => write!(f, "{}{}{}", color.as_str(), self.text, RESET),
            (Some(color), Some(attr)) => {
                write!(
                    f,
                    "{}{}{}{}{}",
                    color.as_str(),
                    attr.as_str(),
                    self.text,
                    RESET,
                    ATTR_RESET
                )
            }
        }
    }
}

///
/// `StyledString` is for coloring and attribute text.
/// String, Color(&str) and Attribute(&str)
///
#[derive(Debug)]
pub struct StyledString {
    text: String,
    color: Option<Color>,
    attribute: Option<Attribute>,
}

impl StyledString {
    ///
    /// # Example
    /// ```
    /// let s = String::from("Hello, world");
    /// StyledString::new(s, None, None);
    /// let s = "Hello, world";
    /// StyledString::new(s, None, None);
    /// ```
    pub fn new<'a, S: Into<Cow<'a, str>>>(
        s: S,
        color: Option<Color>,
        attribute: Option<Attribute>,
    ) -> Self {
        let text: Cow<'a, str> = s.into();
        Self {
            text: text.into_owned(),
            color,
            attribute,
        }
    }

    ///
    /// Methods for pushing additional &str for strings that already have attributes or colors.
    ///
    /// # Example
    /// ```
    /// # use erg_common::style::{Color, Attribute, StyledString};
    /// let mut text = StyledString::new("sample text", None, Some(Attribute::Underline));
    /// text.push_str("\n");
    /// text.push_str("Next break line text");
    /// println!("{text}"); // Two lines of text underlined are displayed
    /// ```
    pub fn push_str(&mut self, s: &str) {
        self.text.push_str(s)
    }

    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }
}

impl std::fmt::Display for StyledString {
    fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
        match (self.color, self.attribute) {
            (None, None) => write!(f, "{}", self.text),
            (None, Some(attr)) => write!(f, "{}{}{}", attr.as_str(), self.text, ATTR_RESET),
            (Some(color), None) => write!(f, "{}{}{}", color.as_str(), self.text, RESET),
            (Some(color), Some(attr)) => write!(
                f,
                "{}{}{}{}{}",
                attr.as_str(),
                color.as_str(),
                self.text,
                RESET,
                ATTR_RESET
            ),
        }
    }
}

///
/// `StyledStrings` is vector of `StyledString` and almost the same as Vec\<String\>.
/// It is possible to change the color and attribute of each String.
/// That's why, if you don't change any color or attribute, you should use 'StyledString' not `StyledStrings`
///
/// # Example
/// ```
/// # use erg_common::style::{Color, Attribute, StyledStrings};
/// let mut texts = StyledStrings::default();
/// texts.push_str("Default color is gray, ");
/// texts.push_str_with_color("and it is possible to color text.\n", Color::Red);
/// texts.push_str("Basically, this `StyledStrings` is one sentence, ");
/// texts.push_str_with_color("so if you want to multiline sentences, you need to add `\n`.", Color::Magenta);
/// println!("{}", texts); // Pushed colored text are displayed
/// ```
/// Basically,initialize by default with mutable.
/// Then, &str(s) are pushed to the Vec, specifying colors or attributes.
///
#[derive(Debug, Default)]
pub struct StyledStrings {
    texts: Vec<StyledString>,
}

impl StyledStrings {
    ///
    /// It is possible push &str type with gray color to Vector.
    ///
    ///  # Example
    /// ```
    /// # use erg_common::style::StyledStrings;
    /// let mut texts = StyledStrings::default();
    /// texts.push_str("sample text");
    /// texts.push_str("\n");
    /// texts.push_str("new text here");
    /// println!("{}", texts);
    ///  /*
    ///     sample text
    ///     new text here
    ///  */
    ///
    /// ```
    pub fn push_str(&mut self, s: &str) {
        if self.is_same_color(Color::Gray) {
            self.texts.last_mut().unwrap().text.push_str(s);
        } else {
            self.texts.push(StyledString::new(s, None, None));
        }
    }

    ///
    /// It is possible to push &str type with specify color to Vector.
    ///
    /// # Example
    /// ```
    /// # use erg_common::style::{Color, Attribute, StyledStrings};
    /// let mut texts = StyledStrings::default();
    /// texts.push_str_with_color("Cyan color text", Color::Cyan);
    /// texts.push_str_with_color("Red color text", Color::Red);
    /// texts.push_str_with_color(", pushed texts become a single String.", Color::Yellow);
    /// texts.push_str_with_color("\n If you want to add break lines, you should add `\n`.", Color::Magenta);
    /// println!("{}", texts);
    /// ```
    pub fn push_str_with_color<'a, S: Into<Cow<'a, str>>>(&mut self, s: S, color: Color) {
        if self.is_same_color(color) {
            let text = s.into();
            self.texts.last_mut().unwrap().text.push_str(&text);
        } else {
            self.texts.push(StyledString::new(s, Some(color), None));
        }
    }

    ///
    /// Text can be pushed color and attribute to Vector.
    /// When color or attribute are different, it will be pushed as different String.
    ///
    /// # Example
    /// ```
    /// # use erg_common::style::{Color, Attribute, StyledStrings};
    /// let mut texts = StyledStrings::default();
    /// texts.push_str_with_color_and_attribute("Magenta and bold text\n", Color::Magenta, Attribute::Bold);
    /// texts.push_str_with_color_and_attribute("White and underlined text", Color::White, Attribute::Underline);
    /// // texts.push_str_with_color_and_attribute("Must be specify the color and attribute", None, Attribute::Underline);
    /// println!("{}", texts);
    /// ```
    pub fn push_str_with_color_and_attribute<'a, S: Into<Cow<'a, str>>>(
        &mut self,
        s: S,
        color: Color,
        attr: Attribute,
    ) {
        if self.is_same_color(color) && self.is_same_attribute(attr) {
            let text = s.into();
            self.texts.last_mut().unwrap().text.push_str(&text);
        } else {
            self.texts
                .push(StyledString::new(s, Some(color), Some(attr)));
        }
    }

    ///
    /// Determine if all strings in Vec are empty
    /// Returns False if any string is present.
    ///
    pub fn is_empty(&self) -> bool {
        self.texts.iter().all(|s| s.is_empty())
    }

    fn is_same_color(&self, color: Color) -> bool {
        if let Some(text) = self.texts.last() {
            return text.color == Some(color);
        }
        false
    }

    fn is_same_attribute(&self, attr: Attribute) -> bool {
        if let Some(text) = self.texts.last() {
            if let Some(text_attr) = text.attribute {
                return text_attr == attr;
            }
        }
        false
    }
}

impl std::fmt::Display for StyledStrings {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        for text in self.texts.iter() {
            write!(f, "{}", text)?;
        }
        Ok(())
    }
}

impl From<StyledStrings> for String {
    fn from(s: StyledStrings) -> Self {
        s.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn text_fg_colorings() {
        println!("{YELLOW}Hello{RESET}, {RED}World{RESET}");
        println!("{BLUE}Hello{RESET}, {GREEN}World{RESET}");
        println!("{MAGENTA}Hello{RESET}, {BLACK}World{RESET}");
        println!("{GRAY}Hello{RESET}, {WHITE}World{RESET}");
        println!("{CUSTOM_BLUE}Hello{RESET}, {CUSTOM_CYAN}World{RESET}");
        println!("{CUSTOM_GRAY}Hello{RESET}, {CUSTOM_GREEN}World{RESET}");
        println!("{CUSTOM_MAGENTA}Hello{RESET}, {CUSTOM_RED}World{RESET}");
    }

    #[test]
    fn text_attribute() {
        println!("{BOLD}BOLD{ATTR_RESET}");
        println!("{UNDERLINE}UNDERLINED{ATTR_RESET}");
        println!("{REVERSED}REVERSED{ATTR_RESET}")
    }

    #[test]
    fn str_texts_test() {
        let mut texts = StyledStrings::default();
        texts.push_str("Gray is the default color\n");
        texts.push_str("If you specify the color, ");
        texts.push_str("you should use `push_str_with_color()`\n");

        texts.push_str_with_color(
            "It is possible to change text foreground color...\n",
            Color::White,
        );
        texts.push_str_with_color("Cyan text, ", Color::Cyan);
        texts.push_str_with_color("Black text, ", Color::Black);
        texts.push_str_with_color("Blue text, ", Color::Blue);
        texts.push_str_with_color("Red text, ", Color::Red);
        texts.push_str_with_color("pushed texts become a String.", Color::Yellow);
        texts.push_str_with_color(
            "\nIf you want to add break lines, you should add `\\n`.\n",
            Color::Magenta,
        );

        texts.push_str_with_color(
            "It is also possible to change text attribute...\n",
            Color::White,
        );
        texts.push_str_with_color_and_attribute(
            "Green and bold text\n",
            Color::Green,
            Attribute::Bold,
        );
        texts.push_str_with_color_and_attribute(
            "Blue and underlined text\n",
            Color::Blue,
            Attribute::Underline,
        );
        texts.push_str_with_color_and_attribute(
            "Red and reversed text",
            Color::Red,
            Attribute::Reversed,
        );
        println!("{}", texts);
    }
}