From a46bc984d014d0b2a734f52b719d4354f04c5123 Mon Sep 17 00:00:00 2001 From: robin <67917529+comfysage@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:26:43 +0200 Subject: [PATCH] feat: add custom colors (#18) Co-authored-by: uncenter <47499684+uncenter@users.noreply.github.com> --- Cargo.lock | 20 ++++++++++++++++++++ Cargo.toml | 1 + README.md | 8 ++++++++ src/main.rs | 32 +++++++++++++++++++++++++------- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 487dcde..a1a0bd6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -195,6 +195,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys 0.52.0", +] + [[package]] name = "crossterm" version = "0.27.0" @@ -220,6 +233,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "eyre" version = "0.6.12" @@ -255,6 +274,7 @@ dependencies = [ "clap", "clap-stdin", "color-eyre", + "console", "crossterm", "textwrap", "unicode-width", diff --git a/Cargo.toml b/Cargo.toml index 6015a7c..e1289e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ edition = "2021" clap = { version = "4.5.4", features = ["derive"] } clap-stdin = "0.4.0" color-eyre = "0.6.3" +console = "0.15.8" crossterm = "0.27.0" textwrap = "0.16.1" unicode-width = "0.1.12" diff --git a/README.md b/README.md index bea360b..94dc54c 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,14 @@ kittysay echo | kittysay ``` +You can customize the output colors. + +```sh +# -c +kittysay -c 2 5 +echo | kittysay -c 2 5 +``` + ### `--width` You can use the `--width` flag to change the width of the speech bubble. Defaults to `45`, maxes out at a little less than the width of your terminal if you try to pass a very large number. diff --git a/src/main.rs b/src/main.rs index f818428..a9495f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,9 @@ struct Cli { /// Enable kittythink mode (thought bubble) #[clap(long, short)] think: bool, + /// Use custom colors. The first colors the message, the second colors the cat + #[arg(long, short, num_args(2))] + colors: Option>, } fn main() -> Result<()> { @@ -75,18 +78,13 @@ fn main() -> Result<()> { let mut lines = wrap(&args.message, width as usize); let longest = lines.iter().map(|line| line.width()).max().unwrap(); - println!( + let msg = format!( " {} {} {} {} - {} - /l、 - (゚、 。 7 - l ~ヽ - じしf_,)ノ - ", + {}", chars.top.repeat(longest), if lines.len() == 1 { format!("{} {} {}", chars.single_left, lines[0], chars.single_right) @@ -126,5 +124,25 @@ fn main() -> Result<()> { chars.arrow, ); + let mut msg_color = console::Color::White; + let mut cat_color = console::Color::White; + if let Some(colors) = args.colors { + msg_color = console::Color::Color256(colors[0]); + cat_color = console::Color::Color256(colors[1]); + } + + let cat = " + /l、 + (゚、 。 7 + l ~ヽ + じしf_,)ノ + "; + + println!( + "{}{}", + console::style(msg).fg(msg_color), + console::style(cat).fg(cat_color) + ); + Ok(()) }