@@ -9,63 +9,56 @@ pub enum Canvas {
9
9
/// Generates ANSI escape sequences for a specific kind of color
10
10
pub trait FormatColor {
11
11
/// 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 ;
13
13
14
14
/// Undo the color application
15
15
#[ 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 {
17
17
f. write_str ( "\x1B [0m" )
18
18
}
19
19
}
20
20
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 > {
23
23
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
31
26
}
32
27
33
28
/// Adds a foreground or background color
34
29
pub trait Colorable < TFormatColor : FormatColor > : Sized {
35
30
/// Adds the given background color
36
- fn bg ( self , color : TFormatColor ) -> WithBackground < Self , TFormatColor > ;
31
+ fn bg ( self , format_color : TFormatColor ) -> Colored < Self , TFormatColor > ;
37
32
38
33
/// Adds the given foreground color
39
- fn fg ( self , color : TFormatColor ) -> WithForeground < Self , TFormatColor > ;
34
+ fn fg ( self , format_color : TFormatColor ) -> Colored < Self , TFormatColor > ;
40
35
}
41
36
42
37
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
+ }
45
44
}
46
45
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
+ }
49
52
}
50
53
}
51
54
52
55
macro_rules! impl_me {
53
56
( $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 > {
64
58
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)
67
60
. 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 ) )
69
62
}
70
63
}
71
64
} ;
0 commit comments