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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/// Environment variable name for enabling/disabling color
///
/// Whan a tty is attached to the environment and this environment variable is
/// either unset or truthy color will be used. Likewise if a tty exists and this
/// variable is set and falsy color will not be used.
pub const TERM_COLOR: &str = "TERM_COLOR";

/// `Color` defines supported color types and provides static functions
///
/// ### Examples
/// ```rust
/// use gory::*;
///
/// print!("{}  ", format!("\\e[1;{}m", Color::Red).red());
/// print!("{}  ", "red".red());
/// println!();
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Color {
    // Standard ANSI defined color
    Black,   // 90
    Red,     // 91
    Green,   // 92
    Yellow,  // 93
    Blue,    // 94
    Magenta, // 95
    Cyan,    // 96
    White,   // 97
}
impl Color {
    /// Is color enabled.
    ///
    /// Determines if the environment has a tty attached and the `TERM_COLOR` environment
    /// variable is either unset or is set to a truthy value i.e. not `0` and not some
    /// case insensitive variation of `false`.
    ///
    /// ### Examples
    /// ```rust
    /// use gory::*;
    ///
    /// println!("{:?}", Color::enabled());
    /// ```
    pub fn enabled() -> bool {
        *private::TERM_COLOR_FLAG
    }

    /// Force color to be enable or disabled regardless of attached tty or value of
    /// `TERM_COLOR` based on the `bool` given.
    ///
    /// To return to automatic color control simply call with a value of `None`.
    ///
    /// ### Examples
    /// ```rust,ignore
    /// use gory::*;
    ///
    /// Color::force(Some(true));
    /// Color::force(Some(false));
    /// Color::force(None);
    /// ```
    pub fn force(val: Option<bool>) {
        *private::FORCE_COLOR.lock().unwrap() = val;
    }

    // Internal functions to check the status of the force value
    pub(crate) fn force_on() -> bool {
        match *private::FORCE_COLOR.lock().unwrap() {
            Some(val) => val,
            None => false,
        }
    }

    pub(crate) fn force_off() -> bool {
        match *private::FORCE_COLOR.lock().unwrap() {
            Some(val) => !val,
            None => false,
        }
    }
}

// Write out the color string
impl std::fmt::Display for Color {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(match *self {
            Color::Black => "90",
            Color::Red => "91",
            Color::Green => "92",
            Color::Yellow => "93",
            Color::Blue => "94",
            Color::Magenta => "95",
            Color::Cyan => "96",
            Color::White => "97",
        })
    }
}

/// `Colorable` defines a set of simple color functions for a given type
pub trait Colorable {
    // Set the style to use for the foreground
    fn set_fg_style(self, color: Color) -> ColorString
    where
        Self: Sized;

    // Black functions
    // -------------------------------------------------------------------------
    fn black(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::Black)
    }

    // Red functions
    // -------------------------------------------------------------------------
    fn red(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::Red)
    }

    // Green functions
    // -------------------------------------------------------------------------
    fn green(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::Green)
    }

    // Yellow functions
    // -------------------------------------------------------------------------
    fn yellow(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::Yellow)
    }

    // Blue functions
    // -------------------------------------------------------------------------
    fn blue(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::Blue)
    }

    // Magenta functions
    // -------------------------------------------------------------------------
    fn magenta(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::Magenta)
    }

    // Cyan functions
    // -------------------------------------------------------------------------
    fn cyan(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::Cyan)
    }

    // White functions
    // -------------------------------------------------------------------------
    fn white(self) -> ColorString
    where
        Self: Sized,
    {
        self.set_fg_style(Color::White)
    }
}

/// Wrapper around the String type to provide colors and styles.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColorString {
    inner: String,
    fg_color: Color,
}

// Implement Deref to make ColorString behave like String
impl core::ops::Deref for ColorString {
    type Target = str;

    fn deref(&self) -> &str {
        &self.inner
    }
}

// Implement the Colorable trait for chaining of operations
impl Colorable for ColorString {
    fn set_fg_style(mut self, color: Color) -> ColorString
    where
        Self: Sized,
    {
        self.fg_color = color;
        self
    }
}

// Write out the color string
impl std::fmt::Display for ColorString {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        // If color is disabled fallback on String's implementation
        if Color::force_off() || (!Color::enabled() && !Color::force_on()) {
            return <String as std::fmt::Display>::fmt(&self.inner, f);
        }

        // Needed to allow the ensure call to mutate the formatter
        let d = std::cell::RefCell::new(f);

