termina 0.3.1

A cross-platform VT manipulation library
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
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
434
435
436
437
438
439
440
441
//! Types for styling terminal cells.

// CREDIT: This is shared almost fairly between crossterm and termwiz. SGR properties like
// `Underline`, `CursorStyle` and `Intensity` are from termwiz. The `StyleExt` trait is similar
// to a crossterm trait.

use std::{
    borrow::Cow,
    fmt::{self, Display},
    str::FromStr,
    sync::atomic::{AtomicBool, Ordering},
};

use crate::escape::{
    self,
    csi::{Csi, Sgr},
};

/// Styling of a cell's underline.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
// <https://sw.kovidgoyal.net/kitty/underlines/>
pub enum Underline {
    /// No underline
    #[default]
    None = 0,
    /// Straight underline
    Single = 1,
    /// Two underlines stacked on top of one another
    Double = 2,
    /// Curly / "squiggly" / "wavy" underline
    Curly = 3,
    /// Dotted underline
    Dotted = 4,
    /// Dashed underline
    Dashed = 5,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum CursorStyle {
    #[default]
    Default = 0,
    BlinkingBlock = 1,
    SteadyBlock = 2,
    BlinkingUnderline = 3,
    SteadyUnderline = 4,
    BlinkingBar = 5,
    SteadyBar = 6,
}

impl Display for CursorStyle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", *self as u8)
    }
}

/// An 8-bit "256-color".
///
/// Colors 0-15 are the same as `AnsiColor`s (0-7 being normal colors and 8-15 being "bright").
/// Colors 16-231 make up a 6x6x6 "color cube." The remaining 232-255 colors define a
/// dark-to-light grayscale in 24 steps.
///
/// These are also known as "web-safe colors" or "X11 colors" historically, although the actual
/// colors varied somewhat between historical usages.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
// <https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit>
pub struct WebColor(pub u8);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RgbColor {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
}

impl RgbColor {
    pub const fn new(red: u8, green: u8, blue: u8) -> Self {
        Self { red, green, blue }
    }

    /// The floats are expected to be in the range `0.0..=1.0`.
    pub fn new_f32(red: f32, green: f32, blue: f32) -> Self {
        let red = (red * 255.) as u8;
        let green = (green * 255.) as u8;
        let blue = (blue * 255.) as u8;
        Self { red, green, blue }
    }

