mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Theme provider and '-t' and '-c' CLI options
This commit is contained in:
426
src/utils/fmt.rs
426
src/utils/fmt.rs
@@ -28,6 +28,7 @@
|
||||
use chrono::prelude::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, SystemTime};
|
||||
use tuirealm::tui::style::Color;
|
||||
|
||||
/// ### fmt_pex
|
||||
///
|
||||
@@ -149,6 +150,174 @@ pub fn fmt_path_elide(p: &Path, width: usize) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// ### fmt_color
|
||||
///
|
||||
/// Format color
|
||||
pub fn fmt_color(color: &Color) -> String {
|
||||
match color {
|
||||
Color::Black => "Black".to_string(),
|
||||
Color::Blue => "Blue".to_string(),
|
||||
Color::Cyan => "Cyan".to_string(),
|
||||
Color::DarkGray => "DarkGray".to_string(),
|
||||
Color::Gray => "Gray".to_string(),
|
||||
Color::Green => "Green".to_string(),
|
||||
Color::LightBlue => "LightBlue".to_string(),
|
||||
Color::LightCyan => "LightCyan".to_string(),
|
||||
Color::LightGreen => "LightGreen".to_string(),
|
||||
Color::LightMagenta => "LightMagenta".to_string(),
|
||||
Color::LightRed => "LightRed".to_string(),
|
||||
Color::LightYellow => "LightYellow".to_string(),
|
||||
Color::Magenta => "Magenta".to_string(),
|
||||
Color::Red => "Red".to_string(),
|
||||
Color::Reset => "Default".to_string(),
|
||||
Color::White => "White".to_string(),
|
||||
Color::Yellow => "Yellow".to_string(),
|
||||
Color::Indexed(_) => "Default".to_string(),
|
||||
// -- css colors
|
||||
Color::Rgb(240, 248, 255) => "aliceblue".to_string(),
|
||||
Color::Rgb(250, 235, 215) => "antiquewhite".to_string(),
|
||||
Color::Rgb(0, 255, 255) => "aqua".to_string(),
|
||||
Color::Rgb(127, 255, 212) => "aquamarine".to_string(),
|
||||
Color::Rgb(240, 255, 255) => "azure".to_string(),
|
||||
Color::Rgb(245, 245, 220) => "beige".to_string(),
|
||||
Color::Rgb(255, 228, 196) => "bisque".to_string(),
|
||||
Color::Rgb(0, 0, 0) => "black".to_string(),
|
||||
Color::Rgb(255, 235, 205) => "blanchedalmond".to_string(),
|
||||
Color::Rgb(0, 0, 255) => "blue".to_string(),
|
||||
Color::Rgb(138, 43, 226) => "blueviolet".to_string(),
|
||||
Color::Rgb(165, 42, 42) => "brown".to_string(),
|
||||
Color::Rgb(222, 184, 135) => "burlywood".to_string(),
|
||||
Color::Rgb(95, 158, 160) => "cadetblue".to_string(),
|
||||
Color::Rgb(127, 255, 0) => "chartreuse".to_string(),
|
||||
Color::Rgb(210, 105, 30) => "chocolate".to_string(),
|
||||
Color::Rgb(255, 127, 80) => "coral".to_string(),
|
||||
Color::Rgb(100, 149, 237) => "cornflowerblue".to_string(),
|
||||
Color::Rgb(255, 248, 220) => "cornsilk".to_string(),
|
||||
Color::Rgb(220, 20, 60) => "crimson".to_string(),
|
||||
Color::Rgb(0, 0, 139) => "darkblue".to_string(),
|
||||
Color::Rgb(0, 139, 139) => "darkcyan".to_string(),
|
||||
Color::Rgb(184, 134, 11) => "darkgoldenrod".to_string(),
|
||||
Color::Rgb(169, 169, 169) => "darkgray".to_string(),
|
||||
Color::Rgb(0, 100, 0) => "darkgreen".to_string(),
|
||||
Color::Rgb(189, 183, 107) => "darkkhaki".to_string(),
|
||||
Color::Rgb(139, 0, 139) => "darkmagenta".to_string(),
|
||||
Color::Rgb(85, 107, 47) => "darkolivegreen".to_string(),
|
||||
Color::Rgb(255, 140, 0) => "darkorange".to_string(),
|
||||
Color::Rgb(153, 50, 204) => "darkorchid".to_string(),
|
||||
Color::Rgb(139, 0, 0) => "darkred".to_string(),
|
||||
Color::Rgb(233, 150, 122) => "darksalmon".to_string(),
|
||||
Color::Rgb(143, 188, 143) => "darkseagreen".to_string(),
|
||||
Color::Rgb(72, 61, 139) => "darkslateblue".to_string(),
|
||||
Color::Rgb(47, 79, 79) => "darkslategray".to_string(),
|
||||
Color::Rgb(0, 206, 209) => "darkturquoise".to_string(),
|
||||
Color::Rgb(148, 0, 211) => "darkviolet".to_string(),
|
||||
Color::Rgb(255, 20, 147) => "deeppink".to_string(),
|
||||
Color::Rgb(0, 191, 255) => "deepskyblue".to_string(),
|
||||
Color::Rgb(105, 105, 105) => "dimgray".to_string(),
|
||||
Color::Rgb(30, 144, 255) => "dodgerblue".to_string(),
|
||||
Color::Rgb(178, 34, 34) => "firebrick".to_string(),
|
||||
Color::Rgb(255, 250, 240) => "floralwhite".to_string(),
|
||||
Color::Rgb(34, 139, 34) => "forestgreen".to_string(),
|
||||
Color::Rgb(255, 0, 255) => "fuchsia".to_string(),
|
||||
Color::Rgb(220, 220, 220) => "gainsboro".to_string(),
|
||||
Color::Rgb(248, 248, 255) => "ghostwhite".to_string(),
|
||||
Color::Rgb(255, 215, 0) => "gold".to_string(),
|
||||
Color::Rgb(218, 165, 32) => "goldenrod".to_string(),
|
||||
Color::Rgb(128, 128, 128) => "gray".to_string(),
|
||||
Color::Rgb(0, 128, 0) => "green".to_string(),
|
||||
Color::Rgb(173, 255, 47) => "greenyellow".to_string(),
|
||||
Color::Rgb(240, 255, 240) => "honeydew".to_string(),
|
||||
Color::Rgb(255, 105, 180) => "hotpink".to_string(),
|
||||
Color::Rgb(205, 92, 92) => "indianred".to_string(),
|
||||
Color::Rgb(75, 0, 130) => "indigo".to_string(),
|
||||
Color::Rgb(255, 255, 240) => "ivory".to_string(),
|
||||
Color::Rgb(240, 230, 140) => "khaki".to_string(),
|
||||
Color::Rgb(230, 230, 250) => "lavender".to_string(),
|
||||
Color::Rgb(255, 240, 245) => "lavenderblush".to_string(),
|
||||
Color::Rgb(124, 252, 0) => "lawngreen".to_string(),
|
||||
Color::Rgb(255, 250, 205) => "lemonchiffon".to_string(),
|
||||
Color::Rgb(173, 216, 230) => "lightblue".to_string(),
|
||||
Color::Rgb(240, 128, 128) => "lightcoral".to_string(),
|
||||
Color::Rgb(224, 255, 255) => "lightcyan".to_string(),
|
||||
Color::Rgb(250, 250, 210) => "lightgoldenrodyellow".to_string(),
|
||||
Color::Rgb(211, 211, 211) => "lightgray".to_string(),
|
||||
Color::Rgb(144, 238, 144) => "lightgreen".to_string(),
|
||||
Color::Rgb(255, 182, 193) => "lightpink".to_string(),
|
||||
Color::Rgb(255, 160, 122) => "lightsalmon".to_string(),
|
||||
Color::Rgb(32, 178, 170) => "lightseagreen".to_string(),
|
||||
Color::Rgb(135, 206, 250) => "lightskyblue".to_string(),
|
||||
Color::Rgb(119, 136, 153) => "lightslategray".to_string(),
|
||||
Color::Rgb(176, 196, 222) => "lightsteelblue".to_string(),
|
||||
Color::Rgb(255, 255, 224) => "lightyellow".to_string(),
|
||||
Color::Rgb(0, 255, 0) => "lime".to_string(),
|
||||
Color::Rgb(50, 205, 50) => "limegreen".to_string(),
|
||||
Color::Rgb(250, 240, 230) => "linen".to_string(),
|
||||
Color::Rgb(128, 0, 0) => "maroon".to_string(),
|
||||
Color::Rgb(102, 205, 170) => "mediumaquamarine".to_string(),
|
||||
Color::Rgb(0, 0, 205) => "mediumblue".to_string(),
|
||||
Color::Rgb(186, 85, 211) => "mediumorchid".to_string(),
|
||||
Color::Rgb(147, 112, 219) => "mediumpurple".to_string(),
|
||||
Color::Rgb(60, 179, 113) => "mediumseagreen".to_string(),
|
||||
Color::Rgb(123, 104, 238) => "mediumslateblue".to_string(),
|
||||
Color::Rgb(0, 250, 154) => "mediumspringgreen".to_string(),
|
||||
Color::Rgb(72, 209, 204) => "mediumturquoise".to_string(),
|
||||
Color::Rgb(199, 21, 133) => "mediumvioletred".to_string(),
|
||||
Color::Rgb(25, 25, 112) => "midnightblue".to_string(),
|
||||
Color::Rgb(245, 255, 250) => "mintcream".to_string(),
|
||||
Color::Rgb(255, 228, 225) => "mistyrose".to_string(),
|
||||
Color::Rgb(255, 228, 181) => "moccasin".to_string(),
|
||||
Color::Rgb(255, 222, 173) => "navajowhite".to_string(),
|
||||
Color::Rgb(0, 0, 128) => "navy".to_string(),
|
||||
Color::Rgb(253, 245, 230) => "oldlace".to_string(),
|
||||
Color::Rgb(128, 128, 0) => "olive".to_string(),
|
||||
Color::Rgb(107, 142, 35) => "olivedrab".to_string(),
|
||||
Color::Rgb(255, 165, 0) => "orange".to_string(),
|
||||
Color::Rgb(255, 69, 0) => "orangered".to_string(),
|
||||
Color::Rgb(218, 112, 214) => "orchid".to_string(),
|
||||
Color::Rgb(238, 232, 170) => "palegoldenrod".to_string(),
|
||||
Color::Rgb(152, 251, 152) => "palegreen".to_string(),
|
||||
Color::Rgb(175, 238, 238) => "paleturquoise".to_string(),
|
||||
Color::Rgb(219, 112, 147) => "palevioletred".to_string(),
|
||||
Color::Rgb(255, 239, 213) => "papayawhip".to_string(),
|
||||
Color::Rgb(255, 218, 185) => "peachpuff".to_string(),
|
||||
Color::Rgb(205, 133, 63) => "peru".to_string(),
|
||||
Color::Rgb(255, 192, 203) => "pink".to_string(),
|
||||
Color::Rgb(221, 160, 221) => "plum".to_string(),
|
||||
Color::Rgb(176, 224, 230) => "powderblue".to_string(),
|
||||
Color::Rgb(128, 0, 128) => "purple".to_string(),
|
||||
Color::Rgb(102, 51, 153) => "rebeccapurple".to_string(),
|
||||
Color::Rgb(255, 0, 0) => "red".to_string(),
|
||||
Color::Rgb(188, 143, 143) => "rosybrown".to_string(),
|
||||
Color::Rgb(65, 105, 225) => "royalblue".to_string(),
|
||||
Color::Rgb(139, 69, 19) => "saddlebrown".to_string(),
|
||||
Color::Rgb(250, 128, 114) => "salmon".to_string(),
|
||||
Color::Rgb(244, 164, 96) => "sandybrown".to_string(),
|
||||
Color::Rgb(46, 139, 87) => "seagreen".to_string(),
|
||||
Color::Rgb(255, 245, 238) => "seashell".to_string(),
|
||||
Color::Rgb(160, 82, 45) => "sienna".to_string(),
|
||||
Color::Rgb(192, 192, 192) => "silver".to_string(),
|
||||
Color::Rgb(135, 206, 235) => "skyblue".to_string(),
|
||||
Color::Rgb(106, 90, 205) => "slateblue".to_string(),
|
||||
Color::Rgb(112, 128, 144) => "slategray".to_string(),
|
||||
Color::Rgb(255, 250, 250) => "snow".to_string(),
|
||||
Color::Rgb(0, 255, 127) => "springgreen".to_string(),
|
||||
Color::Rgb(70, 130, 180) => "steelblue".to_string(),
|
||||
Color::Rgb(210, 180, 140) => "tan".to_string(),
|
||||
Color::Rgb(0, 128, 128) => "teal".to_string(),
|
||||
Color::Rgb(216, 191, 216) => "thistle".to_string(),
|
||||
Color::Rgb(255, 99, 71) => "tomato".to_string(),
|
||||
Color::Rgb(64, 224, 208) => "turquoise".to_string(),
|
||||
Color::Rgb(238, 130, 238) => "violet".to_string(),
|
||||
Color::Rgb(245, 222, 179) => "wheat".to_string(),
|
||||
Color::Rgb(255, 255, 255) => "white".to_string(),
|
||||
Color::Rgb(245, 245, 245) => "whitesmoke".to_string(),
|
||||
Color::Rgb(255, 255, 0) => "yellow".to_string(),
|
||||
Color::Rgb(154, 205, 50) => "yellowgreen".to_string(),
|
||||
// -- others
|
||||
Color::Rgb(r, g, b) => format!("#{:02x}{:02x}{:02x}", r, g, b),
|
||||
}
|
||||
}
|
||||
|
||||
/// ### shadow_password
|
||||
///
|
||||
/// Return a string with the same length of input string, but each character is replaced by '*'
|
||||
@@ -224,6 +393,263 @@ mod tests {
|
||||
assert_eq!(fmt_path_elide(p, 16), String::from("/develop/.../foo/bar"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utils_fmt_color() {
|
||||
assert_eq!(fmt_color(&Color::Black).as_str(), "Black");
|
||||
assert_eq!(fmt_color(&Color::Blue).as_str(), "Blue");
|
||||
assert_eq!(fmt_color(&Color::Cyan).as_str(), "Cyan");
|
||||
assert_eq!(fmt_color(&Color::DarkGray).as_str(), "DarkGray");
|
||||
assert_eq!(fmt_color(&Color::Gray).as_str(), "Gray");
|
||||
assert_eq!(fmt_color(&Color::Green).as_str(), "Green");
|
||||
assert_eq!(fmt_color(&Color::LightBlue).as_str(), "LightBlue");
|
||||
assert_eq!(fmt_color(&Color::LightCyan).as_str(), "LightCyan");
|
||||
assert_eq!(fmt_color(&Color::LightGreen).as_str(), "LightGreen");
|
||||
assert_eq!(fmt_color(&Color::LightMagenta).as_str(), "LightMagenta");
|
||||
assert_eq!(fmt_color(&Color::LightRed).as_str(), "LightRed");
|
||||
assert_eq!(fmt_color(&Color::LightYellow).as_str(), "LightYellow");
|
||||
assert_eq!(fmt_color(&Color::Magenta).as_str(), "Magenta");
|
||||
assert_eq!(fmt_color(&Color::Red).as_str(), "Red");
|
||||
assert_eq!(fmt_color(&Color::Reset).as_str(), "Default");
|
||||
assert_eq!(fmt_color(&Color::White).as_str(), "White");
|
||||
assert_eq!(fmt_color(&Color::Yellow).as_str(), "Yellow");
|
||||
assert_eq!(fmt_color(&Color::Indexed(16)).as_str(), "Default");
|
||||
assert_eq!(fmt_color(&Color::Rgb(204, 170, 22)).as_str(), "#ccaa16");
|
||||
assert_eq!(fmt_color(&Color::Rgb(204, 170, 0)).as_str(), "#ccaa00");
|
||||
// css colors
|
||||
assert_eq!(fmt_color(&Color::Rgb(240, 248, 255)).as_str(), "aliceblue");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(250, 235, 215)).as_str(),
|
||||
"antiquewhite"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 255, 255)).as_str(), "aqua");
|
||||
assert_eq!(fmt_color(&Color::Rgb(127, 255, 212)).as_str(), "aquamarine");
|
||||
assert_eq!(fmt_color(&Color::Rgb(240, 255, 255)).as_str(), "azure");
|
||||
assert_eq!(fmt_color(&Color::Rgb(245, 245, 220)).as_str(), "beige");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 228, 196)).as_str(), "bisque");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 0, 0)).as_str(), "black");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(255, 235, 205)).as_str(),
|
||||
"blanchedalmond"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 0, 255)).as_str(), "blue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(138, 43, 226)).as_str(), "blueviolet");
|
||||
assert_eq!(fmt_color(&Color::Rgb(165, 42, 42)).as_str(), "brown");
|
||||
assert_eq!(fmt_color(&Color::Rgb(222, 184, 135)).as_str(), "burlywood");
|
||||
assert_eq!(fmt_color(&Color::Rgb(95, 158, 160)).as_str(), "cadetblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(127, 255, 0)).as_str(), "chartreuse");
|
||||
assert_eq!(fmt_color(&Color::Rgb(210, 105, 30)).as_str(), "chocolate");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 127, 80)).as_str(), "coral");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(100, 149, 237)).as_str(),
|
||||
"cornflowerblue"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 248, 220)).as_str(), "cornsilk");
|
||||
assert_eq!(fmt_color(&Color::Rgb(220, 20, 60)).as_str(), "crimson");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 0, 139)).as_str(), "darkblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 139, 139)).as_str(), "darkcyan");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(184, 134, 11)).as_str(),
|
||||
"darkgoldenrod"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(169, 169, 169)).as_str(), "darkgray");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 100, 0)).as_str(), "darkgreen");
|
||||
assert_eq!(fmt_color(&Color::Rgb(189, 183, 107)).as_str(), "darkkhaki");
|
||||
assert_eq!(fmt_color(&Color::Rgb(139, 0, 139)).as_str(), "darkmagenta");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(85, 107, 47)).as_str(),
|
||||
"darkolivegreen"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 140, 0)).as_str(), "darkorange");
|
||||
assert_eq!(fmt_color(&Color::Rgb(153, 50, 204)).as_str(), "darkorchid");
|
||||
assert_eq!(fmt_color(&Color::Rgb(139, 0, 0)).as_str(), "darkred");
|
||||
assert_eq!(fmt_color(&Color::Rgb(233, 150, 122)).as_str(), "darksalmon");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(143, 188, 143)).as_str(),
|
||||
"darkseagreen"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(72, 61, 139)).as_str(),
|
||||
"darkslateblue"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(47, 79, 79)).as_str(), "darkslategray");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(0, 206, 209)).as_str(),
|
||||
"darkturquoise"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(148, 0, 211)).as_str(), "darkviolet");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 20, 147)).as_str(), "deeppink");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 191, 255)).as_str(), "deepskyblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(105, 105, 105)).as_str(), "dimgray");
|
||||
assert_eq!(fmt_color(&Color::Rgb(30, 144, 255)).as_str(), "dodgerblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(178, 34, 34)).as_str(), "firebrick");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(255, 250, 240)).as_str(),
|
||||
"floralwhite"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(34, 139, 34)).as_str(), "forestgreen");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 0, 255)).as_str(), "fuchsia");
|
||||
assert_eq!(fmt_color(&Color::Rgb(220, 220, 220)).as_str(), "gainsboro");
|
||||
assert_eq!(fmt_color(&Color::Rgb(248, 248, 255)).as_str(), "ghostwhite");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 215, 0)).as_str(), "gold");
|
||||
assert_eq!(fmt_color(&Color::Rgb(218, 165, 32)).as_str(), "goldenrod");
|
||||
assert_eq!(fmt_color(&Color::Rgb(128, 128, 128)).as_str(), "gray");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 128, 0)).as_str(), "green");
|
||||
assert_eq!(fmt_color(&Color::Rgb(173, 255, 47)).as_str(), "greenyellow");
|
||||
assert_eq!(fmt_color(&Color::Rgb(240, 255, 240)).as_str(), "honeydew");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 105, 180)).as_str(), "hotpink");
|
||||
assert_eq!(fmt_color(&Color::Rgb(205, 92, 92)).as_str(), "indianred");
|
||||
assert_eq!(fmt_color(&Color::Rgb(75, 0, 130)).as_str(), "indigo");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 255, 240)).as_str(), "ivory");
|
||||
assert_eq!(fmt_color(&Color::Rgb(240, 230, 140)).as_str(), "khaki");
|
||||
assert_eq!(fmt_color(&Color::Rgb(230, 230, 250)).as_str(), "lavender");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(255, 240, 245)).as_str(),
|
||||
"lavenderblush"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(124, 252, 0)).as_str(), "lawngreen");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(255, 250, 205)).as_str(),
|
||||
"lemonchiffon"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(173, 216, 230)).as_str(), "lightblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(240, 128, 128)).as_str(), "lightcoral");
|
||||
assert_eq!(fmt_color(&Color::Rgb(224, 255, 255)).as_str(), "lightcyan");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(250, 250, 210)).as_str(),
|
||||
"lightgoldenrodyellow"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(211, 211, 211)).as_str(), "lightgray");
|
||||
assert_eq!(fmt_color(&Color::Rgb(144, 238, 144)).as_str(), "lightgreen");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 182, 193)).as_str(), "lightpink");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(255, 160, 122)).as_str(),
|
||||
"lightsalmon"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(32, 178, 170)).as_str(),
|
||||
"lightseagreen"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(135, 206, 250)).as_str(),
|
||||
"lightskyblue"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(119, 136, 153)).as_str(),
|
||||
"lightslategray"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(176, 196, 222)).as_str(),
|
||||
"lightsteelblue"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(255, 255, 224)).as_str(),
|
||||
"lightyellow"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 255, 0)).as_str(), "lime");
|
||||
assert_eq!(fmt_color(&Color::Rgb(50, 205, 50)).as_str(), "limegreen");
|
||||
assert_eq!(fmt_color(&Color::Rgb(250, 240, 230)).as_str(), "linen");
|
||||
assert_eq!(fmt_color(&Color::Rgb(128, 0, 0)).as_str(), "maroon");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(102, 205, 170)).as_str(),
|
||||
"mediumaquamarine"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 0, 205)).as_str(), "mediumblue");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(186, 85, 211)).as_str(),
|
||||
"mediumorchid"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(147, 112, 219)).as_str(),
|
||||
"mediumpurple"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(60, 179, 113)).as_str(),
|
||||
"mediumseagreen"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(123, 104, 238)).as_str(),
|
||||
"mediumslateblue"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(0, 250, 154)).as_str(),
|
||||
"mediumspringgreen"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(72, 209, 204)).as_str(),
|
||||
"mediumturquoise"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(199, 21, 133)).as_str(),
|
||||
"mediumvioletred"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(25, 25, 112)).as_str(), "midnightblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(245, 255, 250)).as_str(), "mintcream");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 228, 225)).as_str(), "mistyrose");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 228, 181)).as_str(), "moccasin");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(255, 222, 173)).as_str(),
|
||||
"navajowhite"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 0, 128)).as_str(), "navy");
|
||||
assert_eq!(fmt_color(&Color::Rgb(253, 245, 230)).as_str(), "oldlace");
|
||||
assert_eq!(fmt_color(&Color::Rgb(128, 128, 0)).as_str(), "olive");
|
||||
assert_eq!(fmt_color(&Color::Rgb(107, 142, 35)).as_str(), "olivedrab");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 165, 0)).as_str(), "orange");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 69, 0)).as_str(), "orangered");
|
||||
assert_eq!(fmt_color(&Color::Rgb(218, 112, 214)).as_str(), "orchid");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(238, 232, 170)).as_str(),
|
||||
"palegoldenrod"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(152, 251, 152)).as_str(), "palegreen");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(175, 238, 238)).as_str(),
|
||||
"paleturquoise"
|
||||
);
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(219, 112, 147)).as_str(),
|
||||
"palevioletred"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 239, 213)).as_str(), "papayawhip");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 218, 185)).as_str(), "peachpuff");
|
||||
assert_eq!(fmt_color(&Color::Rgb(205, 133, 63)).as_str(), "peru");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 192, 203)).as_str(), "pink");
|
||||
assert_eq!(fmt_color(&Color::Rgb(221, 160, 221)).as_str(), "plum");
|
||||
assert_eq!(fmt_color(&Color::Rgb(176, 224, 230)).as_str(), "powderblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(128, 0, 128)).as_str(), "purple");
|
||||
assert_eq!(
|
||||
fmt_color(&Color::Rgb(102, 51, 153)).as_str(),
|
||||
"rebeccapurple"
|
||||
);
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 0, 0)).as_str(), "red");
|
||||
assert_eq!(fmt_color(&Color::Rgb(188, 143, 143)).as_str(), "rosybrown");
|
||||
assert_eq!(fmt_color(&Color::Rgb(65, 105, 225)).as_str(), "royalblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(139, 69, 19)).as_str(), "saddlebrown");
|
||||
assert_eq!(fmt_color(&Color::Rgb(250, 128, 114)).as_str(), "salmon");
|
||||
assert_eq!(fmt_color(&Color::Rgb(244, 164, 96)).as_str(), "sandybrown");
|
||||
assert_eq!(fmt_color(&Color::Rgb(46, 139, 87)).as_str(), "seagreen");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 245, 238)).as_str(), "seashell");
|
||||
assert_eq!(fmt_color(&Color::Rgb(160, 82, 45)).as_str(), "sienna");
|
||||
assert_eq!(fmt_color(&Color::Rgb(192, 192, 192)).as_str(), "silver");
|
||||
assert_eq!(fmt_color(&Color::Rgb(135, 206, 235)).as_str(), "skyblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(106, 90, 205)).as_str(), "slateblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(112, 128, 144)).as_str(), "slategray");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 250, 250)).as_str(), "snow");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 255, 127)).as_str(), "springgreen");
|
||||
assert_eq!(fmt_color(&Color::Rgb(70, 130, 180)).as_str(), "steelblue");
|
||||
assert_eq!(fmt_color(&Color::Rgb(210, 180, 140)).as_str(), "tan");
|
||||
assert_eq!(fmt_color(&Color::Rgb(0, 128, 128)).as_str(), "teal");
|
||||
assert_eq!(fmt_color(&Color::Rgb(216, 191, 216)).as_str(), "thistle");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 99, 71)).as_str(), "tomato");
|
||||
assert_eq!(fmt_color(&Color::Rgb(64, 224, 208)).as_str(), "turquoise");
|
||||
assert_eq!(fmt_color(&Color::Rgb(238, 130, 238)).as_str(), "violet");
|
||||
assert_eq!(fmt_color(&Color::Rgb(245, 222, 179)).as_str(), "wheat");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 255, 255)).as_str(), "white");
|
||||
assert_eq!(fmt_color(&Color::Rgb(245, 245, 245)).as_str(), "whitesmoke");
|
||||
assert_eq!(fmt_color(&Color::Rgb(255, 255, 0)).as_str(), "yellow");
|
||||
assert_eq!(fmt_color(&Color::Rgb(154, 205, 50)).as_str(), "yellowgreen");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utils_fmt_shadow_password() {
|
||||
assert_eq!(shadow_password("foobar"), String::from("******"));
|
||||
|
||||
@@ -39,6 +39,7 @@ use regex::Regex;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use tuirealm::tui::style::Color;
|
||||
|
||||
// Regex
|
||||
lazy_static! {
|
||||
@@ -58,6 +59,20 @@ lazy_static! {
|
||||
* v0.4.0 => 0.4.0
|
||||
*/
|
||||
static ref SEMVER_REGEX: Regex = Regex::new(r".*(:?[0-9]\.[0-9]\.[0-9])").unwrap();
|
||||
/**
|
||||
* Regex matches:
|
||||
* - group 1: Red
|
||||
* - group 2: Green
|
||||
* - group 3: Blue
|
||||
*/
|
||||
static ref COLOR_HEX_REGEX: Regex = Regex::new(r"#(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})").unwrap();
|
||||
/**
|
||||
* Regex matches:
|
||||
* - group 2: Red
|
||||
* - group 4: Green
|
||||
* - group 6: blue
|
||||
*/
|
||||
static ref COLOR_RGB_REGEX: Regex = Regex::new(r"^(rgb)?\(?([01]?\d\d?|2[0-4]\d|25[0-5])(\W+)([01]?\d\d?|2[0-4]\d|25[0-5])\W+(([01]?\d\d?|2[0-4]\d|25[0-5])\)?)").unwrap();
|
||||
}
|
||||
|
||||
pub struct RemoteOptions {
|
||||
@@ -219,6 +234,237 @@ pub fn parse_semver(haystack: &str) -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// ### parse_color
|
||||
///
|
||||
/// Parse color from string into a `Color` enum.
|
||||
///
|
||||
/// Color may be in different format:
|
||||
///
|
||||
/// 1. color name:
|
||||
/// - Black,
|
||||
/// - Blue,
|
||||
/// - Cyan,
|
||||
/// - DarkGray,
|
||||
/// - Gray,
|
||||
/// - Green,
|
||||
/// - LightBlue,
|
||||
/// - LightCyan,
|
||||
/// - LightGreen,
|
||||
/// - LightMagenta,
|
||||
/// - LightRed,
|
||||
/// - LightYellow,
|
||||
/// - Magenta,
|
||||
/// - Red,
|
||||
/// - Reset,
|
||||
/// - White,
|
||||
/// - Yellow,
|
||||
/// 2. Hex format:
|
||||
/// - #f0ab05
|
||||
/// - #AA33BC
|
||||
/// 3. Rgb format:
|
||||
/// - rgb(255, 64, 32)
|
||||
/// - rgb(255,64,32)
|
||||
/// - 255, 64, 32
|
||||
pub fn parse_color(color: &str) -> Option<Color> {
|
||||
match color.to_lowercase().as_str() {
|
||||
// -- lib colors
|
||||
"black" => Some(Color::Black),
|
||||
"blue" => Some(Color::Blue),
|
||||
"cyan" => Some(Color::Cyan),
|
||||
"darkgray" | "darkgrey" => Some(Color::DarkGray),
|
||||
"default" => Some(Color::Reset),
|
||||
"gray" => Some(Color::Gray),
|
||||
"green" => Some(Color::Green),
|
||||
"lightblue" => Some(Color::LightBlue),
|
||||
"lightcyan" => Some(Color::LightCyan),
|
||||
"lightgreen" => Some(Color::LightGreen),
|
||||
"lightmagenta" => Some(Color::LightMagenta),
|
||||
"lightred" => Some(Color::LightRed),
|
||||
"lightyellow" => Some(Color::LightYellow),
|
||||
"magenta" => Some(Color::Magenta),
|
||||
"red" => Some(Color::Red),
|
||||
"white" => Some(Color::White),
|
||||
"yellow" => Some(Color::Yellow),
|
||||
// -- css colors
|
||||
"aliceblue" => Some(Color::Rgb(240, 248, 255)),
|
||||
"antiquewhite" => Some(Color::Rgb(250, 235, 215)),
|
||||
"aqua" => Some(Color::Rgb(0, 255, 255)),
|
||||
"aquamarine" => Some(Color::Rgb(127, 255, 212)),
|
||||
"azure" => Some(Color::Rgb(240, 255, 255)),
|
||||
"beige" => Some(Color::Rgb(245, 245, 220)),
|
||||
"bisque" => Some(Color::Rgb(255, 228, 196)),
|
||||
"blanchedalmond" => Some(Color::Rgb(255, 235, 205)),
|
||||
"blueviolet" => Some(Color::Rgb(138, 43, 226)),
|
||||
"brown" => Some(Color::Rgb(165, 42, 42)),
|
||||
"burlywood" => Some(Color::Rgb(222, 184, 135)),
|
||||
"cadetblue" => Some(Color::Rgb(95, 158, 160)),
|
||||
"chartreuse" => Some(Color::Rgb(127, 255, 0)),
|
||||
"chocolate" => Some(Color::Rgb(210, 105, 30)),
|
||||
"coral" => Some(Color::Rgb(255, 127, 80)),
|
||||
"cornflowerblue" => Some(Color::Rgb(100, 149, 237)),
|
||||
"cornsilk" => Some(Color::Rgb(255, 248, 220)),
|
||||
"crimson" => Some(Color::Rgb(220, 20, 60)),
|
||||
"darkblue" => Some(Color::Rgb(0, 0, 139)),
|
||||
"darkcyan" => Some(Color::Rgb(0, 139, 139)),
|
||||
"darkgoldenrod" => Some(Color::Rgb(184, 134, 11)),
|
||||
"darkgreen" => Some(Color::Rgb(0, 100, 0)),
|
||||
"darkkhaki" => Some(Color::Rgb(189, 183, 107)),
|
||||
"darkmagenta" => Some(Color::Rgb(139, 0, 139)),
|
||||
"darkolivegreen" => Some(Color::Rgb(85, 107, 47)),
|
||||
"darkorange" => Some(Color::Rgb(255, 140, 0)),
|
||||
"darkorchid" => Some(Color::Rgb(153, 50, 204)),
|
||||
"darkred" => Some(Color::Rgb(139, 0, 0)),
|
||||
"darksalmon" => Some(Color::Rgb(233, 150, 122)),
|
||||
"darkseagreen" => Some(Color::Rgb(143, 188, 143)),
|
||||
"darkslateblue" => Some(Color::Rgb(72, 61, 139)),
|
||||
"darkslategray" | "darkslategrey" => Some(Color::Rgb(47, 79, 79)),
|
||||
"darkturquoise" => Some(Color::Rgb(0, 206, 209)),
|
||||
"darkviolet" => Some(Color::Rgb(148, 0, 211)),
|
||||
"deeppink" => Some(Color::Rgb(255, 20, 147)),
|
||||
"deepskyblue" => Some(Color::Rgb(0, 191, 255)),
|
||||
"dimgray" | "dimgrey" => Some(Color::Rgb(105, 105, 105)),
|
||||
"dodgerblue" => Some(Color::Rgb(30, 144, 255)),
|
||||
"firebrick" => Some(Color::Rgb(178, 34, 34)),
|
||||
"floralwhite" => Some(Color::Rgb(255, 250, 240)),
|
||||
"forestgreen" => Some(Color::Rgb(34, 139, 34)),
|
||||
"fuchsia" => Some(Color::Rgb(255, 0, 255)),
|
||||
"gainsboro" => Some(Color::Rgb(220, 220, 220)),
|
||||
"ghostwhite" => Some(Color::Rgb(248, 248, 255)),
|
||||
"gold" => Some(Color::Rgb(255, 215, 0)),
|
||||
"goldenrod" => Some(Color::Rgb(218, 165, 32)),
|
||||
"greenyellow" => Some(Color::Rgb(173, 255, 47)),
|
||||
"grey" => Some(Color::Rgb(128, 128, 128)),
|
||||
"honeydew" => Some(Color::Rgb(240, 255, 240)),
|
||||
"hotpink" => Some(Color::Rgb(255, 105, 180)),
|
||||
"indianred" => Some(Color::Rgb(205, 92, 92)),
|
||||
"indigo" => Some(Color::Rgb(75, 0, 130)),
|
||||
"ivory" => Some(Color::Rgb(255, 255, 240)),
|
||||
"khaki" => Some(Color::Rgb(240, 230, 140)),
|
||||
"lavender" => Some(Color::Rgb(230, 230, 250)),
|
||||
"lavenderblush" => Some(Color::Rgb(255, 240, 245)),
|
||||
"lawngreen" => Some(Color::Rgb(124, 252, 0)),
|
||||
"lemonchiffon" => Some(Color::Rgb(255, 250, 205)),
|
||||
"lightcoral" => Some(Color::Rgb(240, 128, 128)),
|
||||
"lightgoldenrodyellow" => Some(Color::Rgb(250, 250, 210)),
|
||||
"lightgray" | "lightgrey" => Some(Color::Rgb(211, 211, 211)),
|
||||
"lightpink" => Some(Color::Rgb(255, 182, 193)),
|
||||
"lightsalmon" => Some(Color::Rgb(255, 160, 122)),
|
||||
"lightseagreen" => Some(Color::Rgb(32, 178, 170)),
|
||||
"lightskyblue" => Some(Color::Rgb(135, 206, 250)),
|
||||
"lightslategray" | "lightslategrey" => Some(Color::Rgb(119, 136, 153)),
|
||||
"lightsteelblue" => Some(Color::Rgb(176, 196, 222)),
|
||||
"lime" => Some(Color::Rgb(0, 255, 0)),
|
||||
"limegreen" => Some(Color::Rgb(50, 205, 50)),
|
||||
"linen" => Some(Color::Rgb(250, 240, 230)),
|
||||
"maroon" => Some(Color::Rgb(128, 0, 0)),
|
||||
"mediumaquamarine" => Some(Color::Rgb(102, 205, 170)),
|
||||
"mediumblue" => Some(Color::Rgb(0, 0, 205)),
|
||||
"mediumorchid" => Some(Color::Rgb(186, 85, 211)),
|
||||
"mediumpurple" => Some(Color::Rgb(147, 112, 219)),
|
||||
"mediumseagreen" => Some(Color::Rgb(60, 179, 113)),
|
||||
"mediumslateblue" => Some(Color::Rgb(123, 104, 238)),
|
||||
"mediumspringgreen" => Some(Color::Rgb(0, 250, 154)),
|
||||
"mediumturquoise" => Some(Color::Rgb(72, 209, 204)),
|
||||
"mediumvioletred" => Some(Color::Rgb(199, 21, 133)),
|
||||
"midnightblue" => Some(Color::Rgb(25, 25, 112)),
|
||||
"mintcream" => Some(Color::Rgb(245, 255, 250)),
|
||||
"mistyrose" => Some(Color::Rgb(255, 228, 225)),
|
||||
"moccasin" => Some(Color::Rgb(255, 228, 181)),
|
||||
"navajowhite" => Some(Color::Rgb(255, 222, 173)),
|
||||
"navy" => Some(Color::Rgb(0, 0, 128)),
|
||||
"oldlace" => Some(Color::Rgb(253, 245, 230)),
|
||||
"olive" => Some(Color::Rgb(128, 128, 0)),
|
||||
"olivedrab" => Some(Color::Rgb(107, 142, 35)),
|
||||
"orange" => Some(Color::Rgb(255, 165, 0)),
|
||||
"orangered" => Some(Color::Rgb(255, 69, 0)),
|
||||
"orchid" => Some(Color::Rgb(218, 112, 214)),
|
||||
"palegoldenrod" => Some(Color::Rgb(238, 232, 170)),
|
||||
"palegreen" => Some(Color::Rgb(152, 251, 152)),
|
||||
"paleturquoise" => Some(Color::Rgb(175, 238, 238)),
|
||||
"palevioletred" => Some(Color::Rgb(219, 112, 147)),
|
||||
"papayawhip" => Some(Color::Rgb(255, 239, 213)),
|
||||
"peachpuff" => Some(Color::Rgb(255, 218, 185)),
|
||||
"peru" => Some(Color::Rgb(205, 133, 63)),
|
||||
"pink" => Some(Color::Rgb(255, 192, 203)),
|
||||
"plum" => Some(Color::Rgb(221, 160, 221)),
|
||||
"powderblue" => Some(Color::Rgb(176, 224, 230)),
|
||||
"purple" => Some(Color::Rgb(128, 0, 128)),
|
||||
"rebeccapurple" => Some(Color::Rgb(102, 51, 153)),
|
||||
"rosybrown" => Some(Color::Rgb(188, 143, 143)),
|
||||
"royalblue" => Some(Color::Rgb(65, 105, 225)),
|
||||
"saddlebrown" => Some(Color::Rgb(139, 69, 19)),
|
||||
"salmon" => Some(Color::Rgb(250, 128, 114)),
|
||||
"sandybrown" => Some(Color::Rgb(244, 164, 96)),
|
||||
"seagreen" => Some(Color::Rgb(46, 139, 87)),
|
||||
"seashell" => Some(Color::Rgb(255, 245, 238)),
|
||||
"sienna" => Some(Color::Rgb(160, 82, 45)),
|
||||
"silver" => Some(Color::Rgb(192, 192, 192)),
|
||||
"skyblue" => Some(Color::Rgb(135, 206, 235)),
|
||||
"slateblue" => Some(Color::Rgb(106, 90, 205)),
|
||||
"slategray" | "slategrey" => Some(Color::Rgb(112, 128, 144)),
|
||||
"snow" => Some(Color::Rgb(255, 250, 250)),
|
||||
"springgreen" => Some(Color::Rgb(0, 255, 127)),
|
||||
"steelblue" => Some(Color::Rgb(70, 130, 180)),
|
||||
"tan" => Some(Color::Rgb(210, 180, 140)),
|
||||
"teal" => Some(Color::Rgb(0, 128, 128)),
|
||||
"thistle" => Some(Color::Rgb(216, 191, 216)),
|
||||
"tomato" => Some(Color::Rgb(255, 99, 71)),
|
||||
"turquoise" => Some(Color::Rgb(64, 224, 208)),
|
||||
"violet" => Some(Color::Rgb(238, 130, 238)),
|
||||
"wheat" => Some(Color::Rgb(245, 222, 179)),
|
||||
"whitesmoke" => Some(Color::Rgb(245, 245, 245)),
|
||||
"yellowgreen" => Some(Color::Rgb(154, 205, 50)),
|
||||
// -- hex and rgb
|
||||
other => {
|
||||
// Try as hex
|
||||
if let Some(color) = parse_hex_color(other) {
|
||||
Some(color)
|
||||
} else {
|
||||
parse_rgb_color(other)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ### parse_hex_color
|
||||
///
|
||||
/// Try to parse a color in hex format, such as:
|
||||
///
|
||||
/// - #f0ab05
|
||||
/// - #AA33BC
|
||||
fn parse_hex_color(color: &str) -> Option<Color> {
|
||||
COLOR_HEX_REGEX.captures(color).map(|groups| {
|
||||
Color::Rgb(
|
||||
u8::from_str_radix(groups.get(1).unwrap().as_str(), 16)
|
||||
.ok()
|
||||
.unwrap(),
|
||||
u8::from_str_radix(groups.get(2).unwrap().as_str(), 16)
|
||||
.ok()
|
||||
.unwrap(),
|
||||
u8::from_str_radix(groups.get(3).unwrap().as_str(), 16)
|
||||
.ok()
|
||||
.unwrap(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// ### parse_rgb_color
|
||||
///
|
||||
/// Try to parse a color in rgb format, such as:
|
||||
///
|
||||
/// - rgb(255, 64, 32)
|
||||
/// - rgb(255,64,32)
|
||||
/// - 255, 64, 32
|
||||
fn parse_rgb_color(color: &str) -> Option<Color> {
|
||||
COLOR_RGB_REGEX.captures(color).map(|groups| {
|
||||
Color::Rgb(
|
||||
u8::from_str(groups.get(2).unwrap().as_str()).ok().unwrap(),
|
||||
u8::from_str(groups.get(4).unwrap().as_str()).ok().unwrap(),
|
||||
u8::from_str(groups.get(6).unwrap().as_str()).ok().unwrap(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@@ -405,4 +651,245 @@ mod tests {
|
||||
assert_eq!(parse_semver("1.0.0").unwrap(), String::from("1.0.0"),);
|
||||
assert!(parse_semver("v1.1").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utils_parse_color_hex() {
|
||||
assert_eq!(
|
||||
parse_hex_color("#f0f0f0").unwrap(),
|
||||
Color::Rgb(240, 240, 240)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_hex_color("#60AAcc").unwrap(),
|
||||
Color::Rgb(96, 170, 204)
|
||||
);
|
||||
assert!(parse_hex_color("#fatboy").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utils_parse_color_rgb() {
|
||||
assert_eq!(
|
||||
parse_rgb_color("rgb(255, 64, 32)").unwrap(),
|
||||
Color::Rgb(255, 64, 32)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_rgb_color("rgb(255,64,32)").unwrap(),
|
||||
Color::Rgb(255, 64, 32)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_rgb_color("(255,64,32)").unwrap(),
|
||||
Color::Rgb(255, 64, 32)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_rgb_color("255,64,32").unwrap(),
|
||||
Color::Rgb(255, 64, 32)
|
||||
);
|
||||
assert!(parse_rgb_color("(300, 128, 512)").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utils_parse_color() {
|
||||
assert_eq!(parse_color("Black").unwrap(), Color::Black);
|
||||
assert_eq!(parse_color("BLUE").unwrap(), Color::Blue);
|
||||
assert_eq!(parse_color("Cyan").unwrap(), Color::Cyan);
|
||||
assert_eq!(parse_color("DarkGray").unwrap(), Color::DarkGray);
|
||||
assert_eq!(parse_color("Gray").unwrap(), Color::Gray);
|
||||
assert_eq!(parse_color("Green").unwrap(), Color::Green);
|
||||
assert_eq!(parse_color("LightBlue").unwrap(), Color::LightBlue);
|
||||
assert_eq!(parse_color("LightCyan").unwrap(), Color::LightCyan);
|
||||
assert_eq!(parse_color("LightGreen").unwrap(), Color::LightGreen);
|
||||
assert_eq!(parse_color("LightMagenta").unwrap(), Color::LightMagenta);
|
||||
assert_eq!(parse_color("LightRed").unwrap(), Color::LightRed);
|
||||
assert_eq!(parse_color("LightYellow").unwrap(), Color::LightYellow);
|
||||
assert_eq!(parse_color("Magenta").unwrap(), Color::Magenta);
|
||||
assert_eq!(parse_color("Red").unwrap(), Color::Red);
|
||||
assert_eq!(parse_color("Default").unwrap(), Color::Reset);
|
||||
assert_eq!(parse_color("White").unwrap(), Color::White);
|
||||
assert_eq!(parse_color("Yellow").unwrap(), Color::Yellow);
|
||||
assert_eq!(parse_color("#f0f0f0").unwrap(), Color::Rgb(240, 240, 240));
|
||||
// -- css colors
|
||||
assert_eq!(parse_color("aliceblue"), Some(Color::Rgb(240, 248, 255)));
|
||||
assert_eq!(parse_color("antiquewhite"), Some(Color::Rgb(250, 235, 215)));
|
||||
assert_eq!(parse_color("aqua"), Some(Color::Rgb(0, 255, 255)));
|
||||
assert_eq!(parse_color("aquamarine"), Some(Color::Rgb(127, 255, 212)));
|
||||
assert_eq!(parse_color("azure"), Some(Color::Rgb(240, 255, 255)));
|
||||
assert_eq!(parse_color("beige"), Some(Color::Rgb(245, 245, 220)));
|
||||
assert_eq!(parse_color("bisque"), Some(Color::Rgb(255, 228, 196)));
|
||||
assert_eq!(
|
||||
parse_color("blanchedalmond"),
|
||||
Some(Color::Rgb(255, 235, 205))
|
||||
);
|
||||
assert_eq!(parse_color("blueviolet"), Some(Color::Rgb(138, 43, 226)));
|
||||
assert_eq!(parse_color("brown"), Some(Color::Rgb(165, 42, 42)));
|
||||
assert_eq!(parse_color("burlywood"), Some(Color::Rgb(222, 184, 135)));
|
||||
assert_eq!(parse_color("cadetblue"), Some(Color::Rgb(95, 158, 160)));
|
||||
assert_eq!(parse_color("chartreuse"), Some(Color::Rgb(127, 255, 0)));
|
||||
assert_eq!(parse_color("chocolate"), Some(Color::Rgb(210, 105, 30)));
|
||||
assert_eq!(parse_color("coral"), Some(Color::Rgb(255, 127, 80)));
|
||||
assert_eq!(
|
||||
parse_color("cornflowerblue"),
|
||||
Some(Color::Rgb(100, 149, 237))
|
||||
);
|
||||
assert_eq!(parse_color("cornsilk"), Some(Color::Rgb(255, 248, 220)));
|
||||
assert_eq!(parse_color("crimson"), Some(Color::Rgb(220, 20, 60)));
|
||||
assert_eq!(parse_color("darkblue"), Some(Color::Rgb(0, 0, 139)));
|
||||
assert_eq!(parse_color("darkcyan"), Some(Color::Rgb(0, 139, 139)));
|
||||
assert_eq!(parse_color("darkgoldenrod"), Some(Color::Rgb(184, 134, 11)));
|
||||
assert_eq!(parse_color("darkgreen"), Some(Color::Rgb(0, 100, 0)));
|
||||
assert_eq!(parse_color("darkkhaki"), Some(Color::Rgb(189, 183, 107)));
|
||||
assert_eq!(parse_color("darkmagenta"), Some(Color::Rgb(139, 0, 139)));
|
||||
assert_eq!(parse_color("darkolivegreen"), Some(Color::Rgb(85, 107, 47)));
|
||||
assert_eq!(parse_color("darkorange"), Some(Color::Rgb(255, 140, 0)));
|
||||
assert_eq!(parse_color("darkorchid"), Some(Color::Rgb(153, 50, 204)));
|
||||
assert_eq!(parse_color("darkred"), Some(Color::Rgb(139, 0, 0)));
|
||||
assert_eq!(parse_color("darksalmon"), Some(Color::Rgb(233, 150, 122)));
|
||||
assert_eq!(parse_color("darkseagreen"), Some(Color::Rgb(143, 188, 143)));
|
||||
assert_eq!(parse_color("darkslateblue"), Some(Color::Rgb(72, 61, 139)));
|
||||
assert_eq!(parse_color("darkslategray"), Some(Color::Rgb(47, 79, 79)));
|
||||
assert_eq!(parse_color("darkslategrey"), Some(Color::Rgb(47, 79, 79)));
|
||||
assert_eq!(parse_color("darkturquoise"), Some(Color::Rgb(0, 206, 209)));
|
||||
assert_eq!(parse_color("darkviolet"), Some(Color::Rgb(148, 0, 211)));
|
||||
assert_eq!(parse_color("deeppink"), Some(Color::Rgb(255, 20, 147)));
|
||||
assert_eq!(parse_color("deepskyblue"), Some(Color::Rgb(0, 191, 255)));
|
||||
assert_eq!(parse_color("dimgray"), Some(Color::Rgb(105, 105, 105)));
|
||||
assert_eq!(parse_color("dimgrey"), Some(Color::Rgb(105, 105, 105)));
|
||||
assert_eq!(parse_color("dodgerblue"), Some(Color::Rgb(30, 144, 255)));
|
||||
assert_eq!(parse_color("firebrick"), Some(Color::Rgb(178, 34, 34)));
|
||||
assert_eq!(parse_color("floralwhite"), Some(Color::Rgb(255, 250, 240)));
|
||||
assert_eq!(parse_color("forestgreen"), Some(Color::Rgb(34, 139, 34)));
|
||||
assert_eq!(parse_color("fuchsia"), Some(Color::Rgb(255, 0, 255)));
|
||||
assert_eq!(parse_color("gainsboro"), Some(Color::Rgb(220, 220, 220)));
|
||||
assert_eq!(parse_color("ghostwhite"), Some(Color::Rgb(248, 248, 255)));
|
||||
assert_eq!(parse_color("gold"), Some(Color::Rgb(255, 215, 0)));
|
||||
assert_eq!(parse_color("goldenrod"), Some(Color::Rgb(218, 165, 32)));
|
||||
assert_eq!(parse_color("greenyellow"), Some(Color::Rgb(173, 255, 47)));
|
||||
assert_eq!(parse_color("honeydew"), Some(Color::Rgb(240, 255, 240)));
|
||||
assert_eq!(parse_color("hotpink"), Some(Color::Rgb(255, 105, 180)));
|
||||
assert_eq!(parse_color("indianred"), Some(Color::Rgb(205, 92, 92)));
|
||||
assert_eq!(parse_color("indigo"), Some(Color::Rgb(75, 0, 130)));
|
||||
assert_eq!(parse_color("ivory"), Some(Color::Rgb(255, 255, 240)));
|
||||
assert_eq!(parse_color("khaki"), Some(Color::Rgb(240, 230, 140)));
|
||||
assert_eq!(parse_color("lavender"), Some(Color::Rgb(230, 230, 250)));
|
||||
assert_eq!(
|
||||
parse_color("lavenderblush"),
|
||||
Some(Color::Rgb(255, 240, 245))
|
||||
);
|
||||
assert_eq!(parse_color("lawngreen"), Some(Color::Rgb(124, 252, 0)));
|
||||
assert_eq!(parse_color("lemonchiffon"), Some(Color::Rgb(255, 250, 205)));
|
||||
assert_eq!(parse_color("lightcoral"), Some(Color::Rgb(240, 128, 128)));
|
||||
assert_eq!(
|
||||
parse_color("lightgoldenrodyellow"),
|
||||
Some(Color::Rgb(250, 250, 210))
|
||||
);
|
||||
assert_eq!(parse_color("lightpink"), Some(Color::Rgb(255, 182, 193)));
|
||||
assert_eq!(parse_color("lightsalmon"), Some(Color::Rgb(255, 160, 122)));
|
||||
assert_eq!(parse_color("lightseagreen"), Some(Color::Rgb(32, 178, 170)));
|
||||
assert_eq!(parse_color("lightskyblue"), Some(Color::Rgb(135, 206, 250)));
|
||||
assert_eq!(
|
||||
parse_color("lightslategray"),
|
||||
Some(Color::Rgb(119, 136, 153))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_color("lightslategrey"),
|
||||
Some(Color::Rgb(119, 136, 153))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_color("lightsteelblue"),
|
||||
Some(Color::Rgb(176, 196, 222))
|
||||
);
|
||||
assert_eq!(parse_color("lime"), Some(Color::Rgb(0, 255, 0)));
|
||||
assert_eq!(parse_color("limegreen"), Some(Color::Rgb(50, 205, 50)));
|
||||
assert_eq!(parse_color("linen"), Some(Color::Rgb(250, 240, 230)));
|
||||
assert_eq!(parse_color("maroon"), Some(Color::Rgb(128, 0, 0)));
|
||||
assert_eq!(
|
||||
parse_color("mediumaquamarine"),
|
||||
Some(Color::Rgb(102, 205, 170))
|
||||
);
|
||||
assert_eq!(parse_color("mediumblue"), Some(Color::Rgb(0, 0, 205)));
|
||||
assert_eq!(parse_color("mediumorchid"), Some(Color::Rgb(186, 85, 211)));
|
||||
assert_eq!(parse_color("mediumpurple"), Some(Color::Rgb(147, 112, 219)));
|
||||
assert_eq!(
|
||||
parse_color("mediumseagreen"),
|
||||
Some(Color::Rgb(60, 179, 113))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_color("mediumslateblue"),
|
||||
Some(Color::Rgb(123, 104, 238))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_color("mediumspringgreen"),
|
||||
Some(Color::Rgb(0, 250, 154))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_color("mediumturquoise"),
|
||||
Some(Color::Rgb(72, 209, 204))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_color("mediumvioletred"),
|
||||
Some(Color::Rgb(199, 21, 133))
|
||||
);
|
||||
assert_eq!(parse_color("midnightblue"), Some(Color::Rgb(25, 25, 112)));
|
||||
assert_eq!(parse_color("mintcream"), Some(Color::Rgb(245, 255, 250)));
|
||||
assert_eq!(parse_color("mistyrose"), Some(Color::Rgb(255, 228, 225)));
|
||||
assert_eq!(parse_color("moccasin"), Some(Color::Rgb(255, 228, 181)));
|
||||
assert_eq!(parse_color("navajowhite"), Some(Color::Rgb(255, 222, 173)));
|
||||
assert_eq!(parse_color("navy"), Some(Color::Rgb(0, 0, 128)));
|
||||
assert_eq!(parse_color("oldlace"), Some(Color::Rgb(253, 245, 230)));
|
||||
assert_eq!(parse_color("olive"), Some(Color::Rgb(128, 128, 0)));
|
||||
assert_eq!(parse_color("olivedrab"), Some(Color::Rgb(107, 142, 35)));
|
||||
assert_eq!(parse_color("orange"), Some(Color::Rgb(255, 165, 0)));
|
||||
assert_eq!(parse_color("orangered"), Some(Color::Rgb(255, 69, 0)));
|
||||
assert_eq!(parse_color("orchid"), Some(Color::Rgb(218, 112, 214)));
|
||||
assert_eq!(
|
||||
parse_color("palegoldenrod"),
|
||||
Some(Color::Rgb(238, 232, 170))
|
||||
);
|
||||
assert_eq!(parse_color("palegreen"), Some(Color::Rgb(152, 251, 152)));
|
||||
assert_eq!(
|
||||
parse_color("paleturquoise"),
|
||||
Some(Color::Rgb(175, 238, 238))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_color("palevioletred"),
|
||||
Some(Color::Rgb(219, 112, 147))
|
||||
);
|
||||
assert_eq!(parse_color("papayawhip"), Some(Color::Rgb(255, 239, 213)));
|
||||
assert_eq!(parse_color("peachpuff"), Some(Color::Rgb(255, 218, 185)));
|
||||
assert_eq!(parse_color("peru"), Some(Color::Rgb(205, 133, 63)));
|
||||
assert_eq!(parse_color("pink"), Some(Color::Rgb(255, 192, 203)));
|
||||
assert_eq!(parse_color("plum"), Some(Color::Rgb(221, 160, 221)));
|
||||
assert_eq!(parse_color("powderblue"), Some(Color::Rgb(176, 224, 230)));
|
||||
assert_eq!(parse_color("purple"), Some(Color::Rgb(128, 0, 128)));
|
||||
assert_eq!(parse_color("rebeccapurple"), Some(Color::Rgb(102, 51, 153)));
|
||||
assert_eq!(parse_color("rosybrown"), Some(Color::Rgb(188, 143, 143)));
|
||||
assert_eq!(parse_color("royalblue"), Some(Color::Rgb(65, 105, 225)));
|
||||
assert_eq!(parse_color("saddlebrown"), Some(Color::Rgb(139, 69, 19)));
|
||||
assert_eq!(parse_color("salmon"), Some(Color::Rgb(250, 128, 114)));
|
||||
assert_eq!(parse_color("sandybrown"), Some(Color::Rgb(244, 164, 96)));
|
||||
assert_eq!(parse_color("seagreen"), Some(Color::Rgb(46, 139, 87)));
|
||||
assert_eq!(parse_color("seashell"), Some(Color::Rgb(255, 245, 238)));
|
||||
assert_eq!(parse_color("sienna"), Some(Color::Rgb(160, 82, 45)));
|
||||
assert_eq!(parse_color("silver"), Some(Color::Rgb(192, 192, 192)));
|
||||
assert_eq!(parse_color("skyblue"), Some(Color::Rgb(135, 206, 235)));
|
||||
assert_eq!(parse_color("slateblue"), Some(Color::Rgb(106, 90, 205)));
|
||||
assert_eq!(parse_color("slategray"), Some(Color::Rgb(112, 128, 144)));
|
||||
assert_eq!(parse_color("slategrey"), Some(Color::Rgb(112, 128, 144)));
|
||||
assert_eq!(parse_color("snow"), Some(Color::Rgb(255, 250, 250)));
|
||||
assert_eq!(parse_color("springgreen"), Some(Color::Rgb(0, 255, 127)));
|
||||
assert_eq!(parse_color("steelblue"), Some(Color::Rgb(70, 130, 180)));
|
||||
assert_eq!(parse_color("tan"), Some(Color::Rgb(210, 180, 140)));
|
||||
assert_eq!(parse_color("teal"), Some(Color::Rgb(0, 128, 128)));
|
||||
assert_eq!(parse_color("thistle"), Some(Color::Rgb(216, 191, 216)));
|
||||
assert_eq!(parse_color("tomato"), Some(Color::Rgb(255, 99, 71)));
|
||||
assert_eq!(parse_color("turquoise"), Some(Color::Rgb(64, 224, 208)));
|
||||
assert_eq!(parse_color("violet"), Some(Color::Rgb(238, 130, 238)));
|
||||
assert_eq!(parse_color("wheat"), Some(Color::Rgb(245, 222, 179)));
|
||||
assert_eq!(parse_color("whitesmoke"), Some(Color::Rgb(245, 245, 245)));
|
||||
assert_eq!(parse_color("yellowgreen"), Some(Color::Rgb(154, 205, 50)));
|
||||
// -- hex and rgb
|
||||
assert_eq!(
|
||||
parse_color("rgb(255, 64, 32)").unwrap(),
|
||||
Color::Rgb(255, 64, 32)
|
||||
);
|
||||
assert!(parse_color("redd").is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,6 +185,13 @@ pub fn make_fsentry(path: PathBuf, is_dir: bool) -> FsEntry {
|
||||
}
|
||||
}
|
||||
|
||||
/// ### create_file_ioers
|
||||
///
|
||||
/// Open a file with two handlers, the first is to read, the second is to write
|
||||
pub fn create_file_ioers(p: &Path) -> (File, File) {
|
||||
(File::open(p).ok().unwrap(), File::create(p).ok().unwrap())
|
||||
}
|
||||
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
@@ -245,4 +252,10 @@ mod test {
|
||||
assert!(make_dir_at(tmpdir.path(), "docs").is_ok());
|
||||
assert!(make_dir_at(PathBuf::from("/aaaaa/bbbbb/cccc").as_path(), "docs").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utils_test_helpers_create_file_ioers() {
|
||||
let (_, tmp) = create_sample_file_entry();
|
||||
let _ = create_file_ioers(tmp.path());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user