color_string/
color.rs

1use crate::font::Font;
2use std::fmt::Display;
3
4/// color_string 快速生成彩色字符串
5/// # Examples
6/// ```
7/// use color_string::cs;
8/// use color_string::Font::*;
9/// let s = cs!(Red, Bold; "{:X}", u32::MAX);
10/// println!("{s}");
11/// assert_eq!("\u{1b}[31;1mFFFFFFFF\u{1b}[0m", s);
12///
13/// let s = cs!{
14///     Red => 123456, "\n";
15///     Green,Bold => "hello world";
16/// };
17/// println!("{s}");
18/// assert_eq!("\u{1b}[31m123456\n\u{1b}[32;1mhello world\u{1b}[0m", s);
19/// ```
20#[macro_export]
21macro_rules! cs {
22     ($($arg:tt)*) => {{
23        let mut s = String::new();
24        $crate::wcs!(&mut s, $($arg)*);
25        s
26    }};
27}
28
29/// 输出彩色字体到标准输出
30/// # Examples
31/// ```
32/// use color_string::pcs;
33/// use color_string::Font::*;
34/// pcs!(Red => "hello world");
35/// pcs!(Red; "{} {}","hello","world");
36/// ```
37#[macro_export]
38macro_rules! pcs {
39    ($($arg:tt)*) => {{
40        let s = $crate::cs!($($arg)*);
41        println!("{s}")
42    }};
43}
44
45/// 输出彩色字体到标准错误
46/// ```
47/// use color_string::epcs;
48/// use color_string::Font::*;
49/// epcs!(Red => "hello world");
50/// epcs!(Red; "{} {}","hello","world");
51/// ```
52#[macro_export]
53macro_rules! epcs {
54    ($($arg:tt)*) => {{
55        let s = $crate::cs!($($arg)*);
56        eprintln!("{s}")
57    }};
58}
59
60/// write_color_string 写入彩色字符串
61/// # Examples
62/// ```
63/// use color_string::wcs;
64/// use color_string::Font::*;
65/// let mut s = String::new();
66/// wcs!(&mut s, Red, Bold; "{:X}", u32::MAX);
67/// println!("{s}");
68/// assert_eq!("\u{1b}[31;1mFFFFFFFF\u{1b}[0m", s);
69///
70/// let mut s = String::new();
71/// wcs!{
72///     &mut s,
73///     Red => 123456, "\n";
74///     Green,Bold => "hello world";
75/// }
76/// println!("{s}");
77/// assert_eq!("\u{1b}[31m123456\n\u{1b}[32;1mhello world\u{1b}[0m", s);
78/// ```
79#[macro_export]
80macro_rules! wcs {
81     ($buf:expr, $($font:expr),* ; $($arg:tt)*) => {{
82        use std::fmt::Write;
83        $crate::wf!($buf, $($font),*);
84        write!($buf, $($arg)*).unwrap();
85        $buf.push_str("\x1b[0m");
86    }};
87
88    ($buf:expr, $($($font:expr),* => $($s:expr),* );* $(;)?) => {{
89        use std::fmt::Write;
90        $(
91            $crate::wf!($buf, $($font),*);
92            $(write!($buf, "{}", $s).unwrap();)*
93        )*
94        $buf.push_str("\x1b[0m");
95    }};
96}
97
98macro_rules! colored_trait {
99    ($($method:ident => $font:expr),*) => {
100        pub trait Colored:Display {
101            $(
102                fn $method(&self) -> String {
103                    $crate::cs!($font => self)
104                }
105            )*
106
107            fn color(&self, r:u8, g:u8, b:u8) -> String {
108                $crate::cs!($crate::color::Font::Color(r,g,b) => self)
109            }
110
111            fn bg_color(&self, r:u8, g:u8, b:u8) -> String {
112                $crate::cs!($crate::color::Font::BgColor(r,g,b) => self)
113            }
114
115            /// # Examples
116            /// ```rust,ignore
117            /// println!("{}","test".fonts("\x1b[1;31m"));
118            /// println!("{}","test".fonts(fonts!(Font::Bold,Font::Red)));
119            /// ```
120            #[allow(clippy::ptr_arg)]
121            fn fonts(&self, mut fonts:String) -> String {
122                use std::fmt::Write;
123                write!(&mut fonts, "{}\x1b[0m", self).unwrap();
124                fonts
125            }
126        }
127    };
128}
129
130/// 为所有实现 Display 的数据实现 Colored
131impl<T> Colored for T where T: Display + ?Sized {}
132colored_trait! {
133    bold => Font::Bold,
134    underline => Font::Underline,
135    italic => Font::Italic,
136    reverse => Font::Reverse,
137    delete => Font::Delete,
138    black => Font::Black,
139    red => Font::Red,
140    green => Font::Green,
141    yellow => Font::Yellow,
142    blue => Font::Blue,
143    purple => Font::Purple,
144    cyan => Font::Cyan,
145    grey => Font::Grey,
146    bg_black => Font::BgBlack,
147    bg_red => Font::BgRed,
148    bg_green => Font::BgGreen,
149    bg_yellow => Font::BgYellow,
150    bg_blue => Font::BgBlue,
151    bg_purple => Font::BgPurple,
152    bg_cyan => Font::BgCyan,
153    bg_grey => Font::BgGrey
154}