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
This commit is contained in:
Clement Tsang 2022-11-21 03:34:33 -05:00 committed by GitHub
parent 71bc6c940e
commit db07246f67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 106 deletions

64
Cargo.lock generated
View file

@ -235,7 +235,6 @@ dependencies = [
"mach2",
"nvml-wrapper",
"once_cell",
"phf",
"predicates",
"procfs",
"regex",
@ -1290,48 +1289,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "phf"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "928c6535de93548188ef63bb7c4036bd415cd8f36ad25af44b9789b2ee72a48c"
dependencies = [
"phf_macros",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1181c94580fa345f50f19d738aaa39c0ed30a600d95cb2d3e23f94266f14fbf"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92aacdc5f16768709a569e913f7451034034178b05bdc8acda226659a3dccc66"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1fb5f6f826b772a8d4c0394209441e7d37cbbb967ae9c7e0e8134365c9ee676"
dependencies = [
"siphasher",
]
[[package]]
name = "pin-project-lite"
version = "0.2.9"
@ -1418,21 +1375,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rayon"
version = "1.5.2"
@ -1608,12 +1550,6 @@ dependencies = [
"libc",
]
[[package]]
name = "siphasher"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]]
name = "slab"
version = "0.4.6"

View file

@ -79,7 +79,6 @@ indexmap = "1.8.1"
itertools = "0.10.3"
kstring = { version = "2.0.0", features = ["arc"] }
log = { version = "0.4.16", optional = true }
phf = { version = "0.11", features = ["macros"] }
nvml-wrapper = { version = "0.8.0", optional = true }
once_cell = "1.5.2"
regex = "1.5.5"

View file

@ -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)
);
}
}

View file

@ -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}")]

View file

@ -1,2 +1,2 @@
[colors]
table_header_color="Light Blue"
table_header_color = "LightB lue"