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
use std::fmt;

use crate::ansi_base::{BOLD, DIM, ITALIC, RESET, UNDERLINE};

/// String with the colored text
/// # Example
/// ```rust
/// use inksac::types::*;
///
/// let TITLESTYLE: Style = Style{
///     forground: Some(Color::Green),
///     background: Some(Color::Red),
///     ..Default::default()
/// };
/// let title_text: ColoredString = ColoredString::new(
///    "Hello World",
///     TITLESTYLE
///     );
/// println!("{}", title_text);
/// ```

#[derive(Debug, Clone)]
pub struct ColoredString {
    pub string: String,
    pub style: Style,
}

#[allow(dead_code)]
impl ColoredString {
    pub fn new(string: &str, style: Style) -> Self {
        Self {
            string: string.to_string(),
            style,
        }
    }
    /// Returns the non colored String
    pub fn to_no_style(&self) -> String {
        self.string.clone()
    }
}

impl fmt::Display for ColoredString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}{}{}", self.style, self.string, RESET)
    }
}

/// Style for the colored text
/// # Example
/// ```rust
/// use inksac::types::*;
///
/// let TITLESTYLE: Style = Style{
///     forground: Some(Color::Green),
///     background: Some(Color::Red),
///     ..Default::default()
/// };
/// ```

#[derive(Debug, Clone, Copy, Default)]
pub struct Style {
    pub forground: Option<Color>,
    pub background: Option<Color>,
    pub bold: bool,
    pub dim: bool,
    pub italic: bool,
    pub underline: bool,
}
impl fmt::Display for Style {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let fg = self.forground.unwrap_or(Color::Empty).to_fg();
        let bg = self.background.unwrap_or(Color::Empty).to_bg();
        let bold = if self.bold { BOLD } else { "" };
        let dim = if self.dim { DIM } else { "" };
        let italic = if self.italic { ITALIC } else { "" };
        let underline = if self.underline { UNDERLINE } else { "" };

        write!(f, "{}{}{}{}{}{}", fg, bg, bold, dim, italic, underline)
    }
}

impl Style {
    pub fn new() -> Self {
        Self::default()
    }
    /// Toggle Bold
    pub fn bold(mut self) -> Self {
        if !self.bold {
            self.bold = true;
        } else {
            self.bold = false;
        }
        self
    }
    /// Toggle Dim
    pub fn dim(mut self) -> Self {
        if !self.dim {
            self.dim = true;
        } else {
            self.dim = false;
        }
        self
    }
    /// Toggle Italic
    pub fn italic(mut self) -> Self {
        if !self.italic {
            self.italic = true;
        } else {
            self.italic = false;
        }
        self
    }
    /// Toggle Underline
    pub fn underline(mut self) -> Self {
        if !self.underline {
            self.underline = true;
        } else {
            self.underline = false;
        }
        self
    }
}

/// Color Palette

#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub enum Color {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,

    Empty,
    RGB(u8, u8, u8),
    HEX(&'static str),
}
impl Color {
    fn to_fg(self) -> String {
        match self {
            Color::Black => "\x1b[30m".to_string(),
            Color::Red => "\x1b[31m".to_string(),
            Color::Green => "\x1b[32m".to_string(),
            Color::Yellow => "\x1b[33m".to_string(),
            Color::Blue => "\x1b[34m".to_string(),
            Color::Magenta => "\x1b[35m".to_string(),
            Color::Cyan => "\x1b[36m".to_string(),
            Color::White => "\x1b[37m".to_string(),
            Color::Empty => "".to_string(),
            Color::RGB(r, g, b) => format!("\x1b[38;2;{};{};{}m", r, g, b),
            Color::HEX(code) => {
                let (r, g, b) = Self::hex_to_rgb(code);

                format!("\x1b[38;2;{};{};{}m", r, g, b)
            }
        }
    }
    fn to_bg(self) -> String {
        match self {
            Color::Black => "\x1b[40m".to_string(),
            Color::Red => "\x1b[41m".to_string(),
            Color::Green => "\x1b[42m".to_string(),
            Color::Yellow => "\x1b[43m".to_string(),
            Color::Blue => "\x1b[44m".to_string(),
            Color::Magenta => "\x1b[45m".to_string(),
            Color::Cyan => "\x1b[46m".to_string(),
            Color::White => "\x1b[47m".to_string(),
            Color::Empty => "".to_string(),
            Color::RGB(r, g, b) => format!("\x1b[48;2;{};{};{}m", r, g, b),
            Color::HEX(code) => {
                let (r, g, b) = Self::hex_to_rgb(code);

                format!("\x1b[48;2;{};{};{}m", r, g, b)
            }
        }
    }
    fn hex_to_rgb(hex: &str) -> (u8, u8, u8) {
        let hex = hex.trim_start_matches('#');
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap();
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap();
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap();
        (r, g, b)
    }
}

/*#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn underline() {
        let style = Style::default();
        let style2 = Style{
            forground: Some(Color::HEX("#6667AB")),
            ..Default::default()
        };
        let text = ColoredString::new("hello", style.clone());
        let text2 = ColoredString::new("world", style2.clone());

        println!("{}", text);
        println!("{}", text2);
        panic!();

    }
}*/