nu_color_config/
text_style.rs

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
431
432
433
use nu_ansi_term::{Color, Style};

#[derive(Debug, Clone, Copy)]
pub enum Alignment {
    Center,
    Left,
    Right,
}

#[derive(Debug, Clone, Copy)]
pub struct TextStyle {
    pub alignment: Alignment,
    pub color_style: Option<Style>,
}

impl TextStyle {
    pub fn new() -> TextStyle {
        TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style::default()),
        }
    }

    pub fn bold(&self, bool_value: Option<bool>) -> TextStyle {
        let bv = bool_value.unwrap_or(false);

        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_bold: bv,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_bold(&self) -> bool {
        self.color_style.unwrap_or_default().is_bold
    }

    pub fn dimmed(&self) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_dimmed: true,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_dimmed(&self) -> bool {
        self.color_style.unwrap_or_default().is_dimmed
    }

    pub fn italic(&self) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_italic: true,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_italic(&self) -> bool {
        self.color_style.unwrap_or_default().is_italic
    }

    pub fn underline(&self) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_underline: true,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_underline(&self) -> bool {
        self.color_style.unwrap_or_default().is_underline
    }

    pub fn blink(&self) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_blink: true,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_blink(&self) -> bool {
        self.color_style.unwrap_or_default().is_blink
    }

    pub fn reverse(&self) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_reverse: true,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_reverse(&self) -> bool {
        self.color_style.unwrap_or_default().is_reverse
    }

    pub fn hidden(&self) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_hidden: true,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_hidden(&self) -> bool {
        self.color_style.unwrap_or_default().is_hidden
    }

    pub fn strikethrough(&self) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                is_strikethrough: true,
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn is_strikethrough(&self) -> bool {
        self.color_style.unwrap_or_default().is_strikethrough
    }

    pub fn fg(&self, foreground: Color) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                foreground: Some(foreground),
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn on(&self, background: Color) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                background: Some(background),
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn bg(&self, background: Color) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                background: Some(background),
                ..self.color_style.unwrap_or_default()
            }),
        }
    }

    pub fn alignment(&self, align: Alignment) -> TextStyle {
        TextStyle {
            alignment: align,
            color_style: self.color_style,
        }
    }

    pub fn style(&self, style: Style) -> TextStyle {
        TextStyle {
            alignment: self.alignment,
            color_style: Some(Style {
                foreground: style.foreground,
                background: style.background,
                is_bold: style.is_bold,
                is_dimmed: style.is_dimmed,
                is_italic: style.is_italic,
                is_underline: style.is_underline,
                is_blink: style.is_blink,
                is_reverse: style.is_reverse,
                is_hidden: style.is_hidden,
                is_strikethrough: style.is_strikethrough,
                prefix_with_reset: false,
            }),
        }
    }

    pub fn basic_center() -> TextStyle {
        TextStyle::new()
            .alignment(Alignment::Center)
            .style(Style::default())
    }

    pub fn basic_right() -> TextStyle {
        TextStyle::new()
            .alignment(Alignment::Right)
            .style(Style::default())
    }

    pub fn basic_left() -> TextStyle {
        TextStyle::new()
            .alignment(Alignment::Left)
            .style(Style::default())
    }

    pub fn default_header() -> TextStyle {
        TextStyle::new()
            .alignment(Alignment::Center)
            .fg(Color::Green)
            .bold(Some(true))
    }

    pub fn default_field() -> TextStyle {
        TextStyle::new().fg(Color::Green).bold(Some(true))
    }

    pub fn with_attributes(bo: bool, al: Alignment, co: Color) -> TextStyle {
        TextStyle::new().alignment(al).fg(co).bold(Some(bo))
    }

    pub fn with_style(al: Alignment, style: Style) -> TextStyle {
        TextStyle::new().alignment(al).style(Style {
            foreground: style.foreground,
            background: style.background,
            is_bold: style.is_bold,
            is_dimmed: style.is_dimmed,
            is_italic: style.is_italic,
            is_underline: style.is_underline,
            is_blink: style.is_blink,
            is_reverse: style.is_reverse,
            is_hidden: style.is_hidden,
            is_strikethrough: style.is_strikethrough,
            prefix_with_reset: false,
        })
    }
}

impl Default for TextStyle {
    fn default() -> Self {
        Self::new()
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use nu_ansi_term::Style;

    #[test]
    fn test_is_bold() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_bold: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_bold());
    }

    #[test]
    fn test_dimmed() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                ..Default::default()
            }),
        };
        let dimmed_style = text_style.dimmed();
        assert!(dimmed_style.is_dimmed());
    }

    #[test]
    fn test_is_dimmed() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_dimmed: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_dimmed());
    }

    #[test]
    fn test_italic() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                ..Default::default()
            }),
        };
        let italic_style = text_style.italic();
        assert!(italic_style.is_italic());
    }

    #[test]
    fn test_is_italic() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_italic: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_italic());
    }

    #[test]
    fn test_underline() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                ..Default::default()
            }),
        };
        let underline_style = text_style.underline();
        assert!(underline_style.is_underline());
    }

    #[test]
    fn test_is_underline() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_underline: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_underline());
    }

    #[test]
    fn test_blink() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                ..Default::default()
            }),
        };
        let blink_style = text_style.blink();
        assert!(blink_style.is_blink());
    }

    #[test]
    fn test_is_blink() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_blink: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_blink());
    }

    #[test]
    fn test_reverse() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                ..Default::default()
            }),
        };
        let reverse_style = text_style.reverse();
        assert!(reverse_style.is_reverse());
    }

    #[test]
    fn test_is_reverse() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_reverse: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_reverse());
    }

    #[test]
    fn test_hidden() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                ..Default::default()
            }),
        };
        let hidden_style = text_style.hidden();
        assert!(hidden_style.is_hidden());
    }

    #[test]
    fn test_is_hidden() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_hidden: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_hidden());
    }

    #[test]
    fn test_strikethrough() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                ..Default::default()
            }),
        };
        let strikethrough_style = text_style.strikethrough();
        assert!(strikethrough_style.is_strikethrough());
    }

    #[test]
    fn test_is_strikethrough() {
        let text_style = TextStyle {
            alignment: Alignment::Left,
            color_style: Some(Style {
                is_strikethrough: true,
                ..Default::default()
            }),
        };
        assert!(text_style.is_strikethrough());
    }
}