From 92ab43fc6f89632b57d938bb2986cf673aaa5e8b Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 2 Nov 2024 15:18:33 +0000 Subject: [PATCH] feat: add ansi to ansi_term feature --- build.rs | 1 + examples/term_grid.rs | 55 +++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 40 ++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/build.rs b/build.rs index 5772121..bf685bd 100644 --- a/build.rs +++ b/build.rs @@ -65,6 +65,7 @@ struct AnsiColorPair { bright: AnsiColor, } +// TODO: pull in the name of the group here (i.e. "Normal" or "Bright") #[derive(Debug, Deserialize)] struct AnsiColor { rgb: Rgb, diff --git a/examples/term_grid.rs b/examples/term_grid.rs index e973808..3a772da 100644 --- a/examples/term_grid.rs +++ b/examples/term_grid.rs @@ -1,7 +1,10 @@ //! Example demonstrating integration with the `ansi_term` crate. use catppuccin::PALETTE; -const fn ansi(color: &catppuccin::Color) -> ansi_term::Colour { +const fn ansi_term_color(color: &catppuccin::Color) -> ansi_term::Colour { + ansi_term::Colour::RGB(color.rgb.r, color.rgb.g, color.rgb.b) +} +const fn ansi_term_ansi_color(color: &catppuccin::AnsiColor) -> ansi_term::Colour { ansi_term::Colour::RGB(color.rgb.r, color.rgb.g, color.rgb.b) } @@ -33,7 +36,7 @@ fn main() { ); println!( "{} {:18} → {:6} {:18} {:18}", - ansi(color).reverse().paint(" "), + ansi_term_color(color).reverse().paint(" "), name, color.hex, rgb, @@ -41,5 +44,53 @@ fn main() { ); } println!(); + + println!( + "{}\n", + ansi_term::Style::new() + .underline() + .bold() + .paint(format!("{} ANSI", flavor.name)) + ); + + for ansi_color in &flavor.ansi_colors { + let name = format!("{}", ansi_color.name); + let rgb = format!( + "rgb({:3}, {:3}, {:3})", + ansi_color.normal.rgb.r, ansi_color.normal.rgb.g, ansi_color.normal.rgb.b + ); + let hsl = format!( + "hsl({:3.0}, {:5.3}, {:5.3})", + ansi_color.normal.hsl.h, ansi_color.normal.hsl.s, ansi_color.normal.hsl.l + ); + println!( + "{} {:18} → {:6} {:18} {:18}", + ansi_term_ansi_color(&ansi_color.normal).reverse().paint(" "), + name, + ansi_color.normal.hex, + rgb, + hsl, + ); + + let bright_name = format!("Bright {}", ansi_color.name); + let bright_rgb = format!( + "rgb({:3}, {:3}, {:3})", + ansi_color.bright.rgb.r, ansi_color.bright.rgb.g, ansi_color.bright.rgb.b + ); + let bright_hsl = format!( + "hsl({:3.0}, {:5.3}, {:5.3})", + ansi_color.bright.hsl.h, ansi_color.bright.hsl.s, ansi_color.bright.hsl.l + ); + println!( + "{} {:18} → {:6} {:18} {:18}", + ansi_term_ansi_color(&ansi_color.bright).reverse().paint(" "), + bright_name, + ansi_color.bright.hex, + bright_rgb, + bright_hsl, + ); + } + + println!(); } } diff --git a/src/lib.rs b/src/lib.rs index 07b17ee..26b423a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -526,17 +526,35 @@ impl From for css_colors::HSL { } #[cfg(feature = "ansi-term")] -impl Color { - /// Paints the given input with a color à la [ansi_term](https://docs.rs/ansi_term/latest/ansi_term/) - pub fn ansi_paint<'a, I, S: 'a + ToOwned + ?Sized>( - &self, - input: I, - ) -> ansi_term::ANSIGenericString<'a, S> - where - I: Into>, - ::Owned: core::fmt::Debug, - { - ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input) +mod ansi_term { + use crate::{AnsiColor, Color}; + + impl Color { + /// Paints the given input with a color à la [ansi_term](https://docs.rs/ansi_term/latest/ansi_term/) + pub fn ansi_paint<'a, I, S: 'a + ToOwned + ?Sized>( + &self, + input: I, + ) -> ansi_term::ANSIGenericString<'a, S> + where + I: Into>, + ::Owned: core::fmt::Debug, + { + ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input) + } + } + + impl AnsiColor { + /// Paints the given input with a color à la [ansi_term](https://docs.rs/ansi_term/latest/ansi_term/) + pub fn ansi_paint<'a, I, S: 'a + ToOwned + ?Sized>( + &self, + input: I, + ) -> ansi_term::ANSIGenericString<'a, S> + where + I: Into>, + ::Owned: core::fmt::Debug, + { + ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input) + } } }