Skip to content

Commit 1988ec1

Browse files
committed
feat: add iced feature
1 parent 091a98e commit 1988ec1

File tree

6 files changed

+178
-53
lines changed

6 files changed

+178
-53
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ jobs:
4646
with:
4747
toolchain: stable
4848
components: rustfmt
49+
- uses: Swatinem/rust-cache@v2
4950
- run: cargo fmt --all --check

.github/workflows/release-plz.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
fetch-depth: 0
2424
- name: Install Rust toolchain
2525
uses: dtolnay/rust-toolchain@stable
26+
- uses: Swatinem/rust-cache@v2
2627
- name: Run release-plz
2728
uses: release-plz/[email protected]
2829
with:
@@ -48,6 +49,7 @@ jobs:
4849
fetch-depth: 0
4950
- name: Install Rust toolchain
5051
uses: dtolnay/rust-toolchain@stable
52+
- uses: Swatinem/rust-cache@v2
5153
- name: Run release-plz
5254
uses: release-plz/[email protected]
5355
with:

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ clippy.nursery = "warn"
2323
ansi_term = { version = "0.12", optional = true }
2424
bevy = { version = "0.17", optional = true }
2525
css-colors = { version = "1.0", optional = true }
26+
iced = { version = "0.13.1", optional = true }
2627
ratatui = { version = "0.29", optional = true }
2728
serde = { version = "1.0", features = ["derive"], optional = true }
2829

@@ -41,10 +42,11 @@ serde_json = "1.0.145"
4142

4243
[features]
4344
ansi-term = ["dep:ansi_term"]
45+
bevy = ["dep:bevy"]
4446
css-colors = ["dep:css-colors"]
47+
iced = ["dep:iced"]
4548
ratatui = ["dep:ratatui"]
4649
serde = ["dep:serde"]
47-
bevy = ["dep:bevy"]
4850

4951
[[example]]
5052
name = "css"

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,39 @@ More examples can be found
4747
### ANSI string painting
4848

