color_ansi/ansi/mod.rs
1/// The definition of the ANSI colors.
2#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3pub enum AnsiColor {
4 /// The ansi black color, escape code `0;30`.
5 Black,
6 /// The ansi blue color, escape code `0;34`.
7 Blue,
8 /// The ansi green color, escape code `0;32`.
9 Green,
10 /// The ansi cyan color, escape code `0;36`.
11 Red,
12 /// The ansi red color, escape code `0;31`.
13 Cyan,
14 /// The ansi magenta color, escape code `0;35`.
15 Magenta,
16 /// The ansi yellow color, escape code `0;33`.
17 Yellow,
18 /// The ansi white color, escape code `0;37`.
19 White,
20 /// An ANSI color with a value in the range `[0, 255]`.
21 Ansi256(u8),
22 /// An ANSI color with a value in the range `[0, 255]`.
23 Rgb(u8, u8, u8),
24}
25
26/// The definition of the ANSI styles.
27#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
28pub struct AnsiStyle {
29 /// The foreground color.
30 pub fg_color: Option<AnsiColor>,
31 /// The background color.
32 pub bg_color: Option<AnsiColor>,
33 /// Get whether this is bold or not.
34 pub bold: bool,
35 /// Get whether this is intense or not.
36 ///
37 /// On Unix-like systems, this will output the ANSI escape sequence
38 /// that will print a high-intensity version of the color
39 /// specified.
40 ///
41 /// On Windows systems, this will output the ANSI escape sequence
42 /// that will print a brighter version of the color specified.
43 pub intense: bool,
44 /// Get whether this is underlined or not.
45 pub underline: bool,
46 /// Get whether this is dimmed or not.
47 ///
48 /// Note that the dimmed setting has no effect in a Windows console.
49 pub dimmed: bool,
50 /// Get whether this is italic or not.
51 ///
52 /// Note that the italic setting has no effect in a Windows console.
53 pub italic: bool,
54 /// Get whether reset is enabled or not.
55 ///
56 /// reset is enabled by default. When disabled and using ANSI escape
57 /// sequences, a "reset" code will be emitted every time a `ColorSpec`'s
58 /// settings are applied.
59 ///
60 /// Note that the reset setting has no effect in a Windows console.
61 pub reset: bool,
62
63 /// Get whether this is strikethrough or not.
64 ///
65 /// Note that the strikethrough setting has no effect in a Windows console.
66 pub strikethrough: bool,
67}
68
69impl AnsiStyle {
70 /// Create a new `ColorSpec` with the foreground color specified.
71 pub fn new(foreground: AnsiColor) -> Self {
72 Self { fg_color: Some(foreground), ..Self::default() }
73 }
74 /// Create a new `ColorSpec` with the foreground color specified.
75 pub fn rgb(r: u8, g: u8, b: u8) -> Self {
76 Self { fg_color: Some(AnsiColor::Rgb(r, g, b)), ..Self::default() }
77 }
78 /// Create a new `ColorSpec` with the foreground color specified.
79 pub fn with_fg_color(self, color: AnsiColor) -> Self {
80 Self { fg_color: Some(color), ..self }
81 }
82 /// Create a new `ColorSpec` with the background color specified.
83 pub fn with_bg_color(self, color: AnsiColor) -> Self {
84 Self { bg_color: Some(color), ..self }
85 }
86 /// Create a new [`AnsiStyle`] with underline enabled.
87 pub fn with_underline(self) -> Self {
88 Self { underline: true, ..self }
89 }
90 /// Create a new [`AnsiStyle`] with dimmed enabled.
91 pub fn with_bold(self) -> Self {
92 Self { bold: true, ..self }
93 }
94 /// Create a new [`AnsiStyle`] with dimmed enabled.
95 pub fn with_italic(self) -> Self {
96 Self { italic: true, ..self }
97 }
98}