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
//! Peter builds on the [`ansi_term`] crate to allow styling of anything
//! implementing [`Display`] and makes colorizing text less verbose to use by
//! providing the [`Stylize`] trait.
//!
//! # Examples
//!
//! ```rust
//! use peter::Stylize;
//!
//! println!("This is in red: {}", "a red string".red());
//!
//! println!("How about some {}?", "bold and underline".bold().underline());
//! ```

#![no_std]

use core::fmt;
use core::fmt::Display;
use core::ops::{Deref, DerefMut};

use ansi_term::Style;

/// See [`ansi_term::Color`].
///
pub use ansi_term::Color;

/// Wraps something in a [`Style`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Styled<T> {
    inner: T,
    style: Style,
}

macro_rules! meth_color {
    ($meth:ident, $color:ident) => {
        pub fn $meth(self) -> Self {
            self.fg(Color::$color)
        }
    };
    ($meth:ident, $color:ident($($arg:ident),+)) => {
        pub fn $meth(self, $($arg: u8),+) -> Self {
            self.fg(Color::$color($($arg),+))
        }
    };
}

macro_rules! meth_style {
    ($meth:ident) => {
        pub fn $meth(self) -> Self {
            let Self { inner, style } = self;
            Self {
                inner,
                style: style.$meth(),
            }
        }
    };
    ($meth:ident, $color:ident) => {
        pub fn $meth(self, $color: Color) -> Self {
            let Self { inner, style } = self;
            Self {
                inner,
                style: style.$meth($color),
            }
        }
    };
}

impl<T> Styled<T> {
    /// Construct a new `Styled`.
    fn new(inner: T) -> Self {
        Self {
            inner,
            style: Style::new(),
        }
    }

    // different styles
    meth_style!(bold);
    meth_style!(dimmed);
    meth_style!(italic);
    meth_style!(underline);
    meth_style!(blink);
    meth_style!(reverse);
    meth_style!(hidden);
    meth_style!(strikethrough);
    meth_style!(fg, color);
    meth_style!(on, color);

    // different colors
    meth_color!(black, Black);
    meth_color!(red, Red);
    meth_color!(green, Green);
    meth_color!(yellow, Yellow);
    meth_color!(blue, Blue);
    meth_color!(magenta, Purple);
    meth_color!(cyan, Cyan);
    meth_color!(white, White);
    meth_color!(fixed, Fixed(n));
    meth_color!(rgb, RGB(r, g, b));
}

impl<T> Deref for Styled<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> DerefMut for Styled<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<T: Display> Display for Styled<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.style.prefix())?;
        Display::fmt(&self.inner, f)?;
        write!(f, "{}", self.style.suffix())?;
        Ok(())
    }
}

macro_rules! meth_color {
    ($meth:ident, $color:ident) => {
        fn $meth(&self) -> Styled<&Self> {
            self.fg(Color::$color)
        }
    };
    ($meth:ident, $color:ident($($arg:ident),+)) => {
        fn $meth(&self, $($arg: u8),+) -> Styled<&Self> {
            self.fg(Color::$color($($arg),+))
        }
    };
}

macro_rules! meth_style {
    ($meth:ident) => {
        fn $meth(&self) -> Styled<&Self> {
            Styled::new(self).$meth()
        }
    };
    ($meth:ident, $color:ident) => {
        fn $meth(&self, $color: Color) -> Styled<&Self> {
            Styled::new(self).$meth($color)
        }
    };
}

/// Allows anything implementing [`Display`] to be styled.
pub trait Stylize<T>: Sized {
    // different styles
    meth_style!(bold);
    meth_style!(dimmed);
    meth_style!(italic);
    meth_style!(underline);
    meth_style!(blink);
    meth_style!(reverse);
    meth_style!(hidden);
    meth_style!(strikethrough);
    meth_style!(fg, color);
    meth_style!(on, color);

    // different colors
    meth_color!(black, Black);
    meth_color!(red, Red);
    meth_color!(green, Green);
    meth_color!(yellow, Yellow);
    meth_color!(blue, Blue);
    meth_color!(magenta, Purple);
    meth_color!(cyan, Cyan);
    meth_color!(white, White);
    meth_color!(fixed, Fixed(n));
    meth_color!(rgb, RGB(r, g, b));
}

/// Blanket implementation for everything that implements [`Display`].
impl<T> Stylize<T> for T where T: Display {}