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
//! This is a ANSI specific implementation for styling related action.
//! This module is used for Windows 10 terminals and Unix terminals by default.

use crate::utils::Result;
use crate::{csi, write_cout};

use super::super::{Attribute, Color, Colored, Style};

pub(crate) fn set_fg_csi_sequence(fg_color: Color) -> String {
    format!(csi!("{}m"), Into::<String>::into(Colored::Fg(fg_color)))
}

pub(crate) fn set_bg_csi_sequence(bg_color: Color) -> String {
    format!(csi!("{}m"), Into::<String>::into(Colored::Bg(bg_color)))
}

pub(crate) fn set_attr_csi_sequence(attribute: Attribute) -> String {
    format!(csi!("{}m"), attribute as i16)
}

pub(crate) static RESET_CSI_SEQUENCE: &'static str = csi!("0m");

/// This struct is an ANSI escape code implementation for color related actions.
pub(crate) struct AnsiColor;

impl AnsiColor {
    pub fn new() -> AnsiColor {
        AnsiColor
    }
}

impl Style for AnsiColor {
    fn set_fg(&self, fg_color: Color) -> Result<()> {
        write_cout!(set_fg_csi_sequence(fg_color))?;
        Ok(())
    }

    fn set_bg(&self, bg_color: Color) -> Result<()> {
        write_cout!(set_bg_csi_sequence(bg_color))?;
        Ok(())
    }

    fn reset(&self) -> Result<()> {
        write_cout!(RESET_CSI_SEQUENCE)?;
        Ok(())
    }
}

impl From<Colored> for String {
    fn from(colored: Colored) -> Self {
        let mut ansi_value = String::new();

        let color;

        match colored {
            Colored::Fg(new_color) => {
                if new_color == Color::Reset {
                    ansi_value.push_str("39");
                    return ansi_value;
                } else {
                    ansi_value.push_str("38;");
                    color = new_color;
                }
            }
            Colored::Bg(new_color) => {
                if new_color == Color::Reset {
                    ansi_value.push_str("49");
                    return ansi_value;
                } else {
                    ansi_value.push_str("48;");
                    color = new_color;
                }
            }
        }

        let color_val = match color {
            Color::Black => "5;0",
            Color::DarkGrey => "5;8",
            Color::Red => "5;9",
            Color::DarkRed => "5;1",
            Color::Green => "5;10",
            Color::DarkGreen => "5;2",
            Color::Yellow => "5;11",
            Color::DarkYellow => "5;3",
            Color::Blue => "5;12",
            Color::DarkBlue => "5;4",
            Color::Magenta => "5;13",
            Color::DarkMagenta => "5;5",
            Color::Cyan => "5;14",
            Color::DarkCyan => "5;6",
            Color::White => "5;15",
            Color::Grey => "5;7",
            Color::Rgb { r, g, b } => {
                ansi_value.push_str(format!("2;{};{};{}", r, g, b).as_str());
                ""
            }
            Color::AnsiValue(val) => {
                ansi_value.push_str(format!("5;{}", val).as_str());
                ""
            }
            _ => "",
        };

        ansi_value.push_str(color_val);
        ansi_value
    }
}

#[cfg(test)]
mod tests {
    use crate::{Color, Colored};

    #[test]
    fn test_parse_fg_color() {
        let colored = Colored::Fg(Color::Red);
        assert_eq!(Into::<String>::into(colored), "38;5;9");
    }

    #[test]
    fn test_parse_bg_color() {
        let colored = Colored::Bg(Color::Red);
        assert_eq!(Into::<String>::into(colored), "48;5;9");
    }

    #[test]
    fn test_parse_reset_fg_color() {
        let colored = Colored::Fg(Color::Reset);
        assert_eq!(Into::<String>::into(colored), "39");
    }

    #[test]
    fn test_parse_reset_bg_color() {
        let colored = Colored::Bg(Color::Reset);
        assert_eq!(Into::<String>::into(colored), "49");
    }

    #[test]
    fn test_parse_fg_rgb_color() {
        let colored = Colored::Bg(Color::Rgb { r: 1, g: 2, b: 3 });
        assert_eq!(Into::<String>::into(colored), "48;2;1;2;3");
    }

    #[test]
    fn test_parse_fg_ansi_color() {
        let colored = Colored::Fg(Color::AnsiValue(255));
        assert_eq!(Into::<String>::into(colored), "38;5;255");
    }
}