style_term/color/
mod.rs

1pub mod classic_term;
2pub mod eight_bit;
3pub mod true_color;
4
5use std::fmt::{Display, Formatter};
6pub use crate::color::classic_term::FourBitColor;
7pub use crate::color::eight_bit::EightBitColor;
8pub use crate::color::true_color::TrueColor;
9
10/// Default for Colors.
11/// By default they will convert into 8 Bit Color
12/// If you would like them to auto convert into 4 bit color. Enable the feature `default_four_bit`
13pub enum DefaultColor {
14    /// 0 on 8 bit color
15    Black,
16    /// 0 on 8 bit color
17
18    Red,
19    /// 1 on 8 bit color
20
21    Green,
22    /// 2 on 8 bit color
23
24    Yellow,
25    /// 3 on 8 bit color
26
27    Blue,
28    /// 4 on 8 bit color
29
30    Magenta,
31    /// 5 on 8 bit color
32
33    Cyan,
34    /// 6 on 8 bit color
35
36    White,
37    /// 7 on 8 bit color
38
39    Gray,
40    /// 8 on 8 bit color
41
42    BrightRed,
43    /// 9 on 8 bit color
44
45    BrightGreen,
46    /// 10 on 8 bit color
47
48    BrightYellow,
49    /// 11 on 8 bit color
50
51    BrightBlue,
52    /// 12 on 8 bit color
53
54    BrightMagenta,
55    /// 13 on 8 bit color
56
57    BrightCyan,
58    /// 14 on 8 bit color
59
60    BrightWhite,
61}
62
63
64/// Converts the type into a Color
65/// For the default implementation to work. Display must be implemented with the return type that is compatible with `\x1b[{38 || 48};{VALUE}m`
66///
67/// **The functions below will not check for NO_COLOR automatically. It is recommended that you do this yourself**
68pub trait DisplayColor: Display + Into<Color> {
69    /// Formats the ansi escape code for the background
70    /// On the True Color and Eight Bit it will be the Format `\x1b[48;{}m` on the four bit it will use the `\x1b[{}m`
71    ///
72    /// This is used when you want to push a direct write to the formatter. If you don't have a formatter. Use [background_color](DisplayColor::background_color)
73    fn fmt_background(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74        write!(f, "\x1b[48;{}m", self)
75    }
76    /// Formats the ansi escape code for the normal color
77    /// On the True Color and Eight Bit it will be the Format `\x1b[38;{}m` on the four bit it will use the `\x1b[{}m`
78    ///
79    /// This is used when you want to push a direct write to the formatter. If you don't have a formatter. Use [color](DisplayColor::color)
80    fn fmt_color(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
81        write!(f, "\x1b[38;{}m", self)
82    }
83    /// Creates the ANSI escape code as a string
84    fn background_color(&self) -> String {
85        format!("\x1b[48;{}m", self)
86    }
87    /// Creates the ANSI escape code as a string for the text color
88    fn color(&self) -> String {
89        format!("\x1b[38;{}m", self)
90    }
91}
92
93#[derive(Debug, Clone)]
94#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
95#[cfg_attr(feature = "serde", serde(tag = "color_type", content = "value"))]
96/// The Color allowing for the different implementations where needed
97pub enum Color {
98    /// A True Color terminal color
99    TrueColor(TrueColor),
100    /// Eight Bit Color format
101    EightBitColor(EightBitColor),
102    /// Classic Four Bit Color format.
103    FourBitColor(FourBitColor),
104}
105
106
107impl DisplayColor for Color {
108    fn fmt_background(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
109        match self {
110            Color::TrueColor(color) => { color.fmt_background(f) }
111            Color::EightBitColor(color) => { color.fmt_background(f) }
112            Color::FourBitColor(color) => { color.fmt_background(f) }
113        }
114    }
115    fn fmt_color(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
116        match self {
117            Color::TrueColor(color) => { color.fmt_color(f) }
118            Color::EightBitColor(color) => { color.fmt_color(f) }
119            Color::FourBitColor(color) => { color.fmt_color(f) }
120        }
121    }
122}
123
124impl Display for Color {
125    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
126        match self {
127            Color::TrueColor(color) => {
128                color.fmt(f)
129            }
130            Color::EightBitColor(color) => {
131                color.fmt(f)
132            }
133            Color::FourBitColor(color) => {
134                color.fmt(f)
135            }
136        }
137    }
138}
139
140