termit 0.7.0

Terminal UI over crossterm
Documentation
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
//! ANSI composable styling structures and helpers

use super::color::Color;
use std::{
    borrow::Cow,
    fmt::Display,
    ops::{Deref, DerefMut},
};

/// Wrap displayable types with style for the terminal.
/// It is usually used indirectly through the trait helper [`Styled`] and [`Stylist`]:
///
/// ```rust
/// use termit::prelude::Stylist;
/// use termit::prelude::Pretty;
/// use termit::prelude::Color;
/// let pretty = "hello".front(Color::white());
/// assert!(matches!(pretty, Pretty{..}));
/// // Pretty itself can be styled:
/// pretty.bold(true);
/// // And thanks to deref, you can still access the str
/// assert_eq!(pretty.chars().next(), Some('h'));
/// ```
///
/// [`Pretty`] is also a widget so you can use it in your UI.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Pretty<T> {
    content: T,
    style: Style,
}
impl<T> Pretty<T> {
    /// Create something pretty, then use the [`Stylist`] helper.
    pub fn new(content: T) -> Self {
        Self {
            content,
            style: Style::DEFAULT,
        }
    }
    pub fn into_inner(self) -> (T, Style) {
        (self.content, self.style)
    }
}

/// The composable style for your terminal output.
///
/// You probably do not need to work it directly and
/// instead use the [`Stylist`] helper.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Style {
    // the foreground text color
    pub front_color: Option<Color>,
    // the background color
    pub back_color: Option<Color>,
    // the underline color
    pub frame_color: Option<Color>,
    // use bold font
    pub bold: Option<bool>,
    // use itelic font
    pub italic: Option<bool>,
    // cross out the text (a.k.a. strike through)
    pub crossed_out: Option<bool>,
    // underline the text
    pub underlined: Option<bool>,
    // blinking text
    pub blink: Option<bool>,
}
impl Style {
    /// static empty style
    pub const DEFAULT: Style = Style {
        front_color: None,
        back_color: None,
        frame_color: None,
        bold: None,
        italic: None,
        underlined: None,
        blink: None,
        crossed_out: None,
    };
}
/// Things that have a style and we can modify it
///
/// Implement this if you hold a style inside and want to be `Styled`
pub trait Stylish {
    fn style_mut(&mut self) -> &mut Style;
    fn style(&self) -> &Style;
}
/// Convenience to modify style on things that have one (or may have one)
///
/// You probably wouldn't implement this directly. Instead, implement `Styled`
pub trait Stylist {
    type Cute;
    fn front(self, color: impl Into<Option<Color>>) -> Self::Cute;
    fn back(self, color: impl Into<Option<Color>>) -> Self::Cute;
    fn frame(self, color: impl Into<Option<Color>>) -> Self::Cute;
    fn underlined(self, underlined: impl Into<Option<bool>>) -> Self::Cute;
    fn crossed_out(self, crossed: bool) -> Self::Cute;
    fn bold(self, bold: impl Into<Option<bool>>) -> Self::Cute;
    fn italic(self, italic: impl Into<Option<bool>>) -> Self::Cute;
    fn blink(self, blink: impl Into<Option<bool>>) -> Self::Cute;
    fn apply(self, additional: impl AsRef<Style>) -> Self::Cute;
    fn with_style(self, what: impl FnOnce(&mut Style)) -> Self::Cute;
}
/// Things that would benefit from touching up with extra style
///
/// Implement this if you'd like to be styled.
/// Either you hold the style within, so implement `Stylish` and your `type Cute=Self`.
/// Or you can wrap yourself in `Pretty` and your `type Cute=Pretty<Self>`
pub trait Styled: Sized {
    type Cute: Stylish;
    fn pretty(self) -> Self::Cute;
}

impl<T> Stylish for Pretty<T> {
    fn style_mut(&mut self) -> &mut Style {
        &mut self.style
    }
    fn style(&self) -> &Style {
        &self.style
    }
}
impl Stylish for Style {
    fn style_mut(&mut self) -> &mut Style {
        self
    }
    fn style(&self) -> &Style {
        self
    }
}

impl Styled for Style {
    type Cute = Self;

    fn pretty(self) -> Self::Cute {
        self
    }
}
impl<T> Styled for Pretty<T> {
    type Cute = Self;

    fn pretty(self) -> Self::Cute {
        self
    }
}

impl<T> Stylist for T
where
    T: Styled,
{
    type Cute = T::Cute;
    fn front(self, color: impl Into<Option<Color>>) -> Self::Cute {
        self.with_style(|style| style.front_color = color.into())
    }
    fn back(self, color: impl Into<Option<Color>>) -> Self::Cute {
        self.with_style(|style| style.back_color = color.into())
    }
    fn underlined(self, underlined: impl Into<Option<bool>>) -> Self::Cute {
        self.with_style(|style| style.underlined = underlined.into())
    }
    fn frame(self, color: impl Into<Option<Color>>) -> Self::Cute {
        self.with_style(|style| style.frame_color = color.into())
    }
    fn crossed_out(self, crossed: bool) -> Self::Cute {
        self.with_style(|style| style.crossed_out = crossed.into())
    }
    fn bold(self, bold: impl Into<Option<bool>>) -> Self::Cute {
        self.with_style(|style| style.bold = bold.into())
    }
    fn italic(self, italic: impl Into<Option<bool>>) -> Self::Cute {
        self.with_style(|style| style.italic = italic.into())
    }
    fn blink(self, blink: impl Into<Option<bool>>) -> Self::Cute {
        self.with_style(|style| style.blink = blink.into())
    }
    fn apply(self, additional: impl AsRef<Style>) -> Self::Cute {
        self.with_style(|style| *style += additional)
    }
    fn with_style(self, what: impl FnOnce(&mut Style)) -> Self::Cute {
        let mut pretty = self.pretty();
        what(pretty.style_mut());
        pretty
    }
}

