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
use std::{borrow::Cow, fmt};
/// Represents ANSI terminal foreground and background colors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
/// Standard ANSI black (`\x1b[30m`).
Black,
/// Standard ANSI red (`\x1b[31m`).
Red,
/// Standard ANSI green (`\x1b[32m`).
Green,
/// Standard ANSI yellow (`\x1b[33m`).
Yellow,
/// Standard ANSI blue (`\x1b[34m`).
Blue,
/// Standard ANSI magenta (`\x1b[35m`).
Magenta,
/// Standard ANSI cyan (`\x1b[36m`).
Cyan,
/// Standard ANSI white (`\x1b[37m`).
White,
/// Standard ANSI bright black (`\x1b[90m`).
BrightBlack,
/// Standard ANSI bright red (`\x1b[91m`).
BrightRed,
/// Standard ANSI bright green (`\x1b[92m`).
BrightGreen,
/// Standard ANSI bright yellow (`\x1b[93m`).
BrightYellow,
/// Standard ANSI bright blue (`\x1b[94m`).
BrightBlue,
/// Standard ANSI bright magenta (`\x1b[95m`).
BrightMagenta,
/// Standard ANSI bright cyan (`\x1b[96m`).
BrightCyan,
/// Standard ANSI bright white (`\x1b[97m`).
BrightWhite,
/// RGB `TrueColor` representation.
Rgb(u8, u8, u8),
/// 256-color palette (8-bit fixed colors).
Fixed(u8),
/// Reset all colors and styles to default.
Reset,
}
impl Color {
/// Returns the ANSI escape sequence for the foreground color.
///
/// # Examples
///
/// ```rust
/// use cirious_codex_term::Color;
///
/// assert_eq!(Color::Red.to_fg_str(), "\x1b[31m");
/// ```
#[must_use]
#[inline]
pub fn to_fg_str(&self) -> Cow<'static, str> {
match self {
Self::Black => Cow::Borrowed("\x1b[30m"),
Self::Red => Cow::Borrowed("\x1b[31m"),
Self::Green => Cow::Borrowed("\x1b[32m"),
Self::Yellow => Cow::Borrowed("\x1b[33m"),
Self::Blue => Cow::Borrowed("\x1b[34m"),
Self::Magenta => Cow::Borrowed("\x1b[35m"),
Self::Cyan => Cow::Borrowed("\x1b[36m"),
Self::White => Cow::Borrowed("\x1b[37m"),
Self::BrightBlack => Cow::Borrowed("\x1b[90m"),
Self::BrightRed => Cow::Borrowed("\x1b[91m"),
Self::BrightGreen => Cow::Borrowed("\x1b[92m"),
Self::BrightYellow => Cow::Borrowed("\x1b[93m"),
Self::BrightBlue => Cow::Borrowed("\x1b[94m"),
Self::BrightMagenta => Cow::Borrowed("\x1b[95m"),
Self::BrightCyan => Cow::Borrowed("\x1b[96m"),
Self::BrightWhite => Cow::Borrowed("\x1b[97m"),
// Apenas RGB e Fixed pagam o preço da alocação (Owned)
Self::Rgb(r, g, b) => Cow::Owned(format!("\x1b[38;2;{r};{g};{b}m")),
Self::Fixed(n) => Cow::Owned(format!("\x1b[38;5;{n}m")),
Self::Reset => Cow::Borrowed("\x1b[39m"),
}
}
/// Returns the ANSI escape sequence for the background color.
///
/// # Examples
///
/// ```rust
/// use cirious_codex_term::Color;
///
/// assert_eq!(Color::Green.to_bg_str(), "\x1b[42m");
/// ```
#[must_use]
#[inline]
pub fn to_bg_str(&self) -> Cow<'static, str> {
match self {
Self::Black => Cow::Borrowed("\x1b[40m"),
Self::Red => Cow::Borrowed("\x1b[41m"),
Self::Green => Cow::Borrowed("\x1b[42m"),
Self::Yellow => Cow::Borrowed("\x1b[43m"),
Self::Blue => Cow::Borrowed("\x1b[44m"),
Self::Magenta => Cow::Borrowed("\x1b[45m"),
Self::Cyan => Cow::Borrowed("\x1b[46m"),
Self::White => Cow::Borrowed("\x1b[47m"),
Self::BrightBlack => Cow::Borrowed("\x1b[100m"),
Self::BrightRed => Cow::Borrowed("\x1b[101m"),
Self::BrightGreen => Cow::Borrowed("\x1b[102m"),
Self::BrightYellow => Cow::Borrowed("\x1b[103m"),
Self::BrightBlue => Cow::Borrowed("\x1b[104m"),
Self::BrightMagenta => Cow::Borrowed("\x1b[105m"),
Self::BrightCyan => Cow::Borrowed("\x1b[106m"),
Self::BrightWhite => Cow::Borrowed("\x1b[107m"),
Self::Rgb(r, g, b) => Cow::Owned(format!("\x1b[48;2;{r};{g};{b}m")),
Self::Fixed(n) => Cow::Owned(format!("\x1b[48;5;{n}m")),
Self::Reset => Cow::Borrowed("\x1b[49m"),
}
}
/// Writes the foreground ANSI escape sequence directly to the formatter.
///
/// This method avoids the allocation of `to_fg_str` by formatting the escape
/// sequence directly into the formatter.
///
/// # Examples
///
/// ```rust
/// use cirious_codex_term::Color;
///
/// let mut f = String::new();
/// Color::Red.write_fg(&mut f);
/// assert_eq!(f, "\x1b[31m");
/// ```
///
/// # Errors
/// Returns an `std::fmt::Result` if writing to the formatter fails.
///
#[inline]
pub fn write_fg<W: fmt::Write>(self, f: &mut W) -> fmt::Result {
match self {
Self::Rgb(r, g, b) => write!(f, "\x1b[38;2;{r};{g};{b}m"),
Self::Fixed(n) => write!(f, "\x1b[38;5;{n}m"),
_ => write!(f, "{}", self.to_fg_str()),
}
}
/// Writes the background ANSI escape sequence directly to the formatter.
///
/// This method avoids the allocation of `to_bg_str` by formatting the escape
/// sequence directly into the formatter.
///
/// # Examples
///
/// ```rust
/// use cirious_codex_term::Color;
///
/// let mut f = String::new();
/// Color::Red.write_bg(&mut f);
/// assert_eq!(f, "\x1b[41m");
/// ```
///
/// # Errors
/// Returns an `std::fmt::Result` if writing to the formatter fails.
///
#[inline]
pub fn write_bg<W: fmt::Write>(self, f: &mut W) -> fmt::Result {
match self {
Self::Rgb(r, g, b) => write!(f, "\x1b[48;2;{r};{g};{b}m"),
Self::Fixed(n) => write!(f, "\x1b[48;5;{n}m"),
_ => write!(f, "{}", self.to_bg_str()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_standard_colors() {
assert_eq!(Color::Red.to_fg_str(), "\x1b[31m");
assert_eq!(Color::Green.to_bg_str(), "\x1b[42m");
}
#[test]
fn test_rgb_colors() {
// Adapt according to your RGB struct/methods
let rgb_color = Color::Rgb(255, 100, 50);
assert_eq!(rgb_color.to_fg_str(), "\x1b[38;2;255;100;50m");
}
}