|
| 1 | +pub struct Color(u8, u8, u8); |
| 2 | + |
| 3 | +impl Color { |
| 4 | + pub fn from_rgb(rgb: &str) -> Result<Color, &'static str> { |
| 5 | + let parts: Vec<&str> = rgb.split(',').collect(); |
| 6 | + if parts.len() == 3 { |
| 7 | + let r = parts[0].parse::<u8>().map_err(|_| "Invalid RGB format")?; |
| 8 | + let g = parts[1].parse::<u8>().map_err(|_| "Invalid RGB format")?; |
| 9 | + let b = parts[2].parse::<u8>().map_err(|_| "Invalid RGB format")?; |
| 10 | + Ok(Color(r, g, b)) |
| 11 | + } else { |
| 12 | + Err("Invalid RGB format") |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + pub fn from_hex(hex: &str) -> Result<Color, &'static str> { |
| 17 | + if hex.len() != 7 || !hex.starts_with('#') { |
| 18 | + return Err("Invalid HEX format"); |
| 19 | + } |
| 20 | + |
| 21 | + let r = u8::from_str_radix(&hex[1..3], 16).map_err(|_| "Invalid HEX value")?; |
| 22 | + let g = u8::from_str_radix(&hex[3..5], 16).map_err(|_| "Invalid HEX value")?; |
| 23 | + let b = u8::from_str_radix(&hex[5..7], 16).map_err(|_| "Invalid HEX value")?; |
| 24 | + |
| 25 | + Ok(Color(r, g, b)) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub trait Colorize { |
| 30 | + fn color(self, color: &str) -> String; |
| 31 | + fn bg_color(self, color: &str) -> String; |
| 32 | + fn bold(self) -> String; |
| 33 | + fn italic(self) -> String; |
| 34 | + fn underline(self) -> String; |
| 35 | + fn strikethrough(self) -> String; |
| 36 | + fn dim(self) -> String; |
| 37 | + fn invert(self) -> String; |
| 38 | +} |
| 39 | + |
| 40 | +impl Colorize for &str { |
| 41 | + fn color(self, color: &str) -> String { |
| 42 | + let color = if color.starts_with('#') { |
| 43 | + Color::from_hex(color) |
| 44 | + } else { |
| 45 | + Color::from_rgb(color) |
| 46 | + }; |
| 47 | + |
| 48 | + match color { |
| 49 | + Ok(c) => format!("\x1b[38;2;{};{};{}m{}\x1b[0m", c.0, c.1, c.2, self), |
| 50 | + Err(_) => self.to_string(), |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + fn bg_color(self, color: &str) -> String { |
| 55 | + let color = if color.starts_with('#') { |
| 56 | + Color::from_hex(color) |
| 57 | + } else { |
| 58 | + Color::from_rgb(color) |
| 59 | + }; |
| 60 | + |
| 61 | + match color { |
| 62 | + Ok(c) => format!("\x1b[48;2;{};{};{}m{}\x1b[0m", c.0, c.1, c.2, self), |
| 63 | + Err(_) => self.to_string(), |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fn bold(self) -> String { |
| 68 | + format!("\x1b[1m{}\x1b[0m", self) |
| 69 | + } |
| 70 | + |
| 71 | + fn italic(self) -> String { |
| 72 | + format!("\x1b[3m{}\x1b[0m", self) |
| 73 | + } |
| 74 | + |
| 75 | + fn underline(self) -> String { |
| 76 | + format!("\x1b[4m{}\x1b[0m", self) |
| 77 | + } |
| 78 | + |
| 79 | + fn strikethrough(self) -> String { |
| 80 | + format!("\x1b[9m{}\x1b[0m", self) |
| 81 | + } |
| 82 | + |
| 83 | + fn dim(self) -> String { |
| 84 | + format!("\x1b[2m{}\x1b[0m", self) |
| 85 | + } |
| 86 | + |
| 87 | + fn invert(self) -> String { |
| 88 | + format!("\x1b[7m{}\x1b[0m", self) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
0 commit comments