diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-02-19 21:36:52 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-02-19 21:36:52 -0500 |
commit | 0820849c2b6793f035bfcf7d5cd22dfbe9b45c25 (patch) | |
tree | 508d5d7273f471f84437f81b0958be4116b366e9 | |
parent | 56f830f0b61cc47875bfbf4c0daad6fd5887a450 (diff) | |
download | fancy-prompt-0820849c2b6793f035bfcf7d5cd22dfbe9b45c25.tar.gz fancy-prompt-0820849c2b6793f035bfcf7d5cd22dfbe9b45c25.zip |
allow overriding colors
-rw-r--r-- | src/colors.rs | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/colors.rs b/src/colors.rs index 5049392..ff6ed86 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -8,8 +8,10 @@ pub enum ShellType { Zsh, } +type ColorMap = std::collections::HashMap<String, term::color::Color>; + pub struct Colors { - color_map: std::collections::HashMap<String, term::color::Color>, + color_map: ColorMap, unknown_color: term::color::Color, shell_type: ShellType, } @@ -39,6 +41,8 @@ impl Colors { let unknown_color = term::color::YELLOW; + Self::read_colors_from_env(&mut color_map); + Colors { color_map: color_map, unknown_color: unknown_color, @@ -46,6 +50,41 @@ impl Colors { } } + fn read_colors_from_env(color_map: &mut ColorMap) { + if let Ok(val) = std::env::var("FANCY_PROMPT_COLORS") { + for mapping in val.split(",") { + let parts: Vec<_> = mapping.split("=").collect(); + let (name, color) = (parts[0], parts[1]); + color_map.insert( + String::from(name), + Self::color_from_string(color) + ); + } + } + } + + fn color_from_string(color_name: &str) -> term::color::Color { + match color_name { + "black" => term::color::BLACK, + "blue" => term::color::BLUE, + "bright_black" => term::color::BRIGHT_BLACK, + "bright_blue" => term::color::BRIGHT_BLUE, + "bright_cyan" => term::color::BRIGHT_CYAN, + "bright_green" => term::color::BRIGHT_GREEN, + "bright_magenta" => term::color::BRIGHT_MAGENTA, + "bright_red" => term::color::BRIGHT_RED, + "bright_white" => term::color::BRIGHT_WHITE, + "bright_yellow" => term::color::BRIGHT_YELLOW, + "cyan" => term::color::CYAN, + "green" => term::color::GREEN, + "magenta" => term::color::MAGENTA, + "red" => term::color::RED, + "white" => term::color::WHITE, + "yellow" => term::color::YELLOW, + _ => panic!("unknown color {}", color_name), + } + } + pub fn print(&self, color: &str, text: &str) { let color = self.color_map.get(color); self.print_with_color(color, text); |