Skip to content

Commit

Permalink
feat: add ansi to ansi_term feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoudham committed Nov 2, 2024
1 parent 6c0e790 commit 92ab43f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
55 changes: 53 additions & 2 deletions examples/term_grid.rs
Original file line number Diff line number Diff line change
@@ -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)
}

Expand Down Expand Up @@ -33,13 +36,61 @@ fn main() {
);
println!(
"{} {:18} → {:6} {:18} {:18}",
ansi(color).reverse().paint(" "),
ansi_term_color(color).reverse().paint(" "),
name,
color.hex,
rgb,
hsl,
);
}
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!();
}
}
40 changes: 29 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,17 +526,35 @@ impl From<Color> 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<std::borrow::Cow<'a, S>>,
<S as ToOwned>::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<std::borrow::Cow<'a, S>>,
<S as ToOwned>::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<std::borrow::Cow<'a, S>>,
<S as ToOwned>::Owned: core::fmt::Debug,
{
ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input)
}
}
}

Expand Down

0 comments on commit 92ab43f

Please sign in to comment.