4949
Enable the `ansi-term` feature to add the
50-
`Color::ansi_paint` method.
50+
[`Color::ansi_paint`](Color::ansi_paint) method.
5151
This adds [ansi-term](https://crates.io/crates/ansi_term) as a dependency.
5252

5353
Example: [`examples/term_grid.rs`](https://github.com/catppuccin/rust/blob/main/examples/term_grid.rs)
5454

55+
#### Bevy
56+
57+
Enable the `bevy` feature to enable the conversion of Catppuccin colors to
58+
[`bevy::prelude::Color`] instances.
59+
This adds [bevy](https://crates.io/crates/bevy) as a dependency.
60+
61+
Example: [`examples/bevy.rs`](https://github.com/catppuccin/rust/blob/main/examples/bevy.rs)
62+
5563
#### CSS colors
5664

5765
Enable the `css-colors` feature to enable the conversion of Catppuccin colors to
58-
`css_colors::RGB` instances.
66+
[`css_colors::RGB`] instances.
5967
This adds [css-colors](https://crates.io/crates/css-colors) as a dependency.
6068

6169
Example: [`examples/css.rs`](https://github.com/catppuccin/rust/blob/main/examples/css.rs)
6270

71+
#### Iced
72+
73+
Enable the `iced` feature to enable the conversion of Catppuccin colors to
74+
[`iced::Color`] instances.
75+
This adds [iced](https://crates.io/crates/iced) as a dependency.
76+
77+
Example: [`examples/iced.rs`](https://github.com/catppuccin/rust/blob/main/examples/iced.rs)
78+
6379
#### Ratatui
6480

6581
Enable the `ratatui` feature to enable the conversion of Catppuccin colors to
66-
`ratatui::style::Color` instances.
82+
[`ratatui::style::Color`] instances.
6783
This adds [ratatui](https://crates.io/crates/ratatui) as a dependency.
6884

6985
Example: [`examples/ratatui.rs`](https://github.com/catppuccin/rust/blob/main/examples/ratatui.rs)

examples/iced.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! Example demonstrating integration with the `iced` crate.
2+
use iced::{
3+
application,
4+
daemon::Appearance,
5+
widget::{button, column, container, text},
6+
Alignment::Center,
7+
Element,
8+
Length::Fill,
9+
Result,
10+
};
11+
12+
const COLORS: catppuccin::FlavorColors = catppuccin::PALETTE.latte.colors;
13+
14+
#[derive(Default)]
15+
struct Counter {
16+
value: i64,
17+
}
18+
19+
#[derive(Clone, Copy, Debug)]
20+
enum Message {
21+
Increment,
22+
Decrement,
23+
}
24+
25+
impl Counter {
26+
const fn update(&mut self, message: Message) {
27+
match message {
28+
Message::Increment => {
29+
self.value += 1;
30+
}
31+
Message::Decrement => {
32+
self.value -= 1;
33+
}
34+
}
35+
}
36+
37+
fn view(&self) -> Element<'_, Message> {
38+
let green: iced::Color = COLORS.green.into();
39+
let red: iced::Color = COLORS.red.into();
40+
container(
41+
column![
42+
button(text("+").size(50).center())
43+
.style(move |_, _| button::Style {
44+
background: Some(green.into()),
45+
text_color: COLORS.crust.into(),
46+
..Default::default()
47+
})
48+
.width(60)
49+
.on_press(Message::Increment),
50+
text(self.value).size(50),
51+
button(text("-").size(50).center())
52+
.style(move |_, _| button::Style {
53+
background: Some(red.into()),
54+
text_color: COLORS.crust.into(),
55+
..Default::default()
56+
})
57+
.width(60)
58+
.on_press(Message::Decrement),
59+
]
60+
.align_x(Center)
61+
.spacing(10),
62+
)
63+
.padding(20)
64+
.center_x(Fill)
65+
.center_y(Fill)
66+
.into()
67+
}
68+
}
69+
70+
fn main() -> Result {
71+
application("Counter", Counter::update, Counter::view)
72+
.style(move |_, _| Appearance {
73+
background_color: COLORS.base.into(),
74+
text_color: COLORS.text.into(),
75+
})
76+
.run()
77+
}

src/lib.rs

Lines changed: 76 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
//!
3838
//! Example: [`examples/term_grid.rs`](https://github.com/catppuccin/rust/blob/main/examples/term_grid.rs)
3939
//!
40+
//! ### Bevy
41+
//!
42+
//! Enable the `bevy` feature to enable the conversion of Catppuccin colors to
43+
//! [`bevy::prelude::Color`] instances.
44+
//! This adds [bevy](https://crates.io/crates/bevy) as a dependency.
45+
//!
46+
//! Example: [`examples/bevy.rs`](https://github.com/catppuccin/rust/blob/main/examples/bevy.rs)
47+
//!
4048
//! ### CSS colors
4149
//!
4250
//! Enable the `css-colors` feature to enable the conversion of Catppuccin colors to
@@ -45,6 +53,14 @@
4553
//!
4654
//! Example: [`examples/css.rs`](https://github.com/catppuccin/rust/blob/main/examples/css.rs)
4755
//!
56+
//! ### Iced
57+
//!
58+
//! Enable the `iced` feature to enable the conversion of Catppuccin colors to
59+
//! [`iced::Color`] instances.
60+
//! This adds [iced](https://crates.io/crates/iced) as a dependency.
61+
//!
62+
//! Example: [`examples/iced.rs`](https://github.com/catppuccin/rust/blob/main/examples/iced.rs)
63+
//!
4864
//! ### Ratatui
4965
//!
5066
//! Enable the `ratatui` feature to enable the conversion of Catppuccin colors to
@@ -53,14 +69,6 @@
5369
//!
5470
//! Example: [`examples/ratatui.rs`](https://github.com/catppuccin/rust/blob/main/examples/ratatui.rs)
5571
//!
56-
//! ### Bevy
57-
//!
58-
//! Enable the `bevy` feature to enable the conversion of Catppuccin colors to
59-
//! [`bevy::prelude::Color`] instances.
60-
//! This adds [bevy](https://crates.io/crates/bevy) as a dependency.
61-
//!
62-
//! Example: [`examples/bevy.rs`](https://github.com/catppuccin/rust/blob/main/examples/bevy.rs)
63-
//!
6472
//! ### Serde
6573
//!
6674
//! Enable the `serde` feature to enable the serialization of Catppuccin's palette,
@@ -559,6 +567,58 @@ impl From<(f64, f64, f64)> for Hsl {
559567
}
560568
}
561569

570+
#[cfg(feature = "ansi-term")]
571+
mod ansi_term {
572+
use crate::{AnsiColor, Color};
573+
574+
impl Color {
575+
/// Paints the given input with a color à la [ansi_term](https://docs.rs/ansi_term/latest/ansi_term/)
576+
pub fn ansi_paint<'a, I, S: 'a + ToOwned + ?Sized>(
577+
&self,
578+
input: I,
579+
) -> ansi_term::ANSIGenericString<'a, S>
580+
where
581+
I: Into<std::borrow::Cow<'a, S>>,
582+
<S as ToOwned>::Owned: core::fmt::Debug,
583+
{
584+
ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input)
585+
}
586+
}
587+
588+
impl AnsiColor {
589+
/// Paints the given input with a color à la [ansi_term](https://docs.rs/ansi_term/latest/ansi_term/)
590+
pub fn ansi_paint<'a, I, S: 'a + ToOwned + ?Sized>(
591+
&self,
592+
input: I,
593+
) -> ansi_term::ANSIGenericString<'a, S>
594+
where
595+
I: Into<std::borrow::Cow<'a, S>>,
596+
<S as ToOwned>::Owned: core::fmt::Debug,
597+
{
598+
ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input)
599+
}
600+
}
601+
}
602+
603+
#[cfg(feature = "bevy")]
604+
mod bevy {
605+
use crate::{AnsiColor, Color};
606+
607+
impl From<Color> for bevy::prelude::Color {
608+
fn from(value: Color) -> Self {
609+
#[allow(clippy::cast_possible_truncation)]
610+
Self::hsl(value.hsl.h as f32, value.hsl.s as f32, value.hsl.l as f32)
611+
}
612+
}
613+
614+
impl From<AnsiColor> for bevy::prelude::Color {
615+
fn from(value: AnsiColor) -> Self {
616+
#[allow(clippy::cast_possible_truncation)]
617+
Self::hsl(value.hsl.h as f32, value.hsl.s as f32, value.hsl.l as f32)
618+
}
619+
}
620+
}
621+
562622
#[cfg(feature = "css-colors")]
563623
mod css_colors {
564624
use crate::{AnsiColor, Color};
@@ -606,35 +666,19 @@ mod css_colors {
606666
}
607667
}
608668

609-
#[cfg(feature = "ansi-term")]
610-
mod ansi_term {
669+
#[cfg(feature = "iced")]
670+
mod iced {
611671
use crate::{AnsiColor, Color};
612672

613-
impl Color {
614-
/// Paints the given input with a color à la [ansi_term](https://docs.rs/ansi_term/latest/ansi_term/)
615-
pub fn ansi_paint<'a, I, S: 'a + ToOwned + ?Sized>(
616-
&self,
617-
input: I,
618-
) -> ansi_term::ANSIGenericString<'a, S>
619-
where
620-
I: Into<std::borrow::Cow<'a, S>>,
621-
<S as ToOwned>::Owned: core::fmt::Debug,
622-
{
623-
ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input)
673+
impl From<Color> for iced::Color {
674+
fn from(value: Color) -> Self {
675+
Self::from_rgb8(value.rgb.r, value.rgb.g, value.rgb.b)
624676
}
625677
}
626678

627-
impl AnsiColor {
628-
/// Paints the given input with a color à la [ansi_term](https://docs.rs/ansi_term/latest/ansi_term/)
629-
pub fn ansi_paint<'a, I, S: 'a + ToOwned + ?Sized>(
630-
&self,
631-
input: I,
632-
) -> ansi_term::ANSIGenericString<'a, S>
633-
where
634-
I: Into<std::borrow::Cow<'a, S>>,
635-
<S as ToOwned>::Owned: core::fmt::Debug,
636-
{
637-
ansi_term::Color::RGB(self.rgb.r, self.rgb.g, self.rgb.b).paint(input)
679+
impl From<AnsiColor> for iced::Color {
680+
fn from(value: AnsiColor) -> Self {
681+
Self::from_rgb8(value.rgb.r, value.rgb.g, value.rgb.b)
638682
}
639683
}
640684
}
@@ -655,20 +699,3 @@ mod ratatui {
655699
}
656700
}
657701
}
658-
659-
#[cfg(feature = "bevy")]
660-
mod bevy {
661-
use crate::{AnsiColor, Color};
662-
663-
impl From<Color> for bevy::prelude::Color {
664-
fn from(value: Color) -> Self {
665-
Self::hsl(value.hsl.h as f32, value.hsl.s as f32, value.hsl.l as f32)
666-
}
667-
}
668-
669-
impl From<AnsiColor> for bevy::prelude::Color {
670-
fn from(value: AnsiColor) -> Self {
671-
Self::hsl(value.hsl.h as f32, value.hsl.s as f32, value.hsl.l as f32)
672-
}
673-
}
674-
}

0 commit comments

Comments
 (0)