1
2#![warn(missing_docs)]
3
4pub const RESET: &'static str = "\x1b[0m";
58pub const BOLD: &'static str = "\x1b[1m";
59pub const DIM: &'static str = "\x1b[2m";
60pub const ITALIC: &'static str = "\x1b[3m";
61pub const UNDERLINE: &'static str = "\x1b[4m";
62pub const SLOW_BLINK: &'static str = "\x1b[5m";
63pub const RAPID_BLINK: &'static str = "\x1b[6m";
64pub const INVERTED: &'static str = "\x1b[7m";
65pub const HIDDEN: &'static str = "\x1b[8m";
66pub const STRIKETHROUGH: &'static str = "\x1b[9m";
67pub const PRIMARY_FONT: &'static str = "\x1b[10m";
68pub const ALT_FONT_1: &'static str = "\x1b[11m";
69pub const ALT_FONT_2: &'static str = "\x1b[12m";
70pub const ALT_FONT_3: &'static str = "\x1b[13m";
71pub const ALT_FONT_4: &'static str = "\x1b[14m";
72pub const ALT_FONT_5: &'static str = "\x1b[15m";
73pub const ALT_FONT_6: &'static str = "\x1b[16m";
74pub const ALT_FONT_7: &'static str = "\x1b[17m";
75pub const ALT_FONT_8: &'static str = "\x1b[18m";
76pub const ALT_FONT_9: &'static str = "\x1b[19m";
77pub const FRAKTUR_FONT: &'static str = "\x1b[20m";
78pub const DOUBLE_UNDERLINE: &'static str = "\x1b[21m"; pub const NOT_BOLD: &'static str = "\x1b[21m"; pub const NORMAL_INTENSITY: &'static str = "\x1b[22m";
81pub const NOT_ITALIC_NOT_BOLD: &'static str = "\x1b[23m";
82pub const NOT_UNDERLINED: &'static str = "\x1b[24m";
83pub const NOT_BLINKING: &'static str = "\x1b[25m";
84pub const PROPORTIONAL_SPACING: &'static str = "\x1b[26m";
85pub const NOT_INVERTED: &'static str = "\x1b[27m";
86pub const NOT_HIDDEN: &'static str = "\x1b[28m";
87pub const NOT_STRIKETHROUGH: &'static str = "\x1b[29m";
88
89pub const BLACK: &'static str = "\x1b[30m";
92pub const RED: &'static str = "\x1b[31m";
93pub const GREEN: &'static str = "\x1b[32m";
94pub const YELLOW: &'static str = "\x1b[33m";
95pub const BLUE: &'static str = "\x1b[34m";
96pub const PURPLE: &'static str = "\x1b[35m";
97pub const CYAN: &'static str = "\x1b[36m";
98pub const WHITE: &'static str = "\x1b[37m";
99
100pub fn color_256(n: u8) -> String {
103 format!("\x1b[38;5;{n}m")
104}
105
106pub fn truecolor(r: u8, g: u8, b: u8) -> String {
107 format!("\x1b[38;2;{r};{g};{b}m")
108}
109
110pub const DEFAULT: &'static str = "\x1b[39m";
113
114pub const BLACK_BG: &'static str = "\x1b[40m";
117pub const RED_BG: &'static str = "\x1b[41m";
118pub const GREEN_BG: &'static str = "\x1b[42m";
119pub const YELLOW_BG: &'static str = "\x1b[43m";
120pub const BLUE_BG: &'static str = "\x1b[44m";
121pub const PURPLE_BG: &'static str = "\x1b[45m";
122pub const CYAN_BG: &'static str = "\x1b[46m";
123pub const WHITE_BG: &'static str = "\x1b[47m";
124
125pub fn color_256_bg(n: u8) -> String {
128 format!("\x1b[48;5;{n}m")
129}
130
131pub fn truecolor_bg(r: u8, g: u8, b: u8) -> String {
132 format!("\x1b[48;2;{r};{g};{b}m")
133}
134
135pub const DEFAULT_BG: &'static str = "\x1b[49m";
138
139pub const NO_PROPORTIONAL_SPACING: &'static str = "\x1b[50m";
142pub const FRAMED: &'static str = "\x1b[51m";
143pub const ENCIRCLED: &'static str = "\x1b[52m";
144pub const OVERLINE: &'static str = "\x1b[53m";
145pub const NOT_FRAMED_NOT_ENCIRCLED: &'static str = "\x1b[54m";
146pub const NOT_OVERLINED: &'static str = "\x1b[55m";
147
148pub const BRIGHT_BLACK: &'static str = "\x1b[90m";
151pub const BRIGHT_RED: &'static str = "\x1b[91m";
152pub const BRIGHT_GREEN: &'static str = "\x1b[92m";
153pub const BRIGHT_YELLOW: &'static str = "\x1b[93m";
154pub const BRIGHT_BLUE: &'static str = "\x1b[94m";
155pub const BRIGHT_PURPLE: &'static str = "\x1b[95m";
156pub const BRIGHT_CYAN: &'static str = "\x1b[96m";
157pub const BRIGHT_WHITE: &'static str = "\x1b[97m";
158
159pub const BRIGHT_BLACK_BG: &'static str = "\x1b[100m";
162pub const BRIGHT_RED_BG: &'static str = "\x1b[101m";
163pub const BRIGHT_GREEN_BG: &'static str = "\x1b[102m";
164pub const BRIGHT_YELLOW_BG: &'static str = "\x1b[103m";
165pub const BRIGHT_BLUE_BG: &'static str = "\x1b[104m";
166pub const BRIGHT_PURPLE_BG: &'static str = "\x1b[105m";
167pub const BRIGHT_CYAN_BG: &'static str = "\x1b[106m";
168pub const BRIGHT_WHITE_BG: &'static str = "\x1b[107m";
169
170#[cfg(test)]
173mod tests {
174 use super::*;
175
176 #[test]
177 fn print_and_verify_visually() {
178 println!();
179 println!("{GREEN}green{RESET}");
180 println!("{BOLD}{RED}BOLD RED{RESET}");
181 println!("normal {WHITE}white {BRIGHT_WHITE}bright white{RESET}");
182 println!("{BLUE}blue {BRIGHT_BLUE}bright blue{RESET}");
183 println!("normal {ITALIC}italic {BOLD}and bold {UNDERLINE}and underline{RESET}");
184 println!("normal {DOUBLE_UNDERLINE}double underline{RESET}");
185 println!("normal normal");
186 println!("normal {OVERLINE}overline{RESET}");
187 println!("normal {ENCIRCLED}encircled{RESET}");
188 println!("normal {FRAMED}framed{RESET}");
189 println!(
190 "{}g{}r{}e{}y{}s{}c{}a{}l{}e{} {}c{}o{}l{}o{}r{}s{}",
191 color_256(232),
192 color_256(235),
193 color_256(238),
194 color_256(241),
195 color_256(244),
196 color_256(247),
197 color_256(250),
198 color_256(253),
199 color_256(255),
200 RESET,
201 color_256_bg(255),
202 color_256_bg(251),
203 color_256_bg(247),
204 color_256_bg(243),
205 color_256_bg(239),
206 color_256_bg(235),
207 RESET,
208 );
209 println!(
210 "{}r{}a{}i{}n{}b{}o{}w{}i{}c{} {}c{}o{}l{}o{}r{}s{}",
211 color_256(132),
212 color_256(135),
213 color_256(138),
214 color_256(141),
215 color_256(144),
216 color_256(147),
217 color_256(150),
218 color_256(153),
219 color_256(155),
220 RESET,
221 color_256_bg(155),
222 color_256_bg(151),
223 color_256_bg(147),
224 color_256_bg(143),
225 color_256_bg(139),
226 color_256_bg(135),
227 RESET,
228 );
229
230 println!(
231 "{BOLD}{}t{}r{}u{}e{}c{}o{}l{}o{}r{} {BOLD}{}r{}a{}i{}n{}b{}o{}w{}",
232 truecolor(255, 0, 0),
233 truecolor(170, 0, 0),
234 truecolor(90, 0, 0),
235 truecolor(30, 0, 0),
236 truecolor(0, 30, 0),
237 truecolor(0, 90, 0),
238 truecolor(0, 170, 0),
239 truecolor(0, 200, 0),
240 truecolor(0, 255, 0),
241 RESET,
242 truecolor_bg(0, 255, 0),
243 truecolor_bg(0, 150, 0),
244 truecolor_bg(0, 50, 0),
245 truecolor_bg(0, 0, 50),
246 truecolor_bg(0, 0, 150),
247 truecolor_bg(0, 0, 170),
248 truecolor_bg(0, 0, 255),
249 RESET,
250 );
251 println!("hidden: {HIDDEN}hidden{NOT_HIDDEN} revealed");
252 println!("{}green fg {}reset fg", truecolor(0, 255, 0), DEFAULT);
253 println!("{}green bg {}reset bg", truecolor_bg(0, 255, 0), DEFAULT_BG);
254 println!();
255
256 println!("{GREEN}ok{RESET}"); println!("{BOLD}{RED}error!{RESET}"); println!("{BLUE_BG}cloud{RESET}"); println!();
260
261 println!("{}{}example text{RESET}", color_256(237), color_256_bg(214));
262 println!("{}{}truecolor text{RESET}", truecolor(127, 45, 68), truecolor_bg(0, 255, 255));
263 println!();
264 }
265}
266