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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#![cfg_attr(feature="nightly", feature(const_fn))]

//! A dead simple ANSI terminal color painting library.
//!
//! # Usage
//!
//! Usage is best illustrated via a quick example:
//!
//! ```rust
//! use yansi::Paint;
//! use yansi::Color::White;
//!
//! println!("Testing, {}, {}, {}!", Paint::red(1),
//!     Paint::green(2).bold().underline(),
//!     Paint::blue("3").bg(White).italic());
//! ```
//!
//! ## `Paint`
//!
//! The main entry point into this library is the `Paint` type. `Paint`
//! encapsulates a value of any type that implements the `Display` or `Debug`
//! trait. When a `Paint` is `Display`ed or `Debug`ed, the appropriate ANSI
//! escape characters are emitted before and after the wrapped type's `fmt`
//! implementation.
//!
//! `Paint` can be constructed via any of following methods: [`black`], [`red`],
//! [`green`], [`yellow`], [`blue`], [`purple`], [`cyan`], [`white`]. You can
//! also use the [`paint`] method on a given [`Color`] value to construct a
//! `Paint` type. Both of these methods are shown below:
//!
//! ```rust
//! use yansi::Paint;
//! use yansi::Color::Red;
//!
//! println!("I'm {}!", Paint::red("red").bold());
//! println!("I'm also {}!", Red.paint("red").underline());
//! ```
//!
//! [`black`]: struct.Paint.html#method.black,
//! [`red`]: struct.Paint.html#method.red,
//! [`green`]: struct.Paint.html#method.green,
//! [`yellow`]: struct.Paint.html#method.yellow,
//! [`blue`]: struct.Paint.html#method.blue,
//! [`purple`]: struct.Paint.html#method.purple,
//! [`cyan`]: struct.Paint.html#method.cyan,
//! [`white`]: struct.Paint.html#method.white
//! [`paint`]: enum.Color.html#method.paint
//! [`Color`]: enum.Color.html
//!
//! Each of these methods sets the foreground color of the item to be displayed
//! according to the name of the method. Additionally, [`rgb`] and [`fixed`]
//! allow you to customize the foreground color to your liking.
//!
//! [`rgb`]: struct.Paint.html#method.rgb
//! [`fixed`]: struct.Paint.html#method.fixed
//!
//! Finally, [`new`](struct.Paint.html#method.new) creates a `Paint` item
//! _without_ a foreground color applied.
//!
//! ## Styling
//!
//! Modifications to the styling of the item can be added via the followiing
//! chainable builder methods: [`fg`], [`bg`], [`bold`], [`dimmed`], [`italic`],
//! [`underline`], [`blink`], [`invert`], [`hidden`], [`strikethrough`].
//!
//! [`fg`]: struct.Paint.html#method.fg
//! [`bg`]: struct.Paint.html#method.bg
//! [`bold`]: struct.Paint.html#method.bold
//! [`dimmed`]: struct.Paint.html#method.dimmed
//! [`italic`]: struct.Paint.html#method.italic
//! [`underline`]: struct.Paint.html#method.underline
//! [`blink`]: struct.Paint.html#method.blink
//! [`invert`]: struct.Paint.html#method.invert
//! [`hidden`]: struct.Paint.html#method.hidden
//! [`strikethrough`]: struct.Paint.html#method.strikethrough
//!
//! # Disabling
//!
//! On Rust nightly and with the `nightly` feature enabled, painting can be
//! disabled globally via the [`Paint::disable()`] method. When painting is
//! disabled, the `Display` implementation for `Paint` will emit the `Display`
//! of the contained object and nothing else. Painting can be reenabled via the
//! [`Paint::enable()`] method.
//!
//! One potential use of this feature is to allow users to control color ouput
//! via an environment variable. For instance, to disable coloring if the
//! `CLICOLOR` variable is set to `0`, you might write:
//!
//! ```rust
//! # #[cfg(feature = "nightly")]
//! # { if false { // we don't actually want to disable coloring
//! use yansi::Paint;
//!
//! if let Ok(true) = std::env::var("CLICOLOR").map(|v| v == "0") {
//!     Paint::disable();
//! }
//! # } }
//! ```
//!
//! [`Paint::disable()`]: struct.Paint.html#method.disable
//! [`Paint::enable()`]: struct.Paint.html#method.disable
//!
//! # Windows
//!
//! This is an _ANSI_ terminal coloring library. Unless the Windows terminal
//! supports ANSI colors, colors won't display properly on Windows. This is a
//! bummer, I know. If you'd like, `yansi` makes it easy to disable coloring on
//! Windows:
//!
//! ```rust
//! # #[cfg(feature = "nightly")]
//! # { if false { // we don't actually want to disable coloring
//! use yansi::Paint;
//!
//! if cfg!(windows) {
//!     Paint::disable();
//! }
//! # } }
//! ```
//!
//! # Why?
//!
//! Several terminal coloring libraries exist ([`ansi_term`], [`colored`],
//! [`term_painter`], to name a few), begging the question: why yet another?
//! Here are a few reasons:
//!
//!   * This library is _much_ simpler: there are two types! The complete
//!     implementation is under 250 lines of code.
//!   * Like [`term_painter`], but unlike [`ansi_term`], _any_ type implementing
//!     `Display` can be stylized, not only strings.
//!   * Styling can be enabled and disabled on the fly.
//!   * Typically, only one type needs to be imported: `Paint`.
//!   * Zero dependencies. It really is simple.
//!   * The name `yansi` is pretty short.
//!
//! All that being said, this library borrows the general API from the three
//! libraries as well as plenty of code from [`ansi_term`].
//!
//! [`ansi_term`]: https://crates.io/crates/ansi_term
//! [`colored`]: https://crates.io/crates/colored
//! [`term_painter`]: https://crates.io/crates/term-painter

