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
use std::fmt::{Debug, Display};

use crate::{Color, SGRBuilder, SGRWriter, Style};

/// A String encapsulating the usage of SGR codes
///
/// SGR codes are applied when the [`Display`] trait is used,
/// or when the [`SGRString::place_all`] or [`SGRString::clean_all`]
/// functions are called.
///
/// Writing is done through the use of the [`writing`](crate::writing) module
///
/// # Examples
///
///```rust
///use easy_sgr::{ColorKind, SGRString, StyleKind};
///
///let mut string = SGRString::default();
///string.text = String::from("This is my text");
///string.bold = StyleKind::Place;
///string.foreground = ColorKind::Red;
///println!("{string}");
///```
#[derive(Default, Debug, Clone)]
pub struct SGRString {
    /// The actual text
    pub text: String,
    /// The type of clean to apply when [`SGRString::clean_all`] is called
    ///
    /// By default [`CleanKind::None`], meaning nothing is done
    pub clean: CleanKind,

    /// Any custom codes added
    ///
    /// These codes are written before the string when
    /// the [`Display`] trait is called
    pub custom_places: Vec<u8>,
    /// Any custom codes added
    ///
    /// These codes are written after the string when
    /// the [`Display`] trait is called
    pub custom_cleans: Vec<u8>,

    /// The color of the foreground
    ///
    /// By default [`ColorKind::None`], meaning nothing is applied.
    /// Not to be confused with [`ColorKind::Default`], where the default SGR
    /// code for the foreground is applied.
    pub foreground: ColorKind,
    /// The color of the background
    ///
    /// By default [`ColorKind::None`], meaning nothing is applied.
    /// Not to be confused with [`ColorKind::Default`], where the default SGR
    /// code for the background is applied.
    pub background: ColorKind,

