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
/// Represents an color.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum Color {
    /// Resets the terminal color.
    Reset,
    /// Black color.
    Black,
    /// Dark grey color.
    DarkGrey,
    /// Light red color.
    Red,
    /// Dark red color.
    DarkRed,
    /// Light green color.
    Green,
    /// Dark green color.
    DarkGreen,
    /// Light yellow color.
    Yellow,
    /// Dark yellow color.
    DarkYellow,
    /// Light blue color.
    Blue,
    /// Dark blue color.
    DarkBlue,
    /// Light magenta color.
    Magenta,
    /// Dark magenta color.
    DarkMagenta,
    /// Light cyan color.
    Cyan,
    /// Dark cyan color.
    DarkCyan,
    /// White color.
    White,
    /// Grey color.
    Grey,
    /// An RGB color. See [RGB color model](https://en.wikipedia.org/wiki/RGB_color_model) for more info.
    ///
    /// Most UNIX terminals and Windows 10 supported only.
    /// See [Platform-specific notes](enum.Color.html#platform-specific-notes) for more info.
    Rgb(u8, u8, u8),

    /// An ANSI color. See [256 colors - cheat sheet](https://jonasjacek.github.io/colors/) for more info.
    ///
    /// Most UNIX terminals and Windows 10 supported only.
    /// See [Platform-specific notes](enum.Color.html#platform-specific-notes) for more info.
    AnsiValue(u8),
}

impl From<u8> for Color {
    fn from(n: u8) -> Self {
        match n {
            0 => Color::Black,
            1 => Color::Red,
            2 => Color::Green,
            3 => Color::Yellow,
            4 => Color::Blue,
            5 => Color::Magenta,
            6 => Color::Cyan,
            7 => Color::White,

            8 => Color::Black,
            9 => Color::DarkRed,
            10 => Color::DarkGreen,
            11 => Color::DarkYellow,
            12 => Color::DarkBlue,
            13 => Color::DarkMagenta,
            14 => Color::DarkCyan,
            15 => Color::Grey,

            // parsing: https://stackoverflow.com/questions/27159322/rgb-values-of-the-colors-in-the-ansi-extended-colors-index-17-255
            _ if n > 15 && n < 232 => {
                let rgb_r = ((n - 16) / 36) * 51;
                let rgb_g = (((n - 16) % 36) / 6) * 51;
                let rgb_b = ((n - 16) % 6) * 51;

                Color::Rgb(rgb_r, rgb_g, rgb_b)
            }
            _ if n >= 232 => {
                let value = (n - 232) * 10 + 8;
                Color::Rgb(value, value, value)
            }
            _ => unreachable!(),
        }
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum Attribute {
    /// Resets all the attributes.
    Reset,

    /// Increases the text intensity.
    Bold,
    /// Decreases the text intensity.
    BoldOff,

    /// Emphasises the text.
    Italic,
    /// Turns off the `Italic` attribute.
    ItalicOff,

    /// Underlines the text.
    Underlined,
    /// Turns off the `Underlined` attribute.
    UnderlinedOff,

    /// Makes the text blinking (< 150 per minute).
    SlowBlink,
    /// Makes the text blinking (>= 150 per minute).
    RapidBlink,
    /// Turns off the text blinking (`SlowBlink` or `RapidBlink`).
    BlinkOff,

    /// Crosses the text.
    Crossed,
    /// Turns off the `CrossedOut` attribute.
    CrossedOff,

    /// Swaps foreground and background colors.
    Reversed,
    /// Turns off the `Reverse` attribute.
    ReversedOff,

    /// Hides the text (also known as hidden).
    Conceal,
    /// Turns off the `Hidden` attribute.
    ConcealOff,

    /// Sets the [Fraktur](https://en.wikipedia.org/wiki/Fraktur) typeface.
    ///
    /// Mostly used for [mathematical alphanumeric symbols](https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols).
    Fraktur,

    /// Turns off the `Bold` attribute.
    NormalIntensity,

    /// Switches the text back to normal intensity (no bold, italic).
    BoldItalicOff,
    /// Makes the text framed.
    Framed,

    #[doc(hidden)]
    __Nonexhaustive,
}

impl From<Attribute> for String {
    fn from(attr: Attribute) -> Self {
        format!("{:?}", attr)
    }
}