A Rust library for colorizing console output with cute gradients
at main 2.9 kB view raw
1use crate::gradient::{ColorPalette, GradientConfig, GradientDirection, HueRange}; 2 3macro_rules! palette_methods { 4 ($($name:ident => $variant:ident),* $(,)?) => { 5 $(fn $name(&self) -> CutifiedString { self.palette(ColorPalette::$variant) })* 6 }; 7} 8 9macro_rules! hue_range_methods { 10 ($($name:ident => $variant:ident),* $(,)?) => { 11 $(fn $name(&self) -> CutifiedString { self.hue_range(HueRange::$variant) })* 12 }; 13} 14 15pub trait Cutify { 16 fn gradient(&self) -> CutifiedString; 17 fn palette(&self, palette: ColorPalette) -> CutifiedString; 18 fn hue_range(&self, range: HueRange) -> CutifiedString; 19 fn gradient_direction(&self, direction: GradientDirection) -> CutifiedString; 20 21 palette_methods! { 22 rainbow => Rainbow, 23 pastel => Pastel, 24 neon => Neon, 25 earth => Earth, 26 ocean => Ocean, 27 fire => Fire, 28 sunset => Sunset, 29 forest => Forest, 30 lavender => Lavender, 31 cherry_blossom => CherryBlossom, 32 } 33 34 hue_range_methods! { 35 reds => Reds, 36 oranges => Oranges, 37 yellows => Yellows, 38 greens => Greens, 39 cyans => Cyans, 40 blues => Blues, 41 purples => Purples, 42 pinks => Pinks, 43 warm => Warm, 44 cool => Cool, 45 } 46} 47 48pub struct CutifiedString { 49 text: String, 50 config: GradientConfig, 51} 52 53impl CutifiedString { 54 pub fn new(text: impl Into<String>, config: GradientConfig) -> Self { 55 Self { 56 text: text.into(), 57 config, 58 } 59 } 60 61 pub fn text(&self) -> &str { 62 &self.text 63 } 64 pub fn config(&self) -> &GradientConfig { 65 &self.config 66 } 67 68 crate::config_delegate! { 69 direction(direction: GradientDirection), 70 hue_range(range: HueRange), 71 palette(palette: ColorPalette), 72 saturation(saturation: f32), 73 lightness(lightness: f32), 74 hue_shift(shift: f32), 75 base_hue(hue: f32), 76 random_hue(), 77 reverse(), 78 scale(scale: f32), 79 step(step: f32), 80 } 81} 82 83impl std::fmt::Display for CutifiedString { 84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 85 write!(f, "{}", crate::cutify_string(&self.text, &self.config)) 86 } 87} 88 89impl<T: AsRef<str>> Cutify for T { 90 fn gradient(&self) -> CutifiedString { 91 CutifiedString::new(self.as_ref(), GradientConfig::default()) 92 } 93 fn palette(&self, palette: ColorPalette) -> CutifiedString { 94 CutifiedString::new(self.as_ref(), GradientConfig::default().palette(palette)) 95 } 96 fn hue_range(&self, range: HueRange) -> CutifiedString { 97 CutifiedString::new(self.as_ref(), GradientConfig::default().hue_range(range)) 98 } 99 fn gradient_direction(&self, direction: GradientDirection) -> CutifiedString { 100 CutifiedString::new( 101 self.as_ref(), 102 GradientConfig::default().direction(direction), 103 ) 104 } 105}