use std::fmt::{self, Display};

#[cfg(test)] mod tests;

#[inline(always)]
fn write_spliced<T: Display>(c: &mut bool, f: &mut fmt::Formatter, t: T) -> fmt::Result {
    if *c {
        write!(f, ";{}", t)
    } else {
        *c = true;
        write!(f, "{}", t)
    }
}

/// An enum representing an ANSI color code.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)]
pub enum Color {
    /// No color has been set. Nothing is changed when applied.
    Unset,

    /// Black #0 (foreground code `30`, background code `40`).
    Black,

    /// Red: #1 (foreground code `31`, background code `41`).
    Red,

    /// Green: #2 (foreground code `32`, background code `42`).
    Green,

    /// Yellow: #3 (foreground code `33`, background code `43`).
    Yellow,

    /// Blue: #4 (foreground code `34`, background code `44`).
    Blue,

    /// Purple: #5 (foreground code `35`, background code `45`).
    Purple,

    /// Cyan: #6 (foreground code `36`, background code `46`).
    Cyan,

    /// White: #7 (foreground code `37`, background code `47`).
    White,

    /// A color number from 0 to 255, for use in 256-color terminals.
    Fixed(u8),

    /// A 24-bit RGB color, as specified by ISO-8613-3.
    RGB(u8, u8, u8),
}

impl Color {
    /// Constructs a new `Paint` structure that encapsulates `item` with the
    /// foreground color set to the color `self`.
    ///
    /// ```rust
    /// use yansi::Color::Blue;
    ///
    /// println!("This is going to be blue: {}", Blue.paint("yay!"));
    /// ```
    #[inline(always)]
    pub fn paint<T>(self, item: T) -> Paint<T> {
        Paint::new(item).fg(self)
    }
}

#[doc(hidden)]
impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Color::Unset => Ok(()),
            Color::Black => write!(f, "0"),
            Color::Red => write!(f, "1"),
            Color::Green => write!(f, "2"),
            Color::Yellow => write!(f, "3"),
            Color::Blue => write!(f, "4"),
            Color::Purple => write!(f, "5"),
            Color::Cyan => write!(f, "6"),
            Color::White => write!(f, "7"),
            Color::Fixed(num) => write!(f, "8;5;{}", num),
            Color::RGB(r, g, b) => write!(f, "8;2;{};{};{}", r, g, b)
        }
    }
}

