Skip to content

Commit 0d77122

Browse files
Combine WithForeground and WithBackground into Colored
1 parent f7d42fb commit 0d77122

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

src/color.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,56 @@ pub enum Canvas {
99
/// Generates ANSI escape sequences for a specific kind of color
1010
pub trait FormatColor {
1111
/// Apply the color
12-
fn prelude(&self, f: &mut fmt::Formatter, canvas: Canvas) -> fmt::Result;
12+
fn prelude(&self, f: &mut fmt::Formatter, canvas: &Canvas) -> fmt::Result;
1313

1414
/// Undo the color application
1515
#[allow(unused)]
16-
fn epilogue(&self, f: &mut fmt::Formatter, canvas: Canvas) -> fmt::Result {
16+
fn epilogue(&self, f: &mut fmt::Formatter, canvas: &Canvas) -> fmt::Result {
1717
f.write_str("\x1B[0m")
1818
}
1919
}
2020

21-
/// Something with a background color
22-
pub struct WithBackground<T, TFormatColor: FormatColor> {
21+
/// Something which has been colored
22+
pub struct Colored<T, TFormatColor: FormatColor> {
2323
t: T,
24-
color: TFormatColor,
25-
}
26-
27-
/// Something with a foreground color
28-
pub struct WithForeground<T, TFormatColor: FormatColor> {
29-
t: T,
30-
color: TFormatColor,
24+
format_color: TFormatColor,
25+
canvas: Canvas
3126
}
3227

3328
/// Adds a foreground or background color
3429
pub trait Colorable<TFormatColor: FormatColor>: Sized {
3530
/// Adds the given background color
36-
fn bg(self, color: TFormatColor) -> WithBackground<Self, TFormatColor>;
31+
fn bg(self, format_color: TFormatColor) -> Colored<Self, TFormatColor>;
3732

3833
/// Adds the given foreground color
39-
fn fg(self, color: TFormatColor) -> WithForeground<Self, TFormatColor>;
34+
fn fg(self, format_color: TFormatColor) -> Colored<Self, TFormatColor>;
4035
}
4136

4237
impl<T, TFormatColor: FormatColor> Colorable<TFormatColor> for T {
43-
fn bg(self, color: TFormatColor) -> WithBackground<Self, TFormatColor> {
44-
WithBackground { t: self, color }
38+
fn bg(self, format_color: TFormatColor) -> Colored<Self, TFormatColor> {
39+
Colored {
40+
t: self,
41+
format_color,
42+
canvas: Canvas::Background
43+
}
4544
}
4645

47-
fn fg(self, color: TFormatColor) -> WithForeground<Self, TFormatColor> {
48-
WithForeground { t: self, color }
46+
fn fg(self, format_color: TFormatColor) -> Colored<Self, TFormatColor> {
47+
Colored {
48+
t: self,
49+
format_color,
50+
canvas: Canvas::Foreground
51+
}
4952
}
5053
}
5154

5255
macro_rules! impl_me {
5356
($bound:path) => {
54-
impl<T: $bound, TFormatColor: FormatColor> $bound for WithBackground<T, TFormatColor> {
55-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56-
self.color
57-
.prelude(f, Canvas::Background)
58-
.and_then(|_| self.t.fmt(f))
59-
.and_then(|_| self.color.epilogue(f, Canvas::Background))
60-
}
61-
}
62-
63-
impl<T: $bound, TFormatColor: FormatColor> $bound for WithForeground<T, TFormatColor> {
57+
impl<T: $bound, TFormatColor: FormatColor> $bound for Colored<T, TFormatColor> {
6458
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65-
self.color
66-
.prelude(f, Canvas::Foreground)
59+
self.format_color.prelude(f, &self.canvas)
6760
.and_then(|_| self.t.fmt(f))
68-
.and_then(|_| self.color.epilogue(f, Canvas::Foreground))
61+
.and_then(|_| self.format_color.epilogue(f, &self.canvas))
6962
}
7063
}
7164
};

src/colors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::fmt;
44
use rgb::RGB8;
55

66
impl FormatColor for RGB8 {
7-
fn prelude(&self, f: &mut fmt::Formatter, canvas: crate::Canvas) -> fmt::Result {
7+
fn prelude(&self, f: &mut fmt::Formatter, canvas: &crate::Canvas) -> fmt::Result {
88
match canvas {
99
Canvas::Foreground => write!(f, "\x1B[38;2;{};{};{}m", self.r, self.g, self.b),
1010
Canvas::Background => write!(f, "\x1B[48;2;{};{};{}m", self.r, self.g, self.b),
@@ -98,7 +98,7 @@ pub enum Color3 {
9898
}
9999

100100
impl FormatColor for Color3 {
101-
fn prelude(&self, f: &mut fmt::Formatter, canvas: crate::Canvas) -> fmt::Result {
101+
fn prelude(&self, f: &mut fmt::Formatter, canvas: &crate::Canvas) -> fmt::Result {
102102
match canvas {
103103
Canvas::Foreground => write!(f, "\x1B[{}m", 30 + *self as u8),
104104
Canvas::Background => write!(f, "\x1B[{}m", 40 + *self as u8),
@@ -143,7 +143,7 @@ impl Color4 {
143143
}
144144

145145
impl FormatColor for Color4 {
146-
fn prelude(&self, f: &mut fmt::Formatter, canvas: crate::Canvas) -> fmt::Result {
146+
fn prelude(&self, f: &mut fmt::Formatter, canvas: &crate::Canvas) -> fmt::Result {
147147
match canvas {
148148
Canvas::Foreground => write!(
149149
f,
@@ -180,7 +180,7 @@ impl Color8 {
180180
}
181181

182182
impl FormatColor for Color8 {
183-
fn prelude(&self, f: &mut fmt::Formatter, canvas: Canvas) -> fmt::Result {
183+
fn prelude(&self, f: &mut fmt::Formatter, canvas: &Canvas) -> fmt::Result {
184184
write!(
185185
f,
186186
"\x1B[{};5;{}m",

0 commit comments

Comments
 (0)