-
Notifications
You must be signed in to change notification settings - Fork 93
Allocate a lot less #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Allocate a lot less #202
Changes from 22 commits
7b06a7b
1ad8d71
82fa2c2
19faeba
19eec9b
0c6f6c3
d540f3d
b217771
0ae0b78
57c53ea
ecd6355
098fa4f
34eaf1a
baf9453
1e04c4d
962a9ed
d5cd420
3e7932b
d2d4d93
383397e
362f667
2cd3492
432ebe7
aae4077
2497bd0
a122bea
5270a3c
a561d42
b0778d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| use std::{borrow::Cow, cmp, env, str::FromStr}; | ||
| use std::{borrow::Cow, env, str::FromStr}; | ||
| use Color::{ | ||
| Black, Blue, BrightBlack, BrightBlue, BrightCyan, BrightGreen, BrightMagenta, BrightRed, | ||
| BrightWhite, BrightYellow, Cyan, Green, Magenta, Red, TrueColor, White, Yellow, | ||
|
|
@@ -33,55 +33,88 @@ fn truecolor_support() -> bool { | |
|
|
||
| #[allow(missing_docs)] | ||
| impl Color { | ||
| const fn to_fg_static_str(self) -> Result<&'static str, (u8, u8, u8)> { | ||
| match self { | ||
| Self::Black => Ok("30"), | ||
| Self::Red => Ok("31"), | ||
| Self::Green => Ok("32"), | ||
| Self::Yellow => Ok("33"), | ||
| Self::Blue => Ok("34"), | ||
| Self::Magenta => Ok("35"), | ||
| Self::Cyan => Ok("36"), | ||
| Self::White => Ok("37"), | ||
| Self::BrightBlack => Ok("90"), | ||
| Self::BrightRed => Ok("91"), | ||
| Self::BrightGreen => Ok("92"), | ||
| Self::BrightYellow => Ok("93"), | ||
| Self::BrightBlue => Ok("94"), | ||
| Self::BrightMagenta => Ok("95"), | ||
| Self::BrightCyan => Ok("96"), | ||
| Self::BrightWhite => Ok("97"), | ||
| Self::TrueColor { r, g, b } => Err((r, g, b)), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn to_fg_fmt(self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { | ||
| match self.to_fg_static_str() { | ||
| Ok(s) => f.write_str(s), | ||
| Err((r, g, b)) if !truecolor_support() => Self::TrueColor { r, g, b } | ||
| .closest_color_euclidean() | ||
| .to_fg_fmt(f), | ||
| Err((r, g, b)) => write!(f, "38;2;{r};{g};{b}"), | ||
| } | ||
| } | ||
|
|
||
| #[must_use] | ||
| pub fn to_fg_str(&self) -> Cow<'static, str> { | ||
| match *self { | ||
| Self::Black => "30".into(), | ||
| Self::Red => "31".into(), | ||
| Self::Green => "32".into(), | ||
| Self::Yellow => "33".into(), | ||
| Self::Blue => "34".into(), | ||
| Self::Magenta => "35".into(), | ||
| Self::Cyan => "36".into(), | ||
| Self::White => "37".into(), | ||
| Self::BrightBlack => "90".into(), | ||
| Self::BrightRed => "91".into(), | ||
| Self::BrightGreen => "92".into(), | ||
| Self::BrightYellow => "93".into(), | ||
| Self::BrightBlue => "94".into(), | ||
| Self::BrightMagenta => "95".into(), | ||
| Self::BrightCyan => "96".into(), | ||
| Self::BrightWhite => "97".into(), | ||
| Self::TrueColor { .. } if !truecolor_support() => { | ||
| self.closest_color_euclidean().to_fg_str() | ||
| } | ||
| Self::TrueColor { r, g, b } => format!("38;2;{r};{g};{b}").into(), | ||
| match self.to_fg_static_str() { | ||
| Ok(s) => s.into(), | ||
| Err((r, g, b)) if !truecolor_support() => Self::TrueColor { r, g, b } | ||
| .closest_color_euclidean() | ||
| .to_fg_str(), | ||
| Err((r, g, b)) => format!("38;2;{r};{g};{b}").into(), | ||
| } | ||
| } | ||
| const fn to_bg_static_str(self) -> Result<&'static str, (u8, u8, u8)> { | ||
| match self { | ||
| Self::Black => Ok("40"), | ||
| Self::Red => Ok("41"), | ||
| Self::Green => Ok("42"), | ||
| Self::Yellow => Ok("43"), | ||
| Self::Blue => Ok("44"), | ||
| Self::Magenta => Ok("45"), | ||
| Self::Cyan => Ok("46"), | ||
| Self::White => Ok("47"), | ||
| Self::BrightBlack => Ok("100"), | ||
| Self::BrightRed => Ok("101"), | ||
| Self::BrightGreen => Ok("102"), | ||
| Self::BrightYellow => Ok("103"), | ||
| Self::BrightBlue => Ok("104"), | ||
| Self::BrightMagenta => Ok("105"), | ||
| Self::BrightCyan => Ok("106"), | ||
| Self::BrightWhite => Ok("107"), | ||
| Self::TrueColor { r, g, b } => Err((r, g, b)), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn to_bg_fmt(self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { | ||
| match self.to_bg_static_str() { | ||
| Ok(s) => f.write_str(s), | ||
| Err((r, g, b)) if !truecolor_support() => Self::TrueColor { r, g, b } | ||
| .closest_color_euclidean() | ||
| .to_fg_fmt(f), | ||
| Err((r, g, b)) => write!(f, "48;2;{r};{g};{b}"), | ||
| } | ||
| } | ||
|
|
||
| #[must_use] | ||
| pub fn to_bg_str(&self) -> Cow<'static, str> { | ||
| match *self { | ||
| Self::Black => "40".into(), | ||
| Self::Red => "41".into(), | ||
| Self::Green => "42".into(), | ||
| Self::Yellow => "43".into(), | ||
| Self::Blue => "44".into(), | ||
| Self::Magenta => "45".into(), | ||
| Self::Cyan => "46".into(), | ||
| Self::White => "47".into(), | ||
| Self::BrightBlack => "100".into(), | ||
| Self::BrightRed => "101".into(), | ||
| Self::BrightGreen => "102".into(), | ||
| Self::BrightYellow => "103".into(), | ||
| Self::BrightBlue => "104".into(), | ||
| Self::BrightMagenta => "105".into(), | ||
| Self::BrightCyan => "106".into(), | ||
| Self::BrightWhite => "107".into(), | ||
| Self::TrueColor { .. } if !truecolor_support() => { | ||
| self.closest_color_euclidean().to_bg_str() | ||
| } | ||
| Self::TrueColor { r, g, b } => format!("48;2;{r};{g};{b}").into(), | ||
| match self.to_bg_static_str() { | ||
| Ok(s) => s.into(), | ||
| Err((r, g, b)) if !truecolor_support() => Self::TrueColor { r, g, b } | ||
| .closest_color_euclidean() | ||
| .to_fg_str(), | ||
| Err((r, g, b)) => format!("48;2;{r};{g};{b}").into(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -93,7 +126,7 @@ impl Color { | |
| g: g1, | ||
| b: b1, | ||
| } => { | ||
| let colors = vec![ | ||
| let colors = [ | ||
| Black, | ||
| Red, | ||
| Green, | ||
|
|
@@ -115,19 +148,16 @@ impl Color { | |
| .map(|c| (c, c.into_truecolor())); | ||
| let distances = colors.map(|(c_original, c)| { | ||
| if let TrueColor { r, g, b } = c { | ||
| let rd = cmp::max(r, r1) - cmp::min(r, r1); | ||
| let gd = cmp::max(g, g1) - cmp::min(g, g1); | ||
| let bd = cmp::max(b, b1) - cmp::min(b, b1); | ||
| let rd: u32 = rd.into(); | ||
| let gd: u32 = gd.into(); | ||
| let bd: u32 = bd.into(); | ||
| let distance = rd.pow(2) + gd.pow(2) + bd.pow(2); | ||
| fn distance(a: u8, b: u8) -> u32 { | ||
| u32::from(a.abs_diff(b)).pow(2) | ||
| } | ||
| let distance = distance(r, r1) + distance(g, g1) + distance(b, b1); | ||
| (c_original, distance) | ||
| } else { | ||
| unimplemented!("{:?} not a TrueColor", c) | ||
| } | ||
| }); | ||
| distances.min_by(|(_, d1), (_, d2)| d1.cmp(d2)).unwrap().0 | ||
| distances.min_by_key(|(_, distance)| *distance).unwrap().0 | ||
|
||
| } | ||
| c => c, | ||
| } | ||
|
|
@@ -258,8 +288,83 @@ fn parse_hex(s: &str) -> Option<Color> { | |
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| pub use super::*; | ||
|
|
||
| #[test] | ||
| fn fmt_and_to_str_same() { | ||
| use core::fmt::Display; | ||
| use Color::*; | ||
|
|
||
| // Helper structs to call the method | ||
| struct FmtFgWrapper(Color); | ||
| impl Display for FmtFgWrapper { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| self.0.to_fg_fmt(f) | ||
| } | ||
| } | ||
| struct FmtBgWrapper(Color); | ||
| impl Display for FmtBgWrapper { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| self.0.to_bg_fmt(f) | ||
| } | ||
| } | ||
|
|
||
| // Actual test | ||
| let colors = &[ | ||
| Black, | ||
| Red, | ||
| Green, | ||
| Yellow, | ||
| Blue, | ||
| Magenta, | ||
| Cyan, | ||
| White, | ||
| BrightBlack, | ||
| BrightRed, | ||
| BrightGreen, | ||
| BrightYellow, | ||
| BrightBlue, | ||
| BrightMagenta, | ||
| BrightCyan, | ||
| BrightWhite, | ||
| TrueColor { r: 0, g: 0, b: 0 }, | ||
| TrueColor { | ||
| r: 255, | ||
| g: 255, | ||
| b: 255, | ||
| }, | ||
| TrueColor { | ||
| r: 126, | ||
| g: 127, | ||
| b: 128, | ||
| }, | ||
| TrueColor { r: 255, g: 0, b: 0 }, | ||
| TrueColor { | ||
| r: 255, | ||
| g: 255, | ||
| b: 0, | ||
| }, | ||
| TrueColor { r: 0, g: 255, b: 0 }, | ||
| TrueColor { | ||
| r: 0, | ||
| g: 255, | ||
| b: 255, | ||
| }, | ||
| TrueColor { r: 0, g: 0, b: 255 }, | ||
| TrueColor { | ||
| r: 255, | ||
| g: 0, | ||
| b: 255, | ||
| }, | ||
| ]; | ||
|
|
||
| for color in colors { | ||
| assert_eq!(color.to_fg_str(), FmtFgWrapper(*color).to_string()); | ||
| assert_eq!(color.to_bg_str(), FmtBgWrapper(*color).to_string()); | ||
| } | ||
| } | ||
|
|
||
| mod from_str { | ||
| pub use super::*; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you used
Resultas a clever way to "unwrap" the truecolor if it is one. Here's how I'm thinking it should be implemented instead, for clarity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intent was inspired by something like tokio's send method.
I try to convert the color in a static str. If it is possible, I return it, if not, I return rgb (since the color is not statically known).
The reason, I do not need a second match or any unreachable. I personally dislike the use of unreachable if it can be avoided easily.
Also, for borrowing the color, this is an internal api (so no stability needed) and Color is smaller than a pointer. So it is cheaper to hand over the color instead of an pointer. See this clippy::trivially_copy_pass_by_ref