Skip to content
Open
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7b06a7b
Replace compute_stlye with a Writer
tanculau Mar 26, 2025
1ad8d71
fmt
tanculau Mar 26, 2025
82fa2c2
Replace loop with insert_str
tanculau Mar 26, 2025
19faeba
Replaced escape_inner_reset_sequences
tanculau Mar 26, 2025
19eec9b
Fix tests
tanculau Mar 26, 2025
0c6f6c3
Cleanup
tanculau Mar 26, 2025
d540f3d
Change Helper Structs to Tuples
tanculau Mar 26, 2025
b217771
Padding without alloc
tanculau Mar 27, 2025
0ae0b78
Enhance formatting tests
tanculau Mar 27, 2025
57c53ea
Enhance formatting tests
tanculau Mar 27, 2025
ecd6355
Cleanup
tanculau Mar 27, 2025
098fa4f
Remove duplicate code
tanculau Mar 27, 2025
34eaf1a
Remove alloc in closest_color_euclidean
tanculau Mar 27, 2025
baf9453
Replace min_by with min_by_key
tanculau Mar 27, 2025
1e04c4d
Add comments
tanculau Mar 27, 2025
962a9ed
Refactor distance calculation in color comparison
tanculau Mar 27, 2025
d5cd420
Add tests to check if fmt and to_str are equal
tanculau Mar 27, 2025
3e7932b
Split up formatting test and added some
tanculau Mar 27, 2025
d2d4d93
Cleanup
tanculau Mar 27, 2025
383397e
Move tests to their modul
tanculau Mar 27, 2025
362f667
Typo
tanculau Mar 27, 2025
2cd3492
Enhance formatting, escape and color_fn tests
tanculau Mar 28, 2025
432ebe7
Remove allocations in Style
tanculau Apr 7, 2025
aae4077
Merge branch 'master' into less-alloc
tanculau Apr 7, 2025
2497bd0
Add doc comments & AnsiColor test
tanculau Apr 7, 2025
a122bea
Reworked to_static_str, Removed Helper structs, moved formatting in i…
tanculau Sep 5, 2025
5270a3c
Merge branch 'master' into less-alloc
tanculau Sep 5, 2025
a561d42
cargo fmt
tanculau Sep 5, 2025
b0778d0
Changed dyn to impl
tanculau Sep 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove duplicate code
  • Loading branch information
tanculau committed Mar 27, 2025
commit 098fa4f2ba31d23ad1d5bc7366ea7607d203ff3e
155 changes: 67 additions & 88 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,109 +33,88 @@ fn truecolor_support() -> bool {

#[allow(missing_docs)]
impl Color {
pub(crate) fn to_fg_fmt(self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
const fn to_fg_static_str(self) -> Result<&'static str, (u8, u8, u8)> {
match self {
Self::Black => f.write_str("30"),
Self::Red => f.write_str("31"),
Self::Green => f.write_str("32"),
Self::Yellow => f.write_str("33"),
Self::Blue => f.write_str("34"),
Self::Magenta => f.write_str("35"),
Self::Cyan => f.write_str("36"),
Self::White => f.write_str("37"),
Self::BrightBlack => f.write_str("90"),
Self::BrightRed => f.write_str("91"),
Self::BrightGreen => f.write_str("92"),
Self::BrightYellow => f.write_str("93"),
Self::BrightBlue => f.write_str("94"),
Self::BrightMagenta => f.write_str("95"),
Self::BrightCyan => f.write_str("96"),
Self::BrightWhite => f.write_str("97"),
Self::TrueColor { .. } if !truecolor_support() => {
self.closest_color_euclidean().to_fg_fmt(f)
}
Self::TrueColor { r, g, b } => {
write!(f, "38;2;{r};{g};{b}")
}
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 {
Self::Black => f.write_str("40"),
Self::Red => f.write_str("41"),
Self::Green => f.write_str("42"),
Self::Yellow => f.write_str("43"),
Self::Blue => f.write_str("44"),
Self::Magenta => f.write_str("45"),
Self::Cyan => f.write_str("46"),
Self::White => f.write_str("47"),
Self::BrightBlack => f.write_str("100"),
Self::BrightRed => f.write_str("101"),
Self::BrightGreen => f.write_str("102"),
Self::BrightYellow => f.write_str("103"),
Self::BrightBlue => f.write_str("104"),
Self::BrightMagenta => f.write_str("105"),
Self::BrightCyan => f.write_str("106"),
Self::BrightWhite => f.write_str("107"),
Self::TrueColor { .. } if !truecolor_support() => {
self.closest_color_euclidean().to_bg_fmt(f)
}
Self::TrueColor { r, g, b } => {
write!(f, "48;2;{r};{g};{b}")
}
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(),
}
}

Expand Down