    /// Determines whether the clear code `0` is to be applied to the beginning
    ///
    /// Not be confused with [`SGRString.clean`], this effects [`SGRString::place_all`]
    pub reset: bool,
    /// Refer to [`StyleKind`]
    pub bold: StyleKind,
    /// Refer to [`StyleKind`]
    pub dim: StyleKind,
    /// Refer to [`StyleKind`]
    pub italic: StyleKind,
    /// Refer to [`StyleKind`]
    pub underline: StyleKind,
    /// Refer to [`StyleKind`]
    pub blinking: StyleKind,
    /// Refer to [`StyleKind`]
    pub inverse: StyleKind,
    /// Refer to [`StyleKind`]
    pub hidden: StyleKind,
    /// Refer to [`StyleKind`]
    pub strikethrough: StyleKind,
}
impl SGRString {
    /// Writes all contained SGR codes to the given [`SGRBuilder`]
    ///
    /// Does not perform any IO operations
    pub fn place_all(&self, builder: &mut SGRBuilder) {
        if self.reset {
            builder.write_code(0);
        }
        self.place_colors(builder);
        self.place_styles(builder);
        self.place_custom(builder);
    }
    /// Writes contained SGR color codes to the given [`SGRWriter`]
    ///
    /// Does not perform any IO operations
    pub fn place_colors(&self, builder: &mut SGRBuilder) {
        use ColorKind::*;
        match self.foreground {
            Black => builder.write_code(30),
            Red => builder.write_code(31),
            Green => builder.write_code(32),
            Yellow => builder.write_code(33),
            Blue => builder.write_code(34),
            Magenta => builder.write_code(35),
            Cyan => builder.write_code(36),
            White => builder.write_code(37),
            Byte(n) => builder.write_codes(&[38, 5, n]),
            Rgb(r, g, b) => builder.write_codes(&[38, 2, r, g, b]),
            Default => builder.write_code(39),
            ColorKind::None => (),
        };
        match self.background {
            Black => builder.write_code(40),
            Red => builder.write_code(41),
            Green => builder.write_code(42),
            Yellow => builder.write_code(43),
            Blue => builder.write_code(44),
            Magenta => builder.write_code(45),
            Cyan => builder.write_code(46),
            White => builder.write_code(47),
            Byte(n) => builder.write_codes(&[48, 5, n]),
            Rgb(r, g, b) => builder.write_codes(&[48, 2, r, g, b]),
            Default => builder.write_code(49),
            ColorKind::None => (),
        };
    }
    /// Writes SGR style codes to the given [`SGRWriter`]
    ///
    /// Does not perform any IO operations
    pub fn place_styles(&self, builder: &mut SGRBuilder) {
        use StyleKind::*;
        for (kind, place, not) in [
            (&self.bold, 1, 22),
            (&self.dim, 2, 22),
            (&self.italic, 3, 23),
            (&self.underline, 4, 24),
            (&self.blinking, 5, 25),
            (&self.inverse, 7, 27),
            (&self.hidden, 8, 28),
            (&self.strikethrough, 9, 29),
        ] {
            match kind {
                None => (),
                Place => builder.write_code(place),
                Clean => builder.write_code(not),
            }
        }
    }
    /// Writes custom SGR codes to the given [`SGRWriter`]
    ///
    /// Does not perform any IO operations
    pub fn place_custom(&self, builder: &mut SGRBuilder) {
        builder.write_codes(&self.custom_places);
    }
    /// Writes contained SGR codes to the given [`SGRWriter`]
    ///
    /// Reverses the effects of [`SGRString::place_all`]
    ///
    /// Does not perform any IO operations
    pub fn clean_all(&self, builder: &mut SGRBuilder) {
        match self.clean {
            CleanKind::Reset => builder.write_code(0),
            CleanKind::Reverse => {
                self.clean_colors(builder);
                self.clean_styles(builder);
            }
            CleanKind::None => (),
        }
        self.clean_custom(builder);
    }
    /// Writes SGR color codes to the given [`SGRWriter`]
    ///
    /// Reverses the effects of [`SGRString::place_colors`]
    ///
    /// Does not perform any IO operations
    pub fn clean_colors(&self, builder: &mut SGRBuilder) {
        if self.foreground != ColorKind::None {
            builder.write_code(39);
        }
        if self.background != ColorKind::None {
            builder.write_code(49);
        }
    }
    /// Writes SGR style codes to the given [`SGRWriter`]
    ///
    /// Reverses the effects of [`SGRString::place_styles`]
    ///
    /// Does not perform any IO operations
    pub fn clean_styles(&self, builder: &mut SGRBuilder) {
        for (kind, place, not) in [
            (&self.bold, 22, 1),
            (&self.dim, 22, 2),
            (&self.italic, 23, 3),
            (&self.underline, 24, 4),
            (&self.blinking, 25, 5),
            (&self.inverse, 27, 7),
            (&self.hidden, 28, 8),
            (&self.strikethrough, 29, 9),
        ] {
            match kind {
                StyleKind::None => (),
                StyleKind::Place => builder.write_code(place),
                StyleKind::Clean => builder.write_code(not),
            }
        }
    }
    /// Writes SGR codes to the given [`SGRWriter`]
    ///
    /// Reverses the effects of [`SGRString::place_custom`]
    ///
    /// Does not perform any IO operations
    pub fn clean_custom(&self, builder: &mut SGRBuilder) {
        builder.write_codes(&self.custom_cleans);
    }
}
impl From<Color> for SGRString {
    fn from(value: Color) -> Self {
        Self::default().color(value)
    }
}
impl From<Style> for SGRString {
    fn from(value: Style) -> Self {
        Self::default().style(value)
    }
}
impl From<&str> for SGRString {
    fn from(value: &str) -> Self {
        Self {
            text: String::from(value),
            ..Default::default()
        }
    }
}
impl From<String> for SGRString {
    fn from(value: String) -> Self {
        Self {
            text: value,
            ..Default::default()
        }
    }
}
impl From<&String> for SGRString {
    fn from(value: &String) -> Self {
        Self {
            text: String::from(value),
            ..Default::default()
        }
    }
}
impl Display for SGRString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut fmt = SGRWriter::from(f);
        fmt.place_sgr(self)?;
        fmt.write_inner(&self.text)?;
        fmt.clean_sgr(self)
    }
}
/// Component of [`SGRString`]; the type of clean
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum CleanKind {
    /// Does nothing
    #[default]
    None,
    /// Resets all by writing `\x1b[0m`
    Reset,
    /// Undoes the effects of the [`SGRString::place_all`].
    Reverse,
}
/// Component of [`SGRString`]; the type of a style
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum StyleKind {
    /// Do nothing
    #[default]
    None,
    /// Apply the style
    Place,
    /// Apply what undoes the style
    ///
    /// The equivalent in [`Style`] are variants prefixed with `Not`
    Clean,
}
/// Component of [`SGRString`]; the type of color
///
/// Used for both foreground and background
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum ColorKind {
    /// Does nothing
    #[default]
    None,
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    Byte(u8),
    Rgb(u8, u8, u8),
    /// Applies the default `SGR` color
    Default,
}
impl<I: Into<SGRString>> EasySGR for I {}
/// Allows for chaining SGR sequence types
///
/// Methods return a [`SGRString`]
pub trait EasySGR: Into<SGRString> {
    /// Turns self into [`SGRString`]
    ///
    /// Equivalent to calling
    ///```rust
    /// # use easy_sgr::SGRString;
    /// # pub trait EasySGR: Into<SGRString> {
    /// # fn to_sgr(self) -> SGRString {
    ///Into::<SGRString>::into(self)
    /// # }
    /// # }
    ///```
    #[must_use]
    #[inline]
    fn to_sgr(self) -> SGRString {
        self.into()
    }
    /// Sets the normal text of the returned [`SGRString`]  
    #[must_use]
    #[inline]
    fn text(self, text: impl Into<String>) -> SGRString {
        SGRString {
            text: text.into(),
            ..self.into()
        }
    }
    /// Adds a style to the returned [`SGRString`]
    #[must_use]
    #[inline]
    fn style(self, style: impl Into<Style>) -> SGRString {
        use Style::*;
        use StyleKind::*;

        let mut this = self.into();
        match style.into() {
            Reset => this.reset = true,
            Bold => this.bold = Place,
            Dim => this.dim = Place,
            Italic => this.italic = Place,
            Underline => this.underline = Place,
            Blinking => this.blinking = Place,
            Inverse => this.inverse = Place,
            Hidden => this.hidden = Place,
            Strikethrough => this.strikethrough = Place,

            NotBold => this.bold = Clean,
            NotDim => this.dim = Clean,
            NotItalic => this.italic = Clean,
            NotUnderline => this.underline = Clean,
            NotBlinking => this.blinking = Clean,
            NotInverse => this.inverse = Clean,
            NotHidden => this.hidden = Clean,
            NotStrikethrough => this.strikethrough = Clean,
        }
        this
    }
    /// Adds a color(foreground or background) to the returned [`SGRString`]  
    #[must_use]
    #[inline]
    fn color(self, color: impl Into<Color>) -> SGRString {
        use {Color::*, ColorKind::*};

        let mut this = self.into();

        (this.foreground, this.background) = match color.into() {
            BlackFg => (Black, this.background),
            RedFg => (Red, this.background),
            GreenFg => (Green, this.background),
            YellowFg => (Yellow, this.background),
            BlueFg => (Blue, this.background),
            MagentaFg => (Magenta, this.background),
            CyanFg => (Cyan, this.background),
            WhiteFg => (White, this.background),
            ByteFg(n) => (Byte(n), this.background),
            RgbFg(r, g, b) => (Rgb(r, g, b), this.background),
            DefaultFg => (Default, this.background),

            BlackBg => (this.foreground, Black),
            RedBg => (this.foreground, Red),
            GreenBg => (this.foreground, Green),
            YellowBg => (this.foreground, Yellow),
            BlueBg => (this.foreground, Blue),
            MagentaBg => (this.foreground, Magenta),
            CyanBg => (this.foreground, Cyan),
            WhiteBg => (this.foreground, White),
            ByteBg(n) => (this.foreground, Byte(n)),
            RgbBg(r, g, b) => (this.foreground, Rgb(r, g, b)),
            DefaultBg => (this.foreground, Default),
        };
        this
    }
    /// Adds a custom code to the returned [`SGRString`]
    ///
    /// Code is used in [`SGRString::place_all`]
    #[must_use]
    #[inline]
    fn custom(self, code: impl Into<u8>) -> SGRString {
        let mut this = self.into();
        this.custom_places.push(code.into());
        this
    }
    /// Sets the [`CleanKind`] variant of the returned [`SGRString`]
    #[must_use]
    #[inline]
    fn clean(self, clean: impl Into<CleanKind>) -> SGRString {
        let mut this = self.into();
        this.clean = clean.into();
        this
    }
    /// Adds a custom code to be written before the returned [`SGRString`]'s text
    #[must_use]
    #[inline]
    fn custom_place(self, code: impl Into<u8>) -> SGRString {
        let mut this = self.into();
        this.custom_places.push(code.into());
        this
    }
    /// Adds a custom code to be written after the returned [`SGRString`]'s text
    #[must_use]
    #[inline]
    fn custom_clean(self, code: impl Into<u8>) -> SGRString {
        let mut this = self.into();
        this.custom_cleans.push(code.into());
        this
    }
}