        // Ensure the reset escape sequence gets written out regardless of success
        let _ensure = private::ensure(|| d.borrow_mut().write_str("\x1B[0m"));

        // Start escape sequence
        d.borrow_mut().write_str("\x1B[")?;

        // Always set bold to keep it bright and simple
        d.borrow_mut().write_str("1;")?;

        // Write out foreground color
        d.borrow_mut().write_str(&self.fg_color.to_string())?;

        // Close escape sequence
        d.borrow_mut().write_str("m")?;

        // Write out the actual String
        d.borrow_mut().write_str(&self.inner)?;

        // Write out color strings using terminal escape sequences
        Ok(())
    }
}

// Implement the Colorable Trait for &str
impl<'a> Colorable for &'a str {
    // Set the style to use for the foreground
    fn set_fg_style(self, color: Color) -> ColorString
    where
        Self: Sized,
    {
        ColorString { inner: String::from(self), fg_color: color }
    }
}

// Private implementation
// -------------------------------------------------------------------------------------------------
pub(crate) mod private {
    use lazy_static::*;
    use std::{env, ffi::OsStr, fmt, sync::Mutex};

    lazy_static! {
        /// `TERM_COLOR_FLAG` will be true if the environment is a tty and the
        /// environment variable `TERM_COLOR` is not set to something falsy.
        pub static ref TERM_COLOR_FLAG: bool = hastty() && flag_default(super::TERM_COLOR, true);

        // Force color to be enabled or disabled based on this additional flag
        pub static ref FORCE_COLOR: Mutex<Option<bool>> = Mutex::new(None);
    }

    // Get an environment flag value with a default
    pub fn flag_default<K: AsRef<OsStr>>(key: K, default: bool) -> bool {
        !matches!(env::var(key).unwrap_or_else(|_| default.to_string()).to_lowercase().as_str(), "false" | "0")
    }

    // Check if the environment has a tty
    pub fn hastty() -> bool {
        unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
    }

    // Ensure the given closure is executed once the surrounding scope closes.
    // Inspired by Golang's `defer`, Java's finally and Ruby's `ensure`
    pub fn ensure<T: FnOnce() -> fmt::Result>(f: T) -> impl Drop {
        Ensure(Some(f))
    }

    // Ensure uses Rust's object destructor to execute the given closure once
    // the surrounding scope closes.
    struct Ensure<T: FnOnce() -> fmt::Result>(Option<T>);

    impl<T: FnOnce() -> fmt::Result> Drop for Ensure<T> {
        fn drop(&mut self) {
            self.0.take().map(|f| f());
        }
    }
}

// Unit tests
// -------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_color_enabled() {
        assert!(Color::enabled() || !Color::enabled());
    }

    #[test]
    fn test_colors() {
        // Force color
        assert!(!Color::force_on());
        assert!(!Color::force_off());
        Color::force(Some(true));
        assert!(Color::force_on());
        assert!(!Color::force_off());

        // Deref color
        let foo = String::from("foo").red();
        assert_eq!(String::from("foo"), *foo);

        // Update color
        assert_eq!("\u{1b}[1;91m\u{1b}[0m", "".black().red().to_string());

        // List all colors
        assert_eq!("\u{1b}[1;91m\u{1b}[0m", "".red().to_string());
        assert_eq!("\u{1b}[1;90m\u{1b}[0m", "".black().to_string());
        assert_eq!("\u{1b}[1;92m\u{1b}[0m", "".green().to_string());
        assert_eq!("\u{1b}[1;93m\u{1b}[0m", "".yellow().to_string());
        assert_eq!("\u{1b}[1;94m\u{1b}[0m", "".blue().to_string());
        assert_eq!("\u{1b}[1;95m\u{1b}[0m", "".magenta().to_string());
        assert_eq!("\u{1b}[1;96m\u{1b}[0m", "".cyan().to_string());
        assert_eq!("\u{1b}[1;97m\u{1b}[0m", "".white().to_string());

        Color::force(Some(false));
        assert!(Color::force_off());
        assert!(!Color::force_on());
        assert_eq!("", "".black().to_string());
        assert_eq!("", "".red().to_string());
        assert_eq!("", "".green().to_string());
        assert_eq!("", "".yellow().to_string());
        assert_eq!("", "".blue().to_string());
        assert_eq!("", "".magenta().to_string());
        assert_eq!("", "".cyan().to_string());
        assert_eq!("", "".white().to_string());

        Color::force(None);
    }
}