impl Default for Color {
    #[inline(always)]
    fn default() -> Self {
        Color::Unset
    }
}

#[repr(packed)]
#[derive(Default, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)]
struct Style {
    bold: bool,
    dimmed: bool,
    italic: bool,
    underline: bool,
    blink: bool,
    invert: bool,
    hidden: bool,
    strikethrough: bool,
}

/// A structure encapsulating all of the styling for a given item.
///
/// See the [crate level documentation](./) for usage information.
#[derive(Default, Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)]
pub struct Paint<T> {
    item: T,
    foreground: Color,
    background: Color,
    style: Style,
}

macro_rules! constructors_for {
    ($T:ty, $($name:ident: $color:ident),*) => ($(
        /// Constructs a new `Paint` structure that encapsulates `item` with the
        /// foreground color set to the name of this method.
        ///
        /// ```rust
        /// use yansi::Paint;
        ///
        /// println!("This is going to be blue: {}", Paint::blue("yay!"));
        /// ```
        pub fn $name(item: $T) -> Paint<$T> {
            Paint::new(item).fg(Color::$color)
        }
    )*)
}

macro_rules! style_builder_for {
    ($T:ty, $($name:ident),*) => ($(
            /// Enables the styling corresponding to the name of this method.
            ///
            /// ```rust
            /// use yansi::Paint;
            ///
            /// println!("Red, underlined: {}", Paint::red("beep.").underline());
            /// ```
        #[inline(always)]
        pub fn $name(mut self) -> Paint<$T> {
            self.style.$name = true;
            self
        }
    )*)
}

#[cfg(feature="nightly")] use std::sync::atomic::AtomicBool;
#[cfg(feature="nightly")] use std::sync::atomic::Ordering;

#[cfg(feature="nightly")] static DISABLED: AtomicBool = AtomicBool::new(false);

impl<T> Paint<T> {
    /// Constructs a new `Paint` structure that encapsulates `item`. No styling
    /// is applied.
    ///
    /// ```rust
    /// use yansi::Paint;
    ///
    /// assert_eq!(Paint::new("hello!").to_string(), "hello!".to_string());
    /// ```
    #[inline(always)]
    pub fn new(item: T) -> Paint<T> {
        Paint {
            item: item,
            foreground: Color::default(),
            background: Color::default(),
            style: Style::default()
        }
    }

    constructors_for!(T, black: Black, red: Red, green: Green, yellow: Yellow,
                         blue: Blue, purple: Purple, cyan: Cyan, white: White);

    /// Constructs a new `Paint` structure that encapsulates `item` with the
    /// foreground color set RGB color corresponding to `r`, `g`, `b`.
    ///
    /// ```rust
    /// use yansi::Paint;
    ///
    /// println!("This is going to be funky: {}", Paint::rgb(70, 130, 122, "hi!"));
    /// ```
    #[inline(always)]
    pub fn rgb(r: u8, g: u8, b: u8, item: T) -> Paint<T> {
        Paint::new(item).fg(Color::RGB(r, g, b))
    }

    /// Constructs a new `Paint` structure that encapsulates `item` with the
    /// foreground color set to the fixed color corresponding to `color`.
    ///
    /// ```rust
    /// use yansi::Paint;
    ///
    /// println!("This is going to be funky: {}", Paint::fixed(100, "hi!"));
    /// ```
    #[inline(always)]
    pub fn fixed(color: u8, item: T) -> Paint<T> {
        Paint::new(item).fg(Color::Fixed(color))
    }

    /// Sets the foreground to `color`.
    ///
    /// ```rust
    /// use yansi::Paint;
    /// use yansi::Color::Red;
    ///
    /// println!("Red foreground: {}", Paint::new("hi!").fg(Red));
    /// ```
    #[inline(always)]
    pub fn fg(mut self, color: Color) -> Paint<T> {
        self.foreground = color;
        self
    }

