ansicolor/lib.rs
1pub const Z: &str = "\x1b[0m";
2pub const K: &str = "\x1b[30m";
3pub const R: &str = "\x1b[31m";
4pub const G: &str = "\x1b[32m";
5pub const Y: &str = "\x1b[33m";
6pub const B: &str = "\x1b[34m";
7pub const M: &str = "\x1b[35m";
8pub const C: &str = "\x1b[36m";
9pub const W: &str = "\x1b[37m";
10
11#[doc(hidden)]
12#[macro_export]
13macro_rules! __ac_color {
14 ($c:literal, $e:literal) => { concat!("\x1b[3", $c, "m", $e, "\x1b[0m") };
15 ($c:literal, $e:expr) => { format_args!(concat!("\x1b[3", $c, "m{}\x1b[0m"), $e) };
16 ($c:literal, $($t:tt)*) => { format_args!(concat!("\x1b[3", $c, "m{}\x1b[0m"), format_args!($($t)*)) };
17}
18
19#[macro_export]
20macro_rules! ac_black {
21 ($($t:tt)*) => { $crate::__ac_color!("0", $($t)*) };
22}
23
24/// ## Example:
25/// ```rust
26/// println!("this color is {}.", ansicolor::ac_red!("red"));
27/// println!("this color is {}.", ansicolor::ac_red!(72));
28/// println!("this color is {}.", ansicolor::ac_red!("my name is {}, age is {}", "kiven", 18));
29/// ```
30#[macro_export]
31macro_rules! ac_red {
32 ($($t:tt)*) => { $crate::__ac_color!("1", $($t)*) };
33}
34
35/// ## Example:
36/// ```rust
37/// println!("this color is {}.", ansicolor::ac_green!("green"));
38/// println!("this color is {}.", ansicolor::ac_green!(72));
39/// println!("this color is {}.", ansicolor::ac_green!("my name is {}, age is {}", "kiven", 18));
40/// ```
41#[macro_export]
42macro_rules! ac_green {
43 ($($t:tt)*) => { $crate::__ac_color!("2", $($t)*) };
44}
45
46/// ## Example:
47/// ```rust
48/// println!("this color is {}.", ansicolor::ac_yellow!("yellow"));
49/// println!("this color is {}.", ansicolor::ac_yellow!(72));
50/// println!("this color is {}.", ansicolor::ac_yellow!("my name is {}, age is {}", "kiven", 18));
51/// ```
52#[macro_export]
53macro_rules! ac_yellow {
54 ($($t:tt)*) => { $crate::__ac_color!("3", $($t)*) };
55}
56
57/// ## Example:
58/// ```rust
59/// println!("this color is {}.", ansicolor::ac_blue!("blue"));
60/// println!("this color is {}.", ansicolor::ac_blue!(72));
61/// println!("this color is {}.", ansicolor::ac_blue!("my name is {}, age is {}", "kiven", 18));
62/// ```
63#[macro_export]
64macro_rules! ac_blue {
65 ($($t:tt)*) => { $crate::__ac_color!("4", $($t)*) };
66}
67
68/// ## Example:
69/// ```rust
70/// println!("this color is {}.", ansicolor::ac_magenta!("magenta"));
71/// println!("this color is {}.", ansicolor::ac_magenta!(72));
72/// println!("this color is {}.", ansicolor::ac_magenta!("my name is {}, age is {}", "kiven", 18));
73/// ```
74#[macro_export]
75macro_rules! ac_magenta {
76 ($($t:tt)*) => { $crate::__ac_color!("5", $($t)*) };
77}
78
79/// ## Example:
80/// ```rust
81/// println!("this color is {}.", ansicolor::ac_cyan!("cyan"));
82/// println!("this color is {}.", ansicolor::ac_cyan!(72));
83/// println!("this color is {}.", ansicolor::ac_cyan!("my name is {}, age is {}", "kiven", 18));
84/// ```
85#[macro_export]
86macro_rules! ac_cyan {
87 ($($t:tt)*) => { $crate::__ac_color!("6", $($t)*) };
88}
89
90/// ## Example:
91/// ```rust
92/// println!("this color is {}.", ansicolor::ac_white!("white"));
93/// println!("this color is {}.", ansicolor::ac_white!(72));
94/// println!("this color is {}.", ansicolor::ac_white!("my name is {}, age is {}", "kiven", 18));
95/// ```
96#[macro_export]
97macro_rules! ac_white {
98 ($($t:tt)*) => { $crate::__ac_color!("7", $($t)*) };
99}
100
101pub enum AnsiColor { Z, K, R, G, Y, B, M, C, W }
102
103impl std::fmt::Display for AnsiColor {
104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 let s = match self {
106 AnsiColor::Z => "\x1b[0m", // 重置, reset
107 AnsiColor::K => "\x1b[30m", // 黑, black
108 AnsiColor::R => "\x1b[31m", // 红, red
109 AnsiColor::G => "\x1b[32m", // 绿, green
110 AnsiColor::Y => "\x1b[33m", // 黄, yellow
111 AnsiColor::B => "\x1b[34m", // 蓝, blue
112 AnsiColor::M => "\x1b[35m", // 紫, magenta
113 AnsiColor::C => "\x1b[36m", // 青, cyan
114 AnsiColor::W => "\x1b[37m", // 白, white
115 };
116 write!(f, "{}", s)
117 }
118}
119
120impl std::convert::From<u32> for AnsiColor {
121 fn from(value: u32) -> Self {
122 match value {
123 0 => AnsiColor::Z,
124 1 => AnsiColor::K,
125 2 => AnsiColor::R,
126 3 => AnsiColor::G,
127 4 => AnsiColor::Y,
128 5 => AnsiColor::B,
129 6 => AnsiColor::M,
130 7 => AnsiColor::C,
131 8 => AnsiColor::W,
132 _ => AnsiColor::Z,
133 }
134 }
135}