1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
use regex::Regex; use std::collections::hash_map::RandomState; use std::collections::HashMap; use std::iter::FromIterator; const COLORS_2: usize = 1; const COLORS_16: usize = 4; const COLORS_256: usize = 8; const COLORS_16M: usize = 24; const CI_NAMES: [&'static str; 4] = ["TRAVIS", "CIRCLECI", "APPVEYOR", "GITLAB_CI"]; pub fn internal_get_color_depth(env: HashMap<String, Option<String>, RandomState>) -> usize { if let Some(e) = env.get("FORCE_COLOR") { if let Some(r) = e { match r.as_ref() { "" | "1" | "true" => { return COLORS_16; } "2" => { return COLORS_256; } "3" => { return COLORS_16M; } _ => { return COLORS_2; } } } } if env.get("NO_COLOR").is_some() { return COLORS_2; } if let Some(e) = env.get("TERM") { if e.as_ref().unwrap_or(&"".to_owned()) == "DUMB" { return COLORS_2; } } if let Some(_) = env.get("TMUX") { return COLORS_256; } if let Some(_) = env.get("CI") { for ci_name in CI_NAMES.iter() { if let Some(_) = env.get(ci_name.to_owned()) { return COLORS_256; } } if let Some(ci) = env.get("CI_NAME") { if ci.as_ref().unwrap_or(&"".to_owned()) == "codeship" { return COLORS_256; } } return COLORS_2; } if let Some(teamcity_version) = env.get("TEAMCITY_VERSION") { let re = Regex::new(r"^(9\.(0*[1-9]\d*)\.|\d{2,}\.)").unwrap(); if re.is_match(teamcity_version.as_ref().unwrap_or(&"".to_owned())) { return COLORS_16; } else { return COLORS_2; } } if let Some(term_program) = env.get("TERM_PROGRAM") { if let Some(program) = term_program { match program.as_ref() { "iTerm.app" => { match env.get("TERM_PROGRAM_VERSION") { None => { return COLORS_256; } Some(e) => { let re = Regex::new(r"^[0-2]\.").unwrap(); if re.is_match(e.as_ref().unwrap_or(&"".to_owned())) { return COLORS_256; } } } return COLORS_16M; } "HyperTerm" | "MacTerm" => return COLORS_16M, "Apple_Terminal" => return COLORS_256, _ => {} } } } if let Some(term_check) = env.get("TERM") { if let Some(term) = term_check { let xterm_test = Regex::new(r"^xterm-256").unwrap(); if xterm_test.is_match(term) { return COLORS_256; } let term_env = term.to_lowercase(); match term_env.as_ref() { "eterm" | "cons25" | "console" | "cygwin" | "dtterm" | "gnome" | "hurd" | "jfbterm" | "konsole" | "kterm" | "mlterm" | "putty" | "st" => { return COLORS_16; } "mosh" | "rxvt-unicode-24bit" | "terminator" => { return COLORS_16M; } _ => {} } let terms_with_16_bit_support: [Regex; 8] = [ Regex::new(r"ansi").unwrap(), Regex::new(r"ansi").unwrap(), Regex::new(r"ansi").unwrap(), Regex::new(r"ansi").unwrap(), Regex::new(r"ansi").unwrap(), Regex::new(r"ansi").unwrap(), Regex::new(r"ansi").unwrap(), Regex::new(r"ansi").unwrap(), ]; for term_regex in terms_with_16_bit_support.iter() { if term_regex.is_match(term_env.as_ref()) { return COLORS_16; } } } } if let Some(colorterm_raw) = env.get("COLORTERM") { if let Some(colorterm) = colorterm_raw { if colorterm == &"truecolor" || colorterm == &"24bit" { return COLORS_16M; } return COLORS_16; } } COLORS_2 } pub fn get_color_depth() -> usize { internal_get_color_depth(HashMap::from_iter(std::env::vars_os().map(|x| { let key = x .0 .clone() .into_string() .expect("Enviroment contains from unexpected characters."); let value = x.1.clone().into_string().ok(); return (key, value); }))) } pub fn has_colors(depth: usize) -> bool { depth <= 2usize.pow(get_color_depth() as u32) }