1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! A library for Rust that provides utilities
//! to colorizing terminal outputs and styling strings.
//!
//! Coloriz helps to make your terminal colorful.  
//!
//! # Quick Example
//!
//! ```rs
//! use coloriz::*;
//!
//! fn main() {
//!     let text = "Lorem ipsum";
//!     println!("{}", text.fg(Color::Cyan));
//!     println!("{}", text.bg(Color::BrightBlack).fg(Color::Red));
//!     println!("{}", text.fg(HSL::new(0.03, 0.7, 0.666)));
//!     println!("{}", text.fg((167, 232, 78))); // the same as text.fg(RGB::new(167, 232, 78))
//!     println!("{}", text.fg_gradient(RGB::from_hex(0xFF6712), RGB::from_hex(0x1087FF)));
//!     println!("{}", Color::BrightMagenta.to(text));
//!     println!("{}", Color::BrightMagenta.under(text));
//! }
//! ```

mod ansi;
mod color;
mod colorize;
mod gradient;
mod style;

use ansi::*;
pub use color::*;
pub use colorize::*;
pub use gradient::*;
pub use style::*;

use std::fmt;

/// A string that may be colorized
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StyledText {
    foreground_color_style: ColorStyle,
    background_color_style: ColorStyle,
    style: Style,
    pub text: String,
}

impl StyledText {
    /// Creates a [ColoredText] from `text` without any color styles
    #[inline]
    pub fn new(text: &str) -> Self {
        Self {
            foreground_color_style: ColorStyle::Colorless,
            background_color_style: ColorStyle::Colorless,
            style: Style::new(),
            text: text.to_string(),
        }
    }

    /// Returns the foreground color style of `self`
    #[inline]
    pub fn foreground(&self) -> ColorStyle {
        self.foreground_color_style
    }

    /// Returns the background color style of `self`
    #[inline]
    pub fn background(&self) -> ColorStyle {
        self.background_color_style
    }

    pub fn styles(&self) -> Vec<StyleMode> {
        self.style.styles()
    }

    #[inline]
    fn with_foreground_color<C: Into<ColorStyle>>(&self, color: C) -> Self {
        Self {
            foreground_color_style: color.into(),
            background_color_style: self.background_color_style,
            style: self.style,
            text: self.text.clone(),
        }
    }

    #[inline]
    fn with_background_color<C: Into<ColorStyle>>(&self, color: C) -> Self {
        Self {
            foreground_color_style: self.foreground_color_style,
            background_color_style: color.into(),
            style: self.style,
            text: self.text.clone(),
        }
    }

    fn with_style_mode(&self, mode: StyleMode) -> Self {
        Self {
            foreground_color_style: self.foreground_color_style,
            background_color_style: self.background_color_style,
            style: self.style.add_style_mode(mode),
            text: self.text.clone(),
        }
    }
}

impl From<String> for StyledText {
    fn from(s: String) -> Self {
        StyledText::new(&s)
    }
}

impl From<&str> for StyledText {
    fn from(s: &str) -> Self {
        StyledText::new(s)
    }
}

impl fmt::Display for StyledText {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "\x1B[{}m", self.style.ansi_code())?;
        match (self.foreground_color_style, self.background_color_style) {
            (ColorStyle::Single(fg), ColorStyle::Single(bg)) => {
                write!(
                    f,
                    "\x1B[{};{}m{}\x1B[0m",
                    fg.ansi_color_code(TargetGround::Foreground),
                    bg.ansi_color_code(TargetGround::Background),
                    self.text
                )
            }
            (ColorStyle::Single(fg), ColorStyle::Gradient(bg)) => {
                write!(
                    f,
                    "\x1B[{}m{}",
                    fg.ansi_color_code(TargetGround::Foreground),
                    bg.build(&self.text, TargetGround::Background)
                )
            }
            (ColorStyle::Single(fg), ColorStyle::Colorless) => {
                write!(
                    f,
                    "\x1B[{}m{}\x1B[0m",
                    fg.ansi_color_code(TargetGround::Foreground),
                    self.text
                )
            }
            (ColorStyle::Gradient(fg), ColorStyle::Single(bg)) => {
                write!(
                    f,
                    "\x1B[{}m{}",
                    bg.ansi_color_code(TargetGround::Background),
                    fg.build(&self.text, TargetGround::Foreground)
                )
            }
            (ColorStyle::Gradient(fg), ColorStyle::Gradient(bg)) => {
                write!(f, "{}", build_all_gradient_text(&self.text, fg, bg))
            }
            (ColorStyle::Gradient(fg), ColorStyle::Colorless) => {
                write!(f, "{}", fg.build(&self.text, TargetGround::Foreground))
            }
            (ColorStyle::Colorless, ColorStyle::Single(bg)) => {
                write!(
                    f,
                    "\x1B[{}m{}\x1B[0m",
                    bg.ansi_color_code(TargetGround::Background),
                    self.text
                )
            }
            (ColorStyle::Colorless, ColorStyle::Gradient(bg)) => {
                write!(f, "{}", bg.build(&self.text, TargetGround::Background))
            }
            (ColorStyle::Colorless, ColorStyle::Colorless) => {
                write!(f, "{}\x1B[0m", self.text)
            }
        }
    }
}

/// A Color that can apply to text
pub trait ColorApply: Into<ColorStyle> {
    /// Applies `self` to the foreground color of `c`
    ///
    /// # Example
    /// ```
    /// let red = Color::Red.to("lorem ipsum");
    /// assert_eq!(red, "lorem ipsum".fg(Color::Red));
    /// ```
    fn to<C>(self, c: C) -> StyledText
    where
        C: Colorize + ?Sized,
    {
        c.fg::<Self>(self)
    }

    /// Applies `self` to the background color of `c`
    ///    
    /// # Example
    /// ```
    /// let green = Color::Green.under("lorem ipsum");
    /// assert_eq!(green, "lorem ipsum".bg(Color::Green));
    /// ```
    fn under<C>(self, c: C) -> StyledText
    where
        C: Colorize + ?Sized,
    {
        c.bg::<Self>(self)
    }
}

impl ColorApply for RGB {}
impl ColorApply for HSL {}
impl ColorApply for Color {}
impl ColorApply for Gradient {}