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
use core::fmt;
use rgb::RGB8;
pub trait Foreground: Sized {
fn fg(self, rgb: RGB8) -> WithForeground<Self>;
}
pub struct WithForeground<T> {
t: T,
rgb: RGB8
}
impl<T> Foreground for T {
fn fg(self, rgb: RGB8) -> WithForeground<Self> {
WithForeground {
t: self,
rgb
}
}
}
macro_rules! impl_me {
($bound:path, $format_arg:expr) => {
impl<T> $bound for WithForeground<T>
where T: $bound {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, concat!("\x1B[38;2;{};{};{}m", $format_arg, "\x1B[0m"), self.rgb.r, self.rgb.g, self.rgb.b, self.t)
}
}
};
}
impl_me!(fmt::Binary, "{:b}");
impl_me!(fmt::Debug, "{:?}");
impl_me!(fmt::Display, "{}");
impl_me!(fmt::LowerExp, "{:e}");
impl_me!(fmt::LowerHex, "{:x}");
impl_me!(fmt::Octal, "{:o}");
impl_me!(fmt::Pointer, "{:p}");
impl_me!(fmt::UpperExp, "{:E}");
impl_me!(fmt::UpperHex, "{:X}");