Skip to content

Commit

Permalink
feat: fix lints and docs, simplify simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Nov 20, 2024
1 parent a230184 commit 527152f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
21 changes: 12 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn titlecase<S: AsRef<str>>(s: S) -> String {
}

fn remove_whitespace(s: &str) -> String {
s.replace(" ", "")
s.replace(' ', "")
}

fn flavors_in_order(palette: &Palette) -> std::vec::IntoIter<(&String, &Flavor)> {
Expand Down Expand Up @@ -195,7 +195,7 @@ fn ansi_colors_in_order(flavor: &Flavor) -> std::vec::IntoIter<(String, &AnsiCol
.ansi_colors
.iter()
.flat_map(|(_, c)| [&c.normal, &c.bright])
.map(|c| (c.name.to_lowercase().replace(" ", "_"), c))
.map(|c| (c.name.to_lowercase().replace(' ', "_"), c))
.sorted_by(|(_, a), (_, b)| a.code.cmp(&b.code))
}

Expand All @@ -220,15 +220,16 @@ fn make_flavor_colors_struct(sample_flavor: &Flavor) -> TokenStream {
}

fn make_flavor_ansi_colors_struct(sample_flavor: &Flavor) -> TokenStream {
let colors = ansi_colors_in_order(sample_flavor).map(|(identifier, _)| {
let ident = format_ident!("{identifier}");
let colors = ansi_colors_in_order(sample_flavor).map(|(k, _)| {
let ident = format_ident!("{k}");
let color_img = format!(" {}", ansi_color_palette_circles(&k));
quote! {
/// The #ident ANSI color.
#[doc = #color_img]
pub #ident: AnsiColor
}
});
quote! {
/// All of the ANSI colors for a particular flavor of Catppuccin
/// All of the ANSI colors for a particular flavor of Catppuccin.
/// Obtained via [`Flavor::ansi_colors`].
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -255,10 +256,11 @@ fn make_flavor_ansi_colors_struct(sample_flavor: &Flavor) -> TokenStream {
}

fn make_flavor_ansi_color_pairs_struct(sample_flavor: &Flavor) -> TokenStream {
let color_pairs = ansi_color_pairs_in_order(sample_flavor).map(|(identifier, _)| {
let ident = format_ident!("{identifier}");
let color_pairs = ansi_color_pairs_in_order(sample_flavor).map(|(k, _)| {
let ident = format_ident!("{k}");
let doc = format!("The normal and bright {k} ANSI color pair.");
quote! {
/// The normal and bright #ident ANSI color pair.
#[doc = #doc]
pub #ident: AnsiColorPair
}
});
Expand Down Expand Up @@ -323,6 +325,7 @@ fn make_flavor_ansi_colors_all_impl(sample_flavor: &Flavor) -> TokenStream {

/// Convert the 16 ANSI colors to 8 ANSI color pairs.
#[must_use]
#[allow(clippy::too_many_lines, clippy::unreadable_literal)]
pub const fn to_ansi_color_pairs(&self) -> FlavorAnsiColorPairs {
FlavorAnsiColorPairs {
#(#ansi_color_pairs),*
Expand Down
48 changes: 19 additions & 29 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ fn main() {
);

println!("Mocha's {} is {}", mocha_mauve.name, mocha_mauve.hex);
println!();

// iterate over the 16 ANSI colors (i.e. Black, Red, ..., Bright Black, Bright Red, ...)
println!("Mocha's ANSI colors in code order:");
for AnsiColor {
name,
rgb,
Expand All @@ -31,38 +33,26 @@ fn main() {
hex,
} in &mocha.ansi_colors
{
{
println!(
"Mocha ANSI [{:2}] {:15} → {:6} {:3?} {:19?}",
code,
name.to_string(),
hex,
rgb,
hsl,
);
}
println!(
"Mocha ANSI [{:2}] {:15} → {:6} {:3?} {:19?}",
code,
name.to_string(),
hex,
rgb,
hsl,
);
}

println!();

// iterate over the 16 ANSI colors in 8 pairs (i.e. Black, Bright Black, Red, Bright Red, ...)
for mocha_ansi_color_pairs in &mocha.ansi_colors.all_pairs() {
for AnsiColor {
name,
rgb,
hsl,
code,
hex,
} in [mocha_ansi_color_pairs.normal, mocha_ansi_color_pairs.bright]
{
println!(
"Mocha ANSI [{:2}] {:15} → {:6} {:3?} {:19?}",
code,
name.to_string(),
hex,
rgb,
hsl,
);
}
println!("Mocha's ANSI color pairs:");
for pair in &mocha.ansi_colors.all_pairs() {
println!(
"[{:2}] {:7} / [{:2}] {}",
pair.normal.code,
pair.normal.name.to_string(),
pair.bright.code,
pair.bright.name
);
}
}
2 changes: 1 addition & 1 deletion examples/term_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() {

println!(
"{} {:15} → {:6} {:18} {:18}",
ansi_term_ansi_color(&ansi_color).reverse().paint(" "),
ansi_term_ansi_color(ansi_color).reverse().paint(" "),
ansi_color.name.to_string(),
ansi_color.hex,
rgb,
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ pub struct ColorIterator<'a> {
current: usize,
}

/// An iterator over the ANSI colors in a flavor, defaults to ascending order by ANSI code 0 -> 16.
/// An iterator over the ANSI colors in a flavor.
///
/// Defaults to ascending order by ANSI code 0 -> 16.
/// Obtained via [`FlavorAnsiColors::into_iter()`](struct.FlavorAnsiColors.html#method.into_iter) or [`FlavorAnsiColors::iter()`].
pub struct AnsiColorIterator<'a> {
ansi_colors: &'a FlavorAnsiColors,
Expand Down Expand Up @@ -362,6 +364,7 @@ impl FlavorAnsiColors {
}

/// Get the ANSI color pairs
#[must_use]
pub const fn all_pairs(&self) -> FlavorAnsiColorPairs {
self.to_ansi_color_pairs()
}
Expand Down

0 comments on commit 527152f

Please sign in to comment.