impl<T> Deref for Pretty<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.content
    }
}
impl<T> DerefMut for Pretty<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.content
    }
}

macro_rules! with_default_style {
    ($type:ty) => {
        impl Styled for $type {
            type Cute = Pretty<Self>;
            fn pretty(self) -> Pretty<Self> {
                Pretty {
                    content: self,
                    style: Style::DEFAULT,
                }
            }
        }
    };
}

with_default_style!(Box<dyn Display>);
with_default_style!(&str);
with_default_style!(String);
with_default_style!(&String);
with_default_style!(&mut String);
with_default_style!(Cow<'_, &'_ str>);
with_default_style!(std::sync::Arc<String>);
with_default_style!(std::rc::Rc<String>);
with_default_style!(std::cell::Cell<String>);
with_default_style!(std::sync::Arc<&'_ str>);
with_default_style!(std::rc::Rc<&'_ str>);
with_default_style!(std::cell::Cell<&'_ str>);
with_default_style!(std::sync::Arc<Cow<'_, &'_ str>>);
with_default_style!(std::rc::Rc<Cow<'_, &'_ str>>);
with_default_style!(std::cell::Cell<Cow<'_, &'_ str>>);
with_default_style!(std::fmt::Arguments<'_>);

/// Combine styles - LHS is the default, RHS overrides
impl<T> std::ops::Add<T> for Style
where
    T: AsRef<Style>,
{
    type Output = Style;

    fn add(self, rhs: T) -> Self::Output {
        let rhs = rhs.as_ref();
        Self {
            front_color: rhs.front_color.or(self.front_color),
            back_color: rhs.back_color.or(self.back_color),
            frame_color: rhs.frame_color.or(self.frame_color),
            crossed_out: rhs.crossed_out.or(self.crossed_out),
            bold: rhs.bold.or(self.bold),
            italic: rhs.italic.or(self.italic),
            underlined: rhs.underlined.or(self.underlined),
            blink: rhs.blink.or(self.blink),
        }
    }
}
/// Combine styles - LHS is the default, RHS overrides
impl AsRef<Style> for Style {
    fn as_ref(&self) -> &Style {
        self
    }
}
/// Combine styles - LHS is the default, RHS overrides
impl<T> std::ops::Add<T> for &Style
where
    T: AsRef<Style>,
{
    type Output = Style;

    fn add(self, rhs: T) -> Self::Output {
        let rhs = rhs.as_ref();
        *self + rhs
    }
}
/// Combine styles - LHS is the default, RHS overrides
impl<T> std::ops::Add<T> for &mut Style
where
    T: AsRef<Style>,
{
    type Output = Style;

    fn add(self, rhs: T) -> Self::Output {
        let rhs = rhs.as_ref();
        *self + rhs
    }
}
/// Combine styles - LHS is the default, RHS overrides
impl<T> std::ops::AddAssign<T> for Style
where
    T: AsRef<Style>,
{
    fn add_assign(&mut self, rhs: T) {
        *self = *self + rhs.as_ref()
    }
}

pub type StylishStringy<'a> = Pretty<Cow<'a, str>>;
impl<'a> From<Pretty<&'a str>> for StylishStringy<'a> {
    fn from(content: Pretty<&'a str>) -> Self {
        StylishStringy {
            style: content.style,
            content: Cow::Borrowed(content.content),
        }
    }
}
impl<'a> From<Pretty<String>> for StylishStringy<'a> {
    fn from(content: Pretty<String>) -> Self {
        StylishStringy {
            style: content.style,
            content: Cow::Owned(content.content),
        }
    }
}
impl<'a> From<&'a str> for StylishStringy<'a> {
    fn from(content: &'a str) -> Self {
        let style = Style::default();
        StylishStringy {
            style,
            content: Cow::Borrowed(content),
        }
    }
}
impl<'a> From<String> for StylishStringy<'a> {
    fn from(content: String) -> Self {
        let style = Style::default();
        StylishStringy {
            style,
            content: Cow::Owned(content),
        }
    }
}

#[test]
fn test_str() {
    assert_eq!(
        "nice".front(Color::blue(true)),
        Pretty {
            content: "nice",
            style: Style {
                front_color: Some(Color::blue(true)),
                back_color: None,
                frame_color: None,
                crossed_out: None,
                bold: None,
                italic: None,
                blink: None,
                underlined: None
            }
        }
    );
}

#[test]
fn test_pretty() {
    assert_eq!(
        Pretty::new("content").front(Color::blue(true)),
        Pretty {
            content: "content",
            style: Style {
                front_color: Some(Color::blue(true)),
                back_color: None,
                frame_color: None,
                crossed_out: None,
                bold: None,
                italic: None,
                blink: None,
                underlined: None
            }
        }
    );
}

#[test]
fn test_mut_string() {
    let mut content = "content".to_owned();
    let mut content2 = "content".to_owned();

    let content_mut = &mut content;
    assert_eq!(
        content_mut.front(Color::blue(true)).back(Color::black()),
        Pretty {
            content: &mut content2,
            style: Style {
                front_color: Some(Color::blue(true)),
                back_color: Some(Color::ansi(0)),
                frame_color: None,
                crossed_out: None,
                bold: None,
                italic: None,
                blink: None,
                underlined: None
            }
        }
    );
}

#[test]
fn test_style_copy() {
    assert_eq!(
        Style::DEFAULT.front(Color::blue(true)),
        Style {
            front_color: Some(Color::blue(true)),
            back_color: None,
            frame_color: None,
            crossed_out: None,
            bold: None,
            italic: None,
            blink: None,
            underlined: None
        }
    );
}