    /// Sets the background to `color`.
    ///
    /// ```rust
    /// use yansi::Paint;
    /// use yansi::Color::Yellow;
    ///
    /// println!("Yellow background: {}", Paint::new("hi!").bg(Yellow));
    /// ```
    #[inline(always)]
    pub fn bg(mut self, color: Color) -> Paint<T> {
        self.background = color;
        self
    }

    style_builder_for!(T, bold, dimmed, italic, underline, blink, invert, hidden, strikethrough);

    #[inline]
    fn is_plain(&self) -> bool {
        self.foreground == Color::default()
            && self.background == Color::default()
            && self.style == Style::default()
    }

    fn styles(&self) -> [bool; 10] {
        [false, self.style.bold, self.style.dimmed, self.style.italic, self.style.underline,
            self.style.blink, false, self.style.invert, self.style.hidden, self.style.strikethrough]
    }

    /// Write any ANSI codes that go *before* a piece of text. These should be
    /// the codes to set the terminal to a different colour or font style.
    fn write_prefix(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // A user may just want a code-free string when no styles are applied.
        if self.is_plain() {
            return Ok(());
        }

        let mut splice = false;
        write!(f, "\x1B[")?;

        for (i, _) in self.styles().iter().enumerate().filter(|&(_, e)| *e) {
            write_spliced(&mut splice, f, i)?;
        }

        if self.background != Color::Unset {
            write_spliced(&mut splice, f, "4")?;
            self.background.fmt(f)?;
        }

        if self.foreground != Color::Unset {
            write_spliced(&mut splice, f, "3")?;
            self.foreground.fmt(f)?;
        }

        // All the codes end with an `m`, because reasons.
        write!(f, "m")
    }

    /// Write any ANSI codes that go *after* a piece of text. These should be
    /// the codes to *reset* the terminal back to its normal colour and style.
    fn write_suffix(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if !self.is_plain() {
            write!(f, "\x1B[0m")?;
        }

        Ok(())
    }
}

#[cfg(feature="nightly")]
impl Paint<()> {
    /// Disables coloring globally.
    ///
    /// This method is only available when the "nightly" feature is enabled.
    ///
    /// ```rust
    /// use yansi::Paint;
    ///
    /// // With coloring enabled, ANSI color codes are emitted.
    /// assert_ne!(Paint::green("go").to_string(), "go".to_string());
    ///
    /// // With coloring disabled, ANSI color codes are _not_ emitted.
    /// Paint::disable();
    /// assert_eq!(Paint::green("go").to_string(), "go".to_string());
    /// ```
    pub fn disable() {
        DISABLED.store(true, Ordering::Release);
    }

    /// Enabled coloring globally. Coloring is enabled by default, so this
    /// method should only be called to _re_ enable coloring.
    ///
    /// This method is only available when the "nightly" feature is enabled.
    ///
    /// ```rust
    /// use yansi::Paint;
    ///
    /// // With coloring disabled, ANSI color codes are _not_ emitted.
    /// Paint::disable();
    /// assert_eq!(Paint::green("go").to_string(), "go".to_string());
    ///
    /// // Reenabling causes color code to be emitted.
    /// Paint::enable();
    /// assert_ne!(Paint::green("go").to_string(), "go".to_string());
    /// ```
    pub fn enable() {
        DISABLED.store(false, Ordering::Release);
    }
}

fn paint_enabled() -> bool {
    #[cfg(feature="nightly")]
    { !DISABLED.load(Ordering::Relaxed) }

    #[cfg(not(feature="nightly"))]
    { true }
}

impl<T: fmt::Display> fmt::Display for Paint<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if paint_enabled() {
            self.write_prefix(f)?;
            self.item.fmt(f)?;
            self.write_suffix(f)
        } else {
            self.item.fmt(f)
        }
    }
}

impl<T: fmt::Debug> fmt::Debug for Paint<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if paint_enabled() {
            self.write_prefix(f)?;
            self.item.fmt(f)?;
            self.write_suffix(f)
        } else {
            self.item.fmt(f)
        }
    }
}