1 | //! ANSI constants. |
2 | |
3 | // pub const RESET: u8 = 0; |
4 | pub const BOLD: u8 = 1; |
5 | pub const DIM: u8 = 2; |
6 | pub const ITALIC: u8 = 3; |
7 | pub const UNDERLINE: u8 = 4; |
8 | pub const BLINK: u8 = 5; |
9 | pub const REVERSE: u8 = 7; |
10 | pub const CONCEAL: u8 = 8; |
11 | pub const STRIKE: u8 = 9; |
12 | pub const NO_BOLD: u8 = 22; |
13 | pub const NO_ITALIC: u8 = 23; |
14 | pub const NO_UNDERLINE: u8 = 24; |
15 | pub const NO_BLINK: u8 = 25; |
16 | pub const NO_REVERSE: u8 = 27; |
17 | pub const NO_CONCEAL: u8 = 28; |
18 | pub const NO_STRIKE: u8 = 29; |
19 | pub const SET_FOREGROUND_BASE: u8 = 30; |
20 | pub const SET_FOREGROUND: u8 = 38; |
21 | pub const DEFAULT_FOREGROUND: u8 = 39; |
22 | pub const SET_BACKGROUND_BASE: u8 = 40; |
23 | pub const SET_BACKGROUND: u8 = 48; |
24 | pub const DEFAULT_BACKGROUND: u8 = 49; |
25 | pub const SET_BRIGHT_FOREGROUND_BASE: u8 = 90; |
26 | pub const SET_BRIGHT_BACKGROUND_BASE: u8 = 100; |
27 | |
28 | /// Generate an SGR ANSI sequence. |
29 | pub fn generate_ansi_code(params: &[u8]) -> String { |
30 | let mut ansi_code: String = String::from(" \u{1b}[" ); |
31 | let mut first: bool = true; |
32 | for param: &u8 in params { |
33 | if first { |
34 | first = false; |
35 | } else { |
36 | ansi_code.push(ch:';' ); |
37 | } |
38 | ansi_code.push_str(&format!(" {}" , param)); |
39 | } |
40 | ansi_code.push(ch:'m' ); |
41 | ansi_code |
42 | } |
43 | |
44 | #[cfg (test)] |
45 | mod tests { |
46 | use super::*; |
47 | |
48 | #[test ] |
49 | fn ansi_code() { |
50 | assert_eq!(generate_ansi_code(&[0]), " \u{1b}[0m" ); |
51 | assert_eq!(generate_ansi_code(&[31]), " \u{1b}[31m" ); |
52 | assert_eq!(generate_ansi_code(&[38, 5, 1]), " \u{1b}[38;5;1m" ); |
53 | } |
54 | } |
55 | |