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
//! Tools for working with ansi escape sequences, mainly in serial terminals
//! 
//! **Requires the feature `ansi`**

use core::fmt::{self, Display, Formatter};

const ANSI_ESCAPE: &str = "\u{1B}[";
const ANSI_ESCAPE_END: &str = "m";

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[allow(missing_docs)]
pub enum Color {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    LightBlack,
    LightRed,
    LightGreen,
    LightYellow,
    LightBlue,
    LightMagenta,
    LightCyan,
    LightWhite,
    TrueColor { r: u8, g: u8, b: u8 },
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[allow(missing_docs)]
pub enum Style {
    Clear,
    Bold,
    Dimmed,
    Underline,
    Reversed,
    Italic,
    Blink,
    Hidden,
    Strikethrough,
}

/// A structure defining an ansi escape sequence. To convert
/// the structure to its string representation, use the
/// Display implementation
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct EscapeSequence<'a> {
    fg: Option<Color>,
    bg: Option<Color>,
    styles: &'a [Style],
}

impl<'a> EscapeSequence<'a> {
    /// Create a new escape sequence with no information
    pub const fn new() -> Self {
        Self {
            bg: None,
            fg: None,
            styles: &[],
        }
    }

    /// Set the foreground in the escape sequence
    pub const fn set_fg(self, color: Color) -> Self {
        EscapeSequence {
            fg: Some(color),
            ..self
        }
    }

    /// Set the background for the escape sequence
    pub const fn set_bg(self, color: Color) -> Self {
        EscapeSequence {
            bg: Some(color),
            ..self
        }
    }

    /// Set the styles in the escape sequence
    pub const fn set_styles(self, styles: &'a [Style]) -> Self {
        EscapeSequence { styles, ..self }
    }
}

#[cfg(not(feature = "no_color"))]
impl<'a> Display for EscapeSequence<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(ANSI_ESCAPE)?;

        // Foreground format
        if let Some(color) = self.fg {
            if let Color::TrueColor { r, g, b } = color {
                write!(f, "38;2;{};{};{}", r, g, b)?;
            } else {
                f.write_str(match color {
                    Color::Black => "30",
                    Color::Red => "31",
                    Color::Green => "32",
                    Color::Yellow => "33",
                    Color::Blue => "34",
                    Color::Magenta => "35",
                    Color::Cyan => "36",
                    Color::White => "37",
                    Color::LightBlack => "90",
                    Color::LightRed => "91",
                    Color::LightGreen => "92",
                    Color::LightYellow => "93",
                    Color::LightBlue => "94",
                    Color::LightMagenta => "95",
                    Color::LightCyan => "96",
                    Color::LightWhite => "97",
                    Color::TrueColor { .. } => unreachable!(),
                })?;
            }
        }

        // Background format
        if let Some(color) = self.bg {
            if let Color::TrueColor { r, g, b } = color {
                write!(f, "48;2;{};{};{}", r, g, b)?;
            } else {
                f.write_str(match color {
                    Color::Black => "40",
                    Color::Red => "41",
                    Color::Green => "42",
                    Color::Yellow => "43",
                    Color::Blue => "44",
                    Color::Magenta => "45",
                    Color::Cyan => "46",
                    Color::White => "47",
                    Color::LightBlack => "100",
                    Color::LightRed => "101",
                    Color::LightGreen => "102",
                    Color::LightYellow => "103",
                    Color::LightBlue => "104",
                    Color::LightMagenta => "105",
                    Color::LightCyan => "106",
                    Color::LightWhite => "107",
                    Color::TrueColor { .. } => unreachable!(),
                })?;
            }
        }

        for style in self.styles {
            f.write_str(match style {
                Style::Clear => "0",
                Style::Bold => "1",
                Style::Dimmed => "2",
                Style::Italic => "3",
                Style::Underline => "4",
                Style::Blink => "5",
                Style::Reversed => "7",
                Style::Hidden => "8",
                Style::Strikethrough => "9",
            })?;
        }

        f.write_str(ANSI_ESCAPE_END)
    }
}

#[cfg(feature = "no_color")]
impl Display for EscapeSequence<'_> {
    fn fmt(&self, _: &mut Formatter<'_>) -> fmt::Result {
        Ok(())
    }
}