flower_pot/
lib.rs

1
2// Styles: 0-29.
3
4pub const RESET:                    &'static str = "\x1b[0m";
5pub const BOLD:                     &'static str = "\x1b[1m";
6pub const DIM:                      &'static str = "\x1b[2m";
7pub const ITALIC:                   &'static str = "\x1b[3m";
8pub const UNDERLINE:                &'static str = "\x1b[4m";
9pub const SLOW_BLINK:               &'static str = "\x1b[5m";
10pub const RAPID_BLINK:              &'static str = "\x1b[6m";
11pub const INVERTED:                 &'static str = "\x1b[7m";
12pub const HIDDEN:                   &'static str = "\x1b[8m";
13pub const STRIKETHROUGH:            &'static str = "\x1b[9m";
14pub const PRIMARY_FONT:             &'static str = "\x1b[10m";
15pub const ALT_FONT_1:               &'static str = "\x1b[11m";
16pub const ALT_FONT_2:               &'static str = "\x1b[12m";
17pub const ALT_FONT_3:               &'static str = "\x1b[13m";
18pub const ALT_FONT_4:               &'static str = "\x1b[14m";
19pub const ALT_FONT_5:               &'static str = "\x1b[15m";
20pub const ALT_FONT_6:               &'static str = "\x1b[16m";
21pub const ALT_FONT_7:               &'static str = "\x1b[17m";
22pub const ALT_FONT_8:               &'static str = "\x1b[18m";
23pub const ALT_FONT_9:               &'static str = "\x1b[19m";
24pub const FRAKTUR_FONT:             &'static str = "\x1b[20m";
25pub const DOUBLE_UNDERLINE:         &'static str = "\x1b[21m"; // identical because of competing standards
26pub const NOT_BOLD:                 &'static str = "\x1b[21m"; // identical because of competing standards
27pub const NORMAL_INTENSITY:         &'static str = "\x1b[22m";
28pub const NOT_ITALIC_NOT_BOLD:      &'static str = "\x1b[23m";
29pub const NOT_UNDERLINED:           &'static str = "\x1b[24m";
30pub const NOT_BLINKING:             &'static str = "\x1b[25m";
31pub const PROPORTIONAL_SPACING:     &'static str = "\x1b[26m";
32pub const NOT_INVERTED:             &'static str = "\x1b[27m";
33pub const NOT_HIDDEN:               &'static str = "\x1b[28m";
34pub const NOT_STRIKETHROUGH:        &'static str = "\x1b[29m";
35
36pub mod fg {
37
38    // Standard foreground colors: 30-37.
39
40    pub const BLACK:                &'static str = "\x1b[30m";
41    pub const RED:                  &'static str = "\x1b[31m";
42    pub const GREEN:                &'static str = "\x1b[32m";
43    pub const YELLOW:               &'static str = "\x1b[33m";
44    pub const BLUE:                 &'static str = "\x1b[34m";
45    pub const PURPLE:               &'static str = "\x1b[35m";
46    pub const CYAN:                 &'static str = "\x1b[36m";
47    pub const WHITE:                &'static str = "\x1b[37m";
48
49    // Fine-control foreground colors: 38.
50
51    pub fn color_256(n: u8) -> String {
52        format!("\x1b[38;5;{n}m")
53    }
54
55    pub fn truecolor(r: u8, g: u8, b: u8) -> String {
56        format!("\x1b[38;2;{r};{g};{b}m")
57    }
58
59    // Reset foreground color: 39.
60
61    pub const DEFAULT:              &'static str = "\x1b[39m";
62
63    // Bright foreground colors: 90-97 (discontinuous range).
64
65    pub const BRIGHT_BLACK:         &'static str = "\x1b[90m";
66    pub const BRIGHT_RED:           &'static str = "\x1b[91m";
67    pub const BRIGHT_GREEN:         &'static str = "\x1b[92m";
68    pub const BRIGHT_YELLOW:        &'static str = "\x1b[93m";
69    pub const BRIGHT_BLUE:          &'static str = "\x1b[94m";
70    pub const BRIGHT_PURPLE:        &'static str = "\x1b[95m";
71    pub const BRIGHT_CYAN:          &'static str = "\x1b[96m";
72    pub const BRIGHT_WHITE:         &'static str = "\x1b[97m";
73}
74
75pub mod bg {
76
77    // Standard background colors: 40-47.
78
79    pub const BLACK:                &'static str = "\x1b[40m";
80    pub const RED:                  &'static str = "\x1b[41m";
81    pub const GREEN:                &'static str = "\x1b[42m";
82    pub const YELLOW:               &'static str = "\x1b[43m";
83    pub const BLUE:                 &'static str = "\x1b[44m";
84    pub const PURPLE:               &'static str = "\x1b[45m";
85    pub const CYAN:                 &'static str = "\x1b[46m";
86    pub const WHITE:                &'static str = "\x1b[47m";
87
88    // Fine-control background colors: 48.
89
90    pub fn color_256(n: u8) -> String {
91        format!("\x1b[48;5;{n}m")
92    }
93
94    pub fn truecolor(r: u8, g: u8, b: u8) -> String {
95        format!("\x1b[48;2;{r};{g};{b}m")
96    }
97
98    // Reset background color: 49.
99
100    pub const DEFAULT:              &'static str = "\x1b[49m";
101
102    // Bright background colors: 100-107 (discontinuous range).
103
104    pub const BRIGHT_BLACK:         &'static str = "\x1b[100m";
105    pub const BRIGHT_RED:           &'static str = "\x1b[101m";
106    pub const BRIGHT_GREEN:         &'static str = "\x1b[102m";
107    pub const BRIGHT_YELLOW:        &'static str = "\x1b[103m";
108    pub const BRIGHT_BLUE:          &'static str = "\x1b[104m";
109    pub const BRIGHT_PURPLE:        &'static str = "\x1b[105m";
110    pub const BRIGHT_CYAN:          &'static str = "\x1b[106m";
111    pub const BRIGHT_WHITE:         &'static str = "\x1b[107m";
112}
113
114// Additional styles: 50-55.
115
116pub const NO_PROPORTIONAL_SPACING:  &'static str = "\x1b[50m";
117pub const FRAMED:                   &'static str = "\x1b[51m";
118pub const ENCIRCLED:                &'static str = "\x1b[52m";
119pub const OVERLINE:                 &'static str = "\x1b[53m";
120pub const NOT_FRAMED_NOT_ENCIRCLED: &'static str = "\x1b[54m";
121pub const NOT_OVERLINED:            &'static str = "\x1b[55m";
122
123// Test (requires manual inspection of outputs).
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128    use super::fg::*;
129
130    #[test]
131    fn print_and_verify_visually() {
132        println!();
133        println!("{GREEN}green{RESET}");
134        println!("{BOLD}{RED}BOLD RED{RESET}");
135        println!("normal {WHITE}white {BRIGHT_WHITE}bright white{RESET}");
136        println!("{BLUE}blue {BRIGHT_BLUE}bright blue{RESET}");
137        println!("normal {ITALIC}italic {BOLD}and bold {UNDERLINE}and underline{RESET}");
138        println!("normal {DOUBLE_UNDERLINE}double underline{RESET}");
139        println!("normal normal");
140        println!("normal {OVERLINE}overline{RESET}");
141        println!("normal {ENCIRCLED}encircled{RESET}");
142        println!("normal {FRAMED}framed{RESET}");
143        println!(
144            "{}g{}r{}e{}y{}s{}c{}a{}l{}e{} {}c{}o{}l{}o{}r{}s{}",
145            fg::color_256(232),
146            fg::color_256(235),
147            fg::color_256(238),
148            fg::color_256(241),
149            fg::color_256(244),
150            fg::color_256(247),
151            fg::color_256(250),
152            fg::color_256(253),
153            fg::color_256(255),
154            RESET,
155            bg::color_256(255),
156            bg::color_256(251),
157            bg::color_256(247),
158            bg::color_256(243),
159            bg::color_256(239),
160            bg::color_256(235),
161            RESET,
162        );
163        println!(
164            "{}r{}a{}i{}n{}b{}o{}w{}i{}c{} {}c{}o{}l{}o{}r{}s{}",
165            fg::color_256(132),
166            fg::color_256(135),
167            fg::color_256(138),
168            fg::color_256(141),
169            fg::color_256(144),
170            fg::color_256(147),
171            fg::color_256(150),
172            fg::color_256(153),
173            fg::color_256(155),
174            RESET,
175            bg::color_256(155),
176            bg::color_256(151),
177            bg::color_256(147),
178            bg::color_256(143),
179            bg::color_256(139),
180            bg::color_256(135),
181            RESET,
182        );
183
184        println!(
185            "{BOLD}{}t{}r{}u{}e{}c{}o{}l{}o{}r{} {BOLD}{}r{}a{}i{}n{}b{}o{}w{}",
186            fg::truecolor(255, 0, 0),
187            fg::truecolor(170, 0, 0),
188            fg::truecolor(90, 0, 0),
189            fg::truecolor(30, 0, 0),
190            fg::truecolor(0, 30, 0),
191            fg::truecolor(0, 90, 0),
192            fg::truecolor(0, 170, 0),
193            fg::truecolor(0, 200, 0),
194            fg::truecolor(0, 255, 0),
195            RESET,
196            fg::truecolor(0, 255, 0),
197            fg::truecolor(0, 150, 0),
198            fg::truecolor(0, 50, 0),
199            fg::truecolor(0, 0, 50),
200            fg::truecolor(0, 0, 150),
201            fg::truecolor(0, 0, 170),
202            fg::truecolor(0, 0, 255),
203            RESET,
204        );
205        println!("hidden: {HIDDEN}hidden{NOT_HIDDEN} revealed");
206        println!("{}green fg {}reset fg", fg::truecolor(0, 255, 0), fg::DEFAULT);
207        println!("{}green bg {}reset bg", bg::truecolor(0, 255, 0), bg::DEFAULT);
208        println!();
209    }
210}
211