    fn channel_from_hex(s: &str) -> Result<u8, InvalidFormatError> {
        if s.is_empty() || s.len() > 4 {
            return Err(InvalidFormatError);
        }
        let color: u16 = u16::from_str_radix(s, 16).map_err(|_| InvalidFormatError)?;
        let divisor: usize = match s.len() {
            1 => 0xf,
            2 => 0xff,
            3 => 0xfff,
            4 => 0xffff,
            _ => return Err(InvalidFormatError),
        };
        Ok(((color as usize) * 0xff / divisor) as u8)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InvalidFormatError;

impl FromStr for RgbColor {
    type Err = InvalidFormatError;

    // See `man xparsecolor`. This parses colors according to some of the formats accepted by
    // xterm's `XParseColor` function.
    //
    // 1. rgb:<red>/<green>/<blue>
    //    <red>, <green>, <blue> := h | hh | hhh | hhhh
    //    h := single hexadecimal digits (case insignificant)
    // 2. #RGB, #RRGGBB, #RRRGGGBBB, #RRRRGGGGBBBB
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some(rgb) = s.strip_prefix("rgb:") {
            let mut parts = rgb.split('/').map(Self::channel_from_hex);
            let Some(r) = parts.next().transpose()? else {
                return Err(InvalidFormatError);
            };
            let Some(g) = parts.next().transpose()? else {
                return Err(InvalidFormatError);
            };
            let Some(b) = parts.next().transpose()? else {
                return Err(InvalidFormatError);
            };
            Ok(Self::new(r, g, b))
        } else if let Some(hex) = s.strip_prefix('#') {
            let (r, g, b) = match hex.len() {
                3 => (
                    Self::channel_from_hex(&hex[0..1])?,
                    Self::channel_from_hex(&hex[1..2])?,
                    Self::channel_from_hex(&hex[2..3])?,
                ),
                6 => (
                    Self::channel_from_hex(&hex[0..2])?,
                    Self::channel_from_hex(&hex[2..4])?,
                    Self::channel_from_hex(&hex[4..6])?,
                ),
                9 => (
                    Self::channel_from_hex(&hex[0..3])?,
                    Self::channel_from_hex(&hex[3..6])?,
                    Self::channel_from_hex(&hex[6..9])?,
                ),
                12 => (
                    Self::channel_from_hex(&hex[0..4])?,
                    Self::channel_from_hex(&hex[4..8])?,
                    Self::channel_from_hex(&hex[8..12])?,
                ),
                _ => return Err(InvalidFormatError),
            };
            Ok(Self::new(r, g, b))
        } else {
            Err(InvalidFormatError)
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RgbaColor {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
    /// Also known as "opacity"
    pub alpha: u8,
}

impl From<RgbaColor> for RgbColor {
    fn from(color: RgbaColor) -> Self {
        Self {
            red: color.red,
            green: color.green,
            blue: color.blue,
        }
    }
}

impl From<RgbColor> for RgbaColor {
    fn from(color: RgbColor) -> Self {
        Self {
            red: color.red,
            green: color.green,
            blue: color.blue,
            alpha: 255,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
// <https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>
pub enum AnsiColor {
    Black = 0,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    /// "Bright" black (also known as "Gray")
    BrightBlack,
    BrightRed,
    BrightGreen,
    BrightYellow,
    BrightBlue,
    BrightMagenta,
    BrightCyan,
    BrightWhite,
}

pub type PaletteIndex = u8;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorSpec {
    Reset,
    PaletteIndex(PaletteIndex),
    TrueColor(RgbaColor),
}

impl ColorSpec {
    pub const BLACK: Self = Self::PaletteIndex(AnsiColor::Black as PaletteIndex);
    pub const RED: Self = Self::PaletteIndex(AnsiColor::Red as PaletteIndex);
    pub const GREEN: Self = Self::PaletteIndex(AnsiColor::Green as PaletteIndex);
    pub const YELLOW: Self = Self::PaletteIndex(AnsiColor::Yellow as PaletteIndex);
    pub const BLUE: Self = Self::PaletteIndex(AnsiColor::Blue as PaletteIndex);
    pub const MAGENTA: Self = Self::PaletteIndex(AnsiColor::Magenta as PaletteIndex);
    pub const CYAN: Self = Self::PaletteIndex(AnsiColor::Cyan as PaletteIndex);
    pub const WHITE: Self = Self::PaletteIndex(AnsiColor::White as PaletteIndex);
    pub const BRIGHT_BLACK: Self = Self::PaletteIndex(AnsiColor::BrightBlack as PaletteIndex);
    pub const BRIGHT_RED: Self = Self::PaletteIndex(AnsiColor::BrightRed as PaletteIndex);
    pub const BRIGHT_GREEN: Self = Self::PaletteIndex(AnsiColor::BrightGreen as PaletteIndex);
    pub const BRIGHT_YELLOW: Self = Self::PaletteIndex(AnsiColor::BrightYellow as PaletteIndex);
    pub const BRIGHT_BLUE: Self = Self::PaletteIndex(AnsiColor::BrightBlue as PaletteIndex);
    pub const BRIGHT_MAGENTA: Self = Self::PaletteIndex(AnsiColor::BrightMagenta as PaletteIndex);
    pub const BRIGHT_CYAN: Self = Self::PaletteIndex(AnsiColor::BrightCyan as PaletteIndex);
    pub const BRIGHT_WHITE: Self = Self::PaletteIndex(AnsiColor::BrightWhite as PaletteIndex);
}

impl From<AnsiColor> for ColorSpec {
    fn from(color: AnsiColor) -> Self {
        Self::PaletteIndex(color as u8)
    }
}

impl From<WebColor> for ColorSpec {
    fn from(color: WebColor) -> Self {
        Self::PaletteIndex(color.0)
    }
}

impl From<RgbColor> for ColorSpec {
    fn from(color: RgbColor) -> Self {
        Self::TrueColor(color.into())
    }
}

impl From<RgbaColor> for ColorSpec {
    fn from(color: RgbaColor) -> Self {
        Self::TrueColor(color)
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Intensity {
    #[default]
    Normal,
    Bold,
    Dim,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Blink {
    #[default]
    None,
    Slow,
    Rapid,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Font {
    #[default]
    Default,
    /// An alternate font. Valid values are 1-9.
    Alternate(u8),
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum VerticalAlign {
    #[default]
    BaseLine = 0,
    SuperScript = 1,
    SubScript = 2,
}

/// A helper type for conveniently rendering styled content to the terminal.
///
/// This is meant to be used instead of `PlatformTerminal` and proper CSI SGR codes when printing
/// basic text, for example a CLI's help string.
///
/// Instead of using this type directly, `use` the `StyleExt` trait and the helper functions
/// attached to strings:
///
/// ```
/// use termina::style::StyleExt as _;
/// assert_eq!("green".green().to_string(), "\x1b[0;32mgreen\x1b[m");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stylized<'a> {
    pub content: Cow<'a, str>,
    styles: Vec<Sgr>,
}

static INITIALIZER: parking_lot::Once = parking_lot::Once::new();
static NO_COLOR: AtomicBool = AtomicBool::new(false);

impl Stylized<'_> {
    /// Checks whether ANSI color sequences where turned off in the environment.
    ///
    /// See <https://no-color.org/>: if the `NO_COLOR` environment variable is present and
    /// non-empty, color escape sequences will be omitted when rendering this struct. This
    /// behavior can be overridden with [Self::force_ansi_color].
    pub fn is_ansi_color_disabled() -> bool {
        // <https://no-color.org/>
        INITIALIZER.call_once(|| {
            NO_COLOR.store(
                std::env::var("NO_COLOR").is_ok_and(|e| !e.is_empty()),
                Ordering::SeqCst,
            );
        });
        NO_COLOR.load(Ordering::SeqCst)
    }

    /// Overrides detection of the `NO_COLOR` environment variable.
    ///
    /// Pass `true` to ensure that ANSI color codes are always included when displaying this type
    /// or `false` to ensure ANSI color codes are never included.
    pub fn force_ansi_color(enable_color: bool) {
        // Run the `Once` first so this override is not later overwritten by the `Once` fn.
        let _ = Self::is_ansi_color_disabled();
        NO_COLOR.store(!enable_color, Ordering::SeqCst);
    }
}

impl Display for Stylized<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let no_color = Self::is_ansi_color_disabled();
        let mut styles = self
            .styles
            .iter()
            .filter(|sgr| {
                !(no_color
                    && matches!(
                        sgr,
                        Sgr::Foreground(_) | Sgr::Background(_) | Sgr::UnderlineColor(_)
                    ))
            })
            .peekable();

        if styles.peek().is_none() {
            write!(f, "{}", self.content)?;
        } else {
            write!(f, "{}0", escape::CSI)?;
            for sgr in styles {
                write!(f, ";{sgr}")?;
            }
            write!(f, "m{}{}", self.content, Csi::Sgr(Sgr::Reset))?;
        }
        Ok(())
    }
}

pub trait StyleExt<'a>: Sized {
    fn stylized(self) -> Stylized<'a>;

    fn foreground(self, color: impl Into<ColorSpec>) -> Stylized<'a> {
        let mut this = self.stylized();
        this.styles.push(Sgr::Foreground(color.into()));
        this
    }
    fn red(self) -> Stylized<'a> {
        self.foreground(ColorSpec::RED)
    }
    fn yellow(self) -> Stylized<'a> {
        self.foreground(ColorSpec::YELLOW)
    }
    fn green(self) -> Stylized<'a> {
        self.foreground(ColorSpec::GREEN)
    }
    fn underlined(self) -> Stylized<'a> {
        let mut this = self.stylized();
        this.styles.push(Sgr::Underline(Underline::Single));
        this
    }
    fn bold(self) -> Stylized<'a> {
        let mut this = self.stylized();
        this.styles.push(Sgr::Intensity(Intensity::Bold));
        this
    }
}

impl<'a> StyleExt<'a> for Cow<'a, str> {
    fn stylized(self) -> Stylized<'a> {
        Stylized {
            content: self,
            styles: Vec::with_capacity(2),
        }
    }
}

impl<'a> StyleExt<'a> for &'a str {
    fn stylized(self) -> Stylized<'a> {
        Cow::Borrowed(self).stylized()
    }
}

impl StyleExt<'static> for String {
    fn stylized(self) -> Stylized<'static> {
        Cow::<str>::Owned(self).stylized()
    }
}

// NOTE: this allows chaining like `"hello".green().bold()`.
impl<'a> StyleExt<'a> for Stylized<'a> {
    fn stylized(self) -> Stylized<'a> {
        self
    }
}

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

    #[test]
    fn parse_color() {
        assert_eq!("#282828".parse(), Ok(RgbColor::new(40, 40, 40)));
        assert_eq!("rgb:28/28/28".parse(), Ok(RgbColor::new(40, 40, 40)));
        assert_eq!("rgb:2828/2828/2828".parse(), Ok(RgbColor::new(40, 40, 40)));
    }
}