Skip to content

Commit

Permalink
feat: implement Pair iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoudham committed Nov 6, 2024
1 parent 50f6417 commit e4ee6a8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 29 deletions.
15 changes: 13 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,19 +306,30 @@ fn make_flavor_colors_all_impl(sample_flavor: &Flavor) -> TokenStream {
}

fn make_flavor_ansi_colors_all_impl(sample_flavor: &Flavor) -> TokenStream {
let items = ansi_colors_in_order(sample_flavor).map(|(identifier, _)| {
let ansi_colors = ansi_colors_in_order(sample_flavor).map(|(identifier, _)| {
let ident = format_ident!("{identifier}");
quote! { &self.#ident }
});
let ansi_color_pairs = ansi_color_pairs_in_order(sample_flavor).map(|(identifier, color_pair)| {
let entry = make_ansi_color_pair_entry(identifier, color_pair);
entry
});
quote! {
impl FlavorAnsiColors {
/// Get an array of the ANSI colors in the flavor.
#[must_use]
pub const fn all_ansi_colors(&self) -> [&AnsiColor; 16] {
[
#(#items),*
#(#ansi_colors),*
]
}

#[must_use]
pub const fn to_ansi_color_pairs(&self) -> FlavorAnsiColorPairs {
FlavorAnsiColorPairs {
#(#ansi_color_pairs),*
}
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,26 @@ fn main() {
);
}
}

println!();

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,
);
}
}
}
64 changes: 37 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,12 @@ pub struct AnsiColorIterator<'a> {
current: usize,
}

/// TODO: Figure out how to expose this iterator in the public API
/// An iterator over the ANSI color pairs in a flavor.
/// Obtained via [`FlavorAnsiColorPairs::into_iter()`](struct.FlavorAnsiColorPairs.html#method.into_iter) or [`FlavorAnsiColorPairs::iter()`].
// pub struct AnsiColorPairsIterator<'a> {
// ansi_pair_colors: &'a FlavorAnsiColorPairs,
// current: usize,
// }
pub struct AnsiColorPairsIterator<'a> {
ansi_color_pairs: &'a FlavorAnsiColorPairs,
current: usize,
}

impl Palette {
/// Get an array of the flavors in the palette.
Expand Down Expand Up @@ -362,9 +361,20 @@ impl FlavorAnsiColors {
}
}

/// Analogous to calling [`FlavorAnsiColors::iter()`]
pub const fn all(&self) -> AnsiColorIterator {
self.iter()
/// Get the ANSI color pairs
pub const fn all_pairs(&self) -> FlavorAnsiColorPairs {
self.to_ansi_color_pairs()
}
}

impl FlavorAnsiColorPairs {
/// Create an iterator over the ANSI color pairs in the flavor.
#[must_use]
pub const fn iter(&self) -> AnsiColorPairsIterator {
AnsiColorPairsIterator {
ansi_color_pairs: self,
current: 0,
}
}
}

Expand Down Expand Up @@ -410,19 +420,19 @@ impl<'a> Iterator for AnsiColorIterator<'a> {
}
}

// impl<'a> Iterator for AnsiColorPairsIterator<'a> {
// type Item = &'a AnsiColorPair;
impl<'a> Iterator for AnsiColorPairsIterator<'a> {
type Item = &'a AnsiColorPair;

// fn next(&mut self) -> Option<Self::Item> {
// if self.current >= self.ansi_pair_colors.all_ansi_color_pairs().len() {
// None
// } else {
// let color = self.ansi_pair_colors.all_ansi_color_pairs()[self.current];
// self.current += 1;
// Some(color)
// }
// }
// }
fn next(&mut self) -> Option<Self::Item> {
if self.current >= self.ansi_color_pairs.all_ansi_color_pairs().len() {
None
} else {
let color = self.ansi_color_pairs.all_ansi_color_pairs()[self.current];
self.current += 1;
Some(color)
}
}
}

impl<'a> IntoIterator for &'a Palette {
type Item = &'a Flavor;
Expand Down Expand Up @@ -451,14 +461,14 @@ impl<'a> IntoIterator for &'a FlavorAnsiColors {
}
}

// impl<'a> IntoIterator for &'a FlavorAnsiColorPairs {
// type Item = &'a AnsiColorPair;
// type IntoIter = AnsiColorPairsIterator<'a>;
impl<'a> IntoIterator for &'a FlavorAnsiColorPairs {
type Item = &'a AnsiColorPair;
type IntoIter = AnsiColorPairsIterator<'a>;

// fn into_iter(self) -> Self::IntoIter {
// self.iter()
// }
// }
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl Flavor {
/// Create an iterator over the colors in the flavor.
Expand Down

0 comments on commit e4ee6a8

Please sign in to comment.