summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2022-11-21 03:34:33 -0500
committerGitHub <noreply@github.com>2022-11-21 03:34:33 -0500
commitdb07246f6758f2b4c222f7f9e0a7bf606deabb54 (patch)
tree0f332b8b9e8a65337d2beae1c90e72ae7e2a50fc /src
parent71bc6c940e880ea8c9801ac606055f1f55d516fe (diff)
other: don't use manual map for color name mapping (#908)
* other: don't use manual map for color name mapping I actually don't know why I was doing it like that before. This commit removes the phf crate, as it's not needed anymore. * update test
Diffstat (limited to 'src')
-rw-r--r--src/canvas/canvas_colours/colour_utils.rs124
-rw-r--r--src/utils/error.rs2
2 files changed, 86 insertions, 40 deletions
diff --git a/src/canvas/canvas_colours/colour_utils.rs b/src/canvas/canvas_colours/colour_utils.rs
index 4948d5c4..c5c4e294 100644
--- a/src/canvas/canvas_colours/colour_utils.rs
+++ b/src/canvas/canvas_colours/colour_utils.rs
@@ -10,27 +10,6 @@ pub const HIGHLIGHT_COLOUR: Color = Color::LightBlue;
pub const AVG_COLOUR: Color = Color::Red;
pub const ALL_COLOUR: Color = Color::Green;
-static COLOR_LOOKUP_TABLE: phf::Map<&'static str, Color> = phf::phf_map! {
- "reset" => Color::Reset,
- "black" => Color::Black,
- "red" => Color::Red,
- "green" => Color::Green,
- "yellow" => Color::Yellow,
- "blue" => Color::Blue,
- "magenta" => Color::Magenta,
- "cyan" => Color::Cyan,
- "gray" => Color::Gray,
- "grey" => Color::Gray,
- "darkgray" => Color::DarkGray,
- "lightred" => Color::LightRed,
- "lightgreen" => Color::LightGreen,
- "lightyellow" => Color::LightYellow,
- "lightblue" => Color::LightBlue,
- "lightmagenta" => Color::LightMagenta,
- "lightcyan" => Color::LightCyan,
- "white" => Color::White,
-};
-
pub fn convert_hex_to_color(hex: &str) -> error::Result<Color> {
fn hex_err(hex: &str) -> error::Result<u8> {
Err(
@@ -140,32 +119,99 @@ pub fn get_style_from_rgb(rgb_str: &str) -> error::Result<Style> {
}
fn convert_name_to_color(color_name: &str) -> error::Result<Color> {
- if let Some(color) = COLOR_LOOKUP_TABLE.get(color_name.to_lowercase().as_str()) {
- Ok(*color)
- } else {
- Err(error::BottomError::ConfigError(format!(
+ match color_name.to_lowercase().trim() {
+ "reset" => Ok(Color::Reset),
+ "black" => Ok(Color::Black),
+ "red" => Ok(Color::Red),
+ "green" => Ok(Color::Green),
+ "yellow" => Ok(Color::Yellow),
+ "blue" => Ok(Color::Blue),
+ "magenta" => Ok(Color::Magenta),
+ "cyan" => Ok(Color::Cyan),
+ "gray" | "grey" => Ok(Color::Gray),
+ "darkgray" | "darkgrey" | "dark gray" | "dark grey" => Ok(Color::DarkGray),
+ "lightred" | "light red" => Ok(Color::LightRed),
+ "lightgreen" | "light green" => Ok(Color::LightGreen),
+ "lightyellow" | "light yellow" => Ok(Color::LightYellow),
+ "lightblue" | "light blue" => Ok(Color::LightBlue),
+ "lightmagenta" | "light magenta" => Ok(Color::LightMagenta),
+ "lightcyan" | "light cyan" => Ok(Color::LightCyan),
+ "white" => Ok(Color::White),
+ _ => Err(error::BottomError::ConfigError(format!(
"\"{}\" is an invalid named colour.
The following are supported strings:
-+--------+------------+--------------+
-| Reset | Magenta | LightYellow |
-+--------+------------+--------------+
-| Black | Cyan | LightBlue |
-+--------+------------+--------------+
-| Red | Gray | LightMagenta |
-+--------+------------+--------------+
-| Green | DarkGray | LightCyan |
-+--------+------------+--------------+
-| Yellow | LightRed | White |
-+--------+------------+--------------+
-| Blue | LightGreen | |
-+--------+------------+--------------+
++--------+-------------+---------------------+
+| Reset | Magenta | Light Yellow |
++--------+-------------+---------------------+
+| Black | Cyan | Light Blue |
++--------+-------------+---------------------+
+| Red | Gray/Grey | Light Magenta |
++--------+-------------+---------------------+
+| Green | Light Cyan | Dark Gray/Dark Grey |
++--------+-------------+---------------------+
+| Yellow | Light Red | White |
++--------+-------------+---------------------+
+| Blue | Light Green | |
++--------+-------------+---------------------+
",
color_name
- )))
+ ))),
}
}
pub fn get_style_from_color_name(color_name: &str) -> error::Result<Style> {
Ok(Style::default().fg(convert_name_to_color(color_name)?))
}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn test_invalid_colours() {
+ // Test invalid spacing in single word.
+ assert!(convert_name_to_color("bl ack").is_err());
+
+ // Test invalid spacing in dual word.
+ assert!(convert_name_to_color("darkg ray").is_err());
+
+ // Test completely invalid colour.
+ assert!(convert_name_to_color("darkreset").is_err());
+ }
+
+ #[test]
+ fn test_valid_colours() {
+ // Standard color should work
+ assert_eq!(convert_name_to_color("red"), Ok(Color::Red));
+
+ // Capitalizing should be fine.
+ assert_eq!(convert_name_to_color("RED"), Ok(Color::Red));
+
+ // Spacing shouldn't be an issue now.
+ assert_eq!(convert_name_to_color(" red "), Ok(Color::Red));
+
+ // The following are all equivalent.
+ assert_eq!(convert_name_to_color("darkgray"), Ok(Color::DarkGray));
+ assert_eq!(convert_name_to_color("darkgrey"), Ok(Color::DarkGray));
+ assert_eq!(convert_name_to_color("dark grey"), Ok(Color::DarkGray));
+ assert_eq!(convert_name_to_color("dark gray"), Ok(Color::DarkGray));
+
+ assert_eq!(convert_name_to_color("grey"), Ok(Color::Gray));
+ assert_eq!(convert_name_to_color("gray"), Ok(Color::Gray));
+
+ // One more test with spacing.
+ assert_eq!(
+ convert_name_to_color(" lightmagenta "),
+ Ok(Color::LightMagenta)
+ );
+ assert_eq!(
+ convert_name_to_color("light magenta"),
+ Ok(Color::LightMagenta)
+ );
+ assert_eq!(
+ convert_name_to_color(" light magenta "),
+ Ok(Color::LightMagenta)
+ );
+ }
+}
diff --git a/src/utils/error.rs b/src/utils/error.rs
index fcb65e1b..904f1392 100644
--- a/src/utils/error.rs
+++ b/src/utils/error.rs
@@ -9,7 +9,7 @@ use procfs::ProcError;
pub type Result<T> = result::Result<T, BottomError>;
/// An error that can occur while Bottom runs.
-#[derive(Debug, Error)]
+#[derive(Debug, Error, PartialEq, Eq)]
pub enum BottomError {
/// An error when there is an IO exception.
#[error("IO exception, {0}")]