orf / gping

Ping, but with a graph

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clean-up: is this `try_color_from_string` redundant?

TieWay59 opened this issue · comments

This function is involved in PR #156

fn try_color_from_string(string: &str) -> Result<Color> {
let mut characters = string.chars();
let color = if let Some('#') = characters.next() {
match rgb(&mut characters) {
Some([r, g, b]) => Color::Rgb(r, g, b),
None => return Err(anyhow!("Invalid color code: `{}`", string)),
}
} else {
use Color::*;
match string.to_lowercase().as_str() {
"black" => Black,
"red" => Red,
"green" => Green,
"yellow" => Yellow,
"blue" => Blue,
"magenta" => Magenta,
"cyan" => Cyan,
"gray" => Gray,
"dark-gray" => DarkGray,
"light-red" => LightRed,
"light-green" => LightGreen,
"light-yellow" => LightYellow,
"light-blue" => LightBlue,
"light-magenta" => LightMagenta,
"light-cyan" => LightCyan,
"white" => White,
invalid => return Err(anyhow!("Invalid color name: `{}`", invalid)),
}
};
Ok(color)
}

I believe this function is doing similar behavior of FromStr trait in the original ratatui lib. see:

https://docs.rs/ratatui/latest/src/ratatui/style.rs.html#288-326

https://github.com/tui-rs-revival/ratatui/blob/a1813af297058a141011f03870aed132f2912754/src/style.rs#L312-L377

And as we can see, ratatui has handled more cases and it can save a redundant dep of read_color. Is there any detail that urges us to keep it? I'd like to hear others' opinions.

WDUT @orf @petervaro ?

The difference in the error caption:

ratatui: Failed to parse Colors
gping: Invalid color code: `{}`

This error is easy to wrap & handle:

error => Some(error.map_err(|err| {
    anyhow!(err).context(format!("Invalid color code: `{}`", name))
})),
> ./gping www.google.com www.baidu.com -c "red,yello"                                       07/10/2023 03:32:12 PM
Error: Invalid color code: `yello`

Caused by:
    Failed to parse Colors

Or even simpler:

_ => Some(Err(anyhow!("Invalid color code: `{}`", name))),
> ./gping www.google.com www.baidu.com -c "red,yello"                                     1 07/10/2023 03:32:17 PM
Error: Invalid color code: `yello`

Which case would like @orf?

The difference in the color name conventions:

ratatui: "lightred" | "light red"
gping: "light-red"

Hey! Yeah I’m definitely in favour of doing this if possible. This would be a great clean up 🧹. This code was added before ratatui existed, and if they have the same functionality then we should use it.

Regarding the differences in name conventions, we could add a switch statement to convert “light-red” to “lightred” or make a PR to add these aliases to ratatui?

Hey! Yeah I’m definitely in favour of doing this if possible. This would be a great clean up broom. This code was added before ratatui existed, and if they have the same functionality then we should use it.

Regarding the differences in name conventions, we could add a switch statement to convert “light-red” to “lightred” or make a PR to add these aliases to ratatui?

I'm not sure because the naming convention may not be a standard for the framework, we can definitely work around it by replacing the -, and I'll try to ask for opinions from upstream~

Personally, I believe ratatui is not very mature in this part, because some same colors can have different expressions in their enum model. That would be way too much to discuss.