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
use std::fmt;
use rawcmd_utils::{ANSI_PAIR};

/// Style structure which represents a formatted command-line string.
#[derive(Debug, Clone, PartialEq)]
pub struct Style {
    text: String,
    color: Option<usize>,
    bg: Option<usize>,
    style: Option<usize>,
}

/// Style structure implementation.
impl Style {

    /// Returns a new instance.
    pub fn new(txt: &str) -> Self {
        Self {
            text: txt.to_string(),
            color: None,
            bg: None,
            style: None,
        }
    }

    /// Applies `bold` style.
    pub fn bold(mut self) -> Self {
        self.style = Some(1);
        self
    }

    /// Applies `dim` style.
    pub fn dim(mut self) -> Self {
        self.style = Some(2);
        self
    }

    /// Applies `italic` style.
    pub fn italic(mut self) -> Self {
        self.style = Some(3);
        self
    }

    /// Applies `underline` style.
    pub fn underline(mut self) -> Self {
        self.style = Some(4);
        self
    }
    /// Applies `blink` style.
    pub fn blink(mut self) -> Self {
        self.style = Some(5);
        self
    }
    /// Applies `inverse` style.
    pub fn inverse(mut self) -> Self {
        self.style = Some(6);
        self
    }
    /// Applies `hidden` style.
    pub fn hidden(mut self) -> Self {
        self.style = Some(7);
        self
    }
    /// Applies `black` style.
    pub fn black(mut self) -> Self {
        self.color = Some(8);
        self
    }
    /// Applies `red` style.
    pub fn red(mut self) -> Self {
        self.color = Some(9);
        self
    }
    /// Applies `green` style.
    pub fn green(mut self) -> Self {
        self.color = Some(10);
        self
    }
    /// Applies `yellow` style.
    pub fn yellow(mut self) -> Self {
        self.color = Some(11);
        self
    }
    /// Applies `blue` style.
    pub fn blue(mut self) -> Self {
        self.color = Some(12);
        self
    }
    /// Applies `magenta` style.
    pub fn magenta(mut self) -> Self {
        self.color = Some(13);
        self
    }
    /// Applies `cyan` style.
    pub fn cyan(mut self) -> Self {
        self.color = Some(14);
        self
    }
    /// Applies `white` style.
    pub fn white(mut self) -> Self {
        self.color = Some(15);
        self
    }
    /// Applies `bgblack` style.
    pub fn bgblack(mut self) -> Self {
        self.bg = Some(16);
        self
    }
    /// Applies `bgred` style.
    pub fn bgred(mut self) -> Self {
        self.bg = Some(17);
        self
    }
    /// Applies `bggreen` style.
    pub fn bggreen(mut self) -> Self {
        self.bg = Some(18);
        self
    }
    /// Applies `bgyellow` style.
    pub fn bgyellow(mut self) -> Self {
        self.bg = Some(19);
        self
    }
    /// Applies `bgblue` style.
    pub fn bgblue(mut self) -> Self {
        self.bg = Some(20);
        self
    }
    /// Applies `bgmagenta` style.
    pub fn bgmagenta(mut self) -> Self {
        self.bg = Some(21);
        self
    }
    /// Applies `bgcyan` style.
    pub fn bgcyan(mut self) -> Self {
        self.bg = Some(22);
        self
    }
    /// Applies `bgwhite` style.
    pub fn bgwhite(mut self) -> Self {
        self.bg = Some(23);
        self
    }

    /// Removes all styles.
    pub fn clean(mut self) -> Self {
        self.color = None;
        self.bg = None;
        self.style = None;
        self
    }
}

/// Style structure implementation.
impl fmt::Display for Style {

    /// Formats the user-facing output.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut txt = self.text.to_string();
        if self.color.is_some() {
            let index = self.color.unwrap();
            txt = format!("{}{}{}", ANSI_PAIR[index][0], txt, ANSI_PAIR[index][1]);
        }
        if self.bg.is_some() {
            let index = self.bg.unwrap();
            txt = format!("{}{}{}", ANSI_PAIR[index][0], txt, ANSI_PAIR[index][1]);
        }
        if self.style.is_some() {
            let index = self.style.unwrap();
            txt = format!("{}{}{}", ANSI_PAIR[index][0], txt, ANSI_PAIR[index][1]);
        }
        write!(f, "{}", txt)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn applies_bold_style() {
        assert_eq!(
            Style::new("foo").bold().to_string(),
            format!("{}{}{}", ANSI_PAIR[1][0], "foo", ANSI_PAIR[1][1]),
        );
    }

    #[test]
    fn applies_dim_style() {
        assert_eq!(
            Style::new("foo").dim().to_string(),
            format!("{}{}{}", ANSI_PAIR[2][0], "foo", ANSI_PAIR[2][1]),
        );
    }

    #[test]
    fn applies_italic_style() {
        assert_eq!(
            Style::new("foo").italic().to_string(),
            format!("{}{}{}", ANSI_PAIR[3][0], "foo", ANSI_PAIR[3][1]),
        );
    }

    #[test]
    fn applies_underline_style() {
        assert_eq!(
            Style::new("foo").underline().to_string(),
            format!("{}{}{}", ANSI_PAIR[4][0], "foo", ANSI_PAIR[4][1]),
        );
    }

