Skip to content

Commit b7abd51

Browse files
Implement all formatting traits
1 parent c2b5422 commit b7abd51

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/background.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ impl<T> Background for &T {
1919
}
2020
}
2121

22-
impl<T> fmt::Display for WithBackground<T>
23-
where T: fmt::Display {
24-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25-
write!(f, "\x1B[48;2;{};{};{}m{}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
26-
}
22+
macro_rules! impl_me {
23+
($bound:path, $format_arg:expr) => {
24+
impl<T> $bound for WithBackground<T>
25+
where T: $bound {
26+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27+
write!(f, concat!("\x1B[48;2;{};{};{}m", $format_arg, "\x1B[0m"), self.rgb.r, self.rgb.g, self.rgb.b, self.t)
28+
}
29+
}
30+
};
2731
}
2832

29-
impl<T> fmt::Debug for WithBackground<T>
30-
where T: fmt::Debug {
31-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32-
write!(f, "\x1B[48;2;{};{};{}m{:?}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
33-
}
34-
}
33+
impl_me!(fmt::Binary, "{:b}");
34+
impl_me!(fmt::Debug, "{:?}");
35+
impl_me!(fmt::Display, "{}");
36+
impl_me!(fmt::LowerExp, "{:e}");
37+
impl_me!(fmt::LowerHex, "{:x}");
38+
impl_me!(fmt::Octal, "{:o}");
39+
impl_me!(fmt::Pointer, "{:p}");
40+
impl_me!(fmt::UpperExp, "{:E}");
41+
impl_me!(fmt::UpperHex, "{:X}");

src/foreground.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ impl<T> Foreground for &T {
1919
}
2020
}
2121

22-
impl<T> fmt::Display for WithForeground<T>
23-
where T: fmt::Display {
24-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25-
write!(f, "\x1B[38;2;{};{};{}m{}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
26-
}
22+
macro_rules! impl_me {
23+
($bound:path, $format_arg:expr) => {
24+
impl<T> $bound for WithForeground<T>
25+
where T: $bound {
26+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27+
write!(f, concat!("\x1B[38;2;{};{};{}m", $format_arg, "\x1B[0m"), self.rgb.r, self.rgb.g, self.rgb.b, self.t)
28+
}
29+
}
30+
};
2731
}
2832

29-
impl<T> fmt::Debug for WithForeground<T>
30-
where T: fmt::Debug {
31-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32-
write!(f, "\x1B[38;2;{};{};{}m{:?}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
33-
}
34-
}
33+
impl_me!(fmt::Binary, "{:b}");
34+
impl_me!(fmt::Debug, "{:?}");
35+
impl_me!(fmt::Display, "{}");
36+
impl_me!(fmt::LowerExp, "{:e}");
37+
impl_me!(fmt::LowerHex, "{:x}");
38+
impl_me!(fmt::Octal, "{:o}");
39+
impl_me!(fmt::Pointer, "{:p}");
40+
impl_me!(fmt::UpperExp, "{:E}");
41+
impl_me!(fmt::UpperHex, "{:X}");

0 commit comments

Comments
 (0)