git_graph/print/
colors.rs

1//! ANSI terminal color handling.
2
3use lazy_static::lazy_static;
4use std::collections::HashMap;
5
6/// Converts a color name to the index in the 256-color palette.
7pub fn to_terminal_color(color: &str) -> Result<u8, String> {
8    match NAMED_COLORS.get(color) {
9        None => match color.parse::<u8>() {
10            Ok(col) => Ok(col),
11            Err(_) => Err(format!("Color {} not found", color)),
12        },
13        Some(rgb) => Ok(*rgb),
14    }
15}
16
17macro_rules! hashmap {
18    ($( $key: expr => $val: expr ),*) => {{
19         let mut map = ::std::collections::HashMap::new();
20         $( map.insert($key, $val); )*
21         map
22    }}
23}
24
25lazy_static! {
26    /// Named ANSI colors
27    pub static ref NAMED_COLORS: HashMap<&'static str, u8> = hashmap![
28        "black" => 0,
29        "red" => 1,
30        "green" => 2,
31        "yellow" => 3,
32        "blue" => 4,
33        "magenta" => 5,
34        "cyan" => 6,
35        "white" => 7,
36        "bright_black" => 8,
37        "bright_red" => 9,
38        "bright_green" => 10,
39        "bright_yellow" => 11,
40        "bright_blue" => 12,
41        "bright_magenta" => 13,
42        "bright_cyan" => 14,
43        "bright_white" => 15
44    ];
45}