    #[test]
    fn applies_blink_style() {
        assert_eq!(
            Style::new("foo").blink().to_string(),
            format!("{}{}{}", ANSI_PAIR[5][0], "foo", ANSI_PAIR[5][1]),
        );
    }

    #[test]
    fn applies_inverse_style() {
        assert_eq!(
            Style::new("foo").inverse().to_string(),
            format!("{}{}{}", ANSI_PAIR[6][0], "foo", ANSI_PAIR[6][1]),
        );
    }

    #[test]
    fn applies_hidden_style() {
        assert_eq!(
            Style::new("foo").hidden().to_string(),
            format!("{}{}{}", ANSI_PAIR[7][0], "foo", ANSI_PAIR[7][1]),
        );
    }

    #[test]
    fn applies_black_color() {
        assert_eq!(
            Style::new("foo").black().to_string(),
            format!("{}{}{}", ANSI_PAIR[8][0], "foo", ANSI_PAIR[8][1]),
        );
    }

    #[test]
    fn applies_red_color() {
        assert_eq!(
            Style::new("foo").red().to_string(),
            format!("{}{}{}", ANSI_PAIR[9][0], "foo", ANSI_PAIR[9][1]),
        );
    }

    #[test]
    fn applies_green_color() {
        assert_eq!(
            Style::new("foo").green().to_string(),
            format!("{}{}{}", ANSI_PAIR[10][0], "foo", ANSI_PAIR[10][1]),
        );
    }

    #[test]
    fn applies_yellow_color() {
        assert_eq!(
            Style::new("foo").yellow().to_string(),
            format!("{}{}{}", ANSI_PAIR[11][0], "foo", ANSI_PAIR[11][1]),
        );
    }

    #[test]
    fn applies_blue_color() {
        assert_eq!(
            Style::new("foo").blue().to_string(),
            format!("{}{}{}", ANSI_PAIR[12][0], "foo", ANSI_PAIR[12][1]),
        );
    }

    #[test]
    fn applies_magenta_color() {
        assert_eq!(
            Style::new("foo").magenta().to_string(),
            format!("{}{}{}", ANSI_PAIR[13][0], "foo", ANSI_PAIR[13][1]),
        );
    }

    #[test]
    fn applies_cyan_color() {
        assert_eq!(
            Style::new("foo").cyan().to_string(),
            format!("{}{}{}", ANSI_PAIR[14][0], "foo", ANSI_PAIR[14][1]),
        );
    }

    #[test]
    fn applies_white_color() {
        assert_eq!(
            Style::new("foo").white().to_string(),
            format!("{}{}{}", ANSI_PAIR[15][0], "foo", ANSI_PAIR[15][1]),
        );
    }

    #[test]
    fn applies_black_background() {
        assert_eq!(
            Style::new("foo").bgblack().to_string(),
            format!("{}{}{}", ANSI_PAIR[16][0], "foo", ANSI_PAIR[16][1]),
        );
    }

    #[test]
    fn applies_red_background() {
        assert_eq!(
            Style::new("foo").bgred().to_string(),
            format!("{}{}{}", ANSI_PAIR[17][0], "foo", ANSI_PAIR[17][1]),
        );
    }

    #[test]
    fn applies_green_background() {
        assert_eq!(
            Style::new("foo").bggreen().to_string(),
            format!("{}{}{}", ANSI_PAIR[18][0], "foo", ANSI_PAIR[18][1]),
        );
    }

    #[test]
    fn applies_yellow_background() {
        assert_eq!(
            Style::new("foo").bgyellow().to_string(),
            format!("{}{}{}", ANSI_PAIR[19][0], "foo", ANSI_PAIR[19][1]),
        );
    }

    #[test]
    fn applies_blue_background() {
        assert_eq!(
            Style::new("foo").bgblue().to_string(),
            format!("{}{}{}", ANSI_PAIR[20][0], "foo", ANSI_PAIR[20][1]),
        );
    }

    #[test]
    fn applies_magenta_background() {
        assert_eq!(
            Style::new("foo").bgmagenta().to_string(),
            format!("{}{}{}", ANSI_PAIR[21][0], "foo", ANSI_PAIR[21][1]),
        );
    }

    #[test]
    fn applies_cyan_background() {
        assert_eq!(
            Style::new("foo").bgcyan().to_string(),
            format!("{}{}{}", ANSI_PAIR[22][0], "foo", ANSI_PAIR[22][1]),
        );
    }

    #[test]
    fn applies_white_background() {
        assert_eq!(
            Style::new("foo").bgwhite().to_string(),
            format!("{}{}{}", ANSI_PAIR[23][0], "foo", ANSI_PAIR[23][1]),
        );
    }

    #[test]
    fn applyes_color_background_and_style() {
        assert_eq!(
            Style::new("foo").blink().red().bgwhite().to_string(),
            format!("{}{}{}{}{}{}{}", ANSI_PAIR[5][0], ANSI_PAIR[23][0], ANSI_PAIR[9][0], "foo", ANSI_PAIR[9][1], ANSI_PAIR[23][1], ANSI_PAIR[5][1]),
        );
    }

    #[test]
    fn cleans_styles() {
        assert_eq!(
            Style::new("foo").red().clean().to_string(),
            "foo",
        );
    }
}