1//! ANSI constants.
2
3// pub const RESET: u8 = 0;
4pub const BOLD: u8 = 1;
5pub const DIM: u8 = 2;
6pub const ITALIC: u8 = 3;
7pub const UNDERLINE: u8 = 4;
8pub const BLINK: u8 = 5;
9pub const REVERSE: u8 = 7;
10pub const CONCEAL: u8 = 8;
11pub const STRIKE: u8 = 9;
12pub const NO_BOLD: u8 = 22;
13pub const NO_ITALIC: u8 = 23;
14pub const NO_UNDERLINE: u8 = 24;
15pub const NO_BLINK: u8 = 25;
16pub const NO_REVERSE: u8 = 27;
17pub const NO_CONCEAL: u8 = 28;
18pub const NO_STRIKE: u8 = 29;
19pub const SET_FOREGROUND_BASE: u8 = 30;
20pub const SET_FOREGROUND: u8 = 38;
21pub const DEFAULT_FOREGROUND: u8 = 39;
22pub const SET_BACKGROUND_BASE: u8 = 40;
23pub const SET_BACKGROUND: u8 = 48;
24pub const DEFAULT_BACKGROUND: u8 = 49;
25pub const SET_BRIGHT_FOREGROUND_BASE: u8 = 90;
26pub const SET_BRIGHT_BACKGROUND_BASE: u8 = 100;
27
28/// Generate an SGR ANSI sequence.
29pub 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)]
45mod 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