crates/tui-core/src/text.rs 3.0 KiB raw
1
//! Convenience constructors for consistently styled terminal text.
2
//!
3
//! These helpers create common painted text fragments such as dim, bold,
4
//! emphasized, or badge-like labels. They keep small style choices consistent
5
//! across examples and widgets without adding a larger theme system.
6
7
use std::fmt;
8
9
use super::{Color, Paint, Style, colors};
10
11
/// Default foreground color for faint neutral text.
12
pub const FAINT_COLOR: Color = colors::fixed::FAINT;
13
14
/// Returns unstyled text.
15
pub fn default<D: fmt::Display>(msg: D) -> Paint<D> {
16
    Paint::new(msg)
17
}
18
19
/// Returns wrapping text that preserves outer styling across resets.
20
pub fn wrap<D: fmt::Display>(msg: D) -> Paint<D> {
21
    Paint::wrapping(msg)
22
}
23
24
/// Returns text styled as negative or failing.
25
pub fn negative<D: fmt::Display>(msg: D) -> Paint<D> {
26
    Paint::red(msg)
27
}
28
29
/// Returns text styled as positive or successful.
30
pub fn positive<D: fmt::Display>(msg: D) -> Paint<D> {
31
    Paint::green(msg)
32
}
33
34
/// Returns faint neutral text.
35
pub fn faint<D: fmt::Display>(msg: D) -> Paint<D> {
36
    Paint::new(msg).fg(FAINT_COLOR)
37
}
38
39
/// Returns emphasized highlighted text.
40
pub fn highlight<D: fmt::Debug + fmt::Display>(input: D) -> Paint<D> {
41
    Paint::green(input).bold()
42
}
43
44
/// Returns bold text.
45
pub fn bold<D: fmt::Display>(input: D) -> Paint<D> {
46
    Paint::new(input).bold()
47
}
48
49
/// Returns dim text.
50
pub fn dim<D: fmt::Display>(input: D) -> Paint<D> {
51
    Paint::new(input).dim()
52
}
53
54
/// Returns dim italic text.
55
pub fn italic<D: fmt::Display>(input: D) -> Paint<D> {
56
    Paint::new(input).italic().dim()
57
}
58
59
/// Returns text styled as an inverted badge.
60
pub fn badge(text: impl fmt::Display, color: Color) -> Paint<String> {
61
    Paint::new(format!(" {text} ")).with_style(Style::new(color).invert())
62
}
63
64
#[cfg(test)]
65
#[allow(clippy::unwrap_used)]
66
mod tests {
67
    use super::*;
68
    use crate::Color;
69
70
    fn prefix<D: std::fmt::Display>(paint: Paint<D>) -> String {
71
        let mut out = String::new();
72
        paint.style().fmt_prefix(&mut out).unwrap();
73
        out
74
    }
75
76
    #[test]
77
    fn text_constructors_apply_expected_foreground_colors() {
78
        assert_eq!(default("x").style().fg_color(), Color::Unset);
79
        assert_eq!(wrap("x").style().fg_color(), Color::Unset);
80
        assert!(wrap("x").style().is_wrapping());
81
        assert_eq!(negative("x").style().fg_color(), Color::Red);
82
        assert_eq!(positive("x").style().fg_color(), Color::Green);
83
        assert_eq!(faint("x").style().fg_color(), Color::Fixed(236));
84
    }
85
86
    #[test]
87
    fn emphasis_constructors_set_style_properties() {
88
        assert_eq!(prefix(highlight("x")), "\x1B[1;32m");
89
        assert_eq!(prefix(bold("x")), "\x1B[1m");
90
        assert_eq!(prefix(dim("x")), "\x1B[2m");
91
        assert_eq!(prefix(italic("x")), "\x1B[2;3m");
92
    }
93
94
    #[test]
95
    fn badge_returns_inverted_paint() {
96
        let badge = badge("new", Color::Magenta);
97
98
        assert_eq!(badge.content(), " new ");
99
        assert_eq!(badge.style().fg_color(), Color::Magenta);
100
        assert_eq!(badge.styled().to_string(), "\x1B[7;35m new \x1B[0m");
101
    }
102
}