A Rust library for colorizing console output with cute gradients
1use crate::gradient::{ColorPalette, GradientConfig};
2use std::cell::RefCell;
3
4thread_local! {
5 static GLOBAL_GRADIENT: RefCell<GradientState> = RefCell::new(GradientState::new());
6}
7
8pub struct GradientState {
9 config: GradientConfig,
10 position: usize,
11}
12
13impl GradientState {
14 fn new() -> Self {
15 Self {
16 config: GradientConfig::default().random_hue(),
17 position: 0,
18 }
19 }
20
21 fn colorize(&mut self, text: &str) -> String {
22 let mut result = String::new();
23
24 for ch in text.chars() {
25 match ch {
26 ' ' | '\n' => result.push(ch),
27 _ => {
28 let color = self.config.color_at_position(self.position, 0, 1000, 1);
29 if let crossterm::style::Color::Rgb { r, g, b } = color {
30 result.push_str(&format!("\x1b[38;2;{};{};{}m{}\x1b[0m", r, g, b, ch));
31 }
32 self.position += 1;
33 }
34 }
35 }
36 result
37 }
38
39 fn reset(&mut self) {
40 self.position = 0;
41 }
42 fn set_config(&mut self, config: GradientConfig) {
43 self.config = config;
44 }
45}
46
47pub fn cute(text: &str) -> String {
48 GLOBAL_GRADIENT.with(|state| state.borrow_mut().colorize(text))
49}
50
51pub fn reset_gradient() {
52 GLOBAL_GRADIENT.with(|state| state.borrow_mut().reset());
53}
54
55pub fn set_gradient_config(config: GradientConfig) {
56 GLOBAL_GRADIENT.with(|state| state.borrow_mut().set_config(config));
57}
58
59pub fn set_palette(palette: ColorPalette) {
60 set_gradient_config(GradientConfig::default().palette(palette).random_hue());
61}
62
63pub fn cuteprint(text: &str) {
64 print!("{}", cute(text));
65}
66
67pub fn cuteprintln(text: &str) {
68 println!("{}", cute(text));
69}