7
7
#[ macro_use( expect) ]
8
8
extern crate expectest;
9
9
10
+ mod color;
11
+ mod style;
12
+
13
+ use color:: * ;
14
+
10
15
use std:: convert:: From ;
11
16
use std:: ops:: Deref ;
12
17
use std:: string:: String ;
13
18
use std:: fmt;
14
19
15
- #[ derive( Debug , PartialEq , Eq ) ]
16
- enum Color {
17
- Black ,
18
- Red ,
19
- Green ,
20
- Yellow ,
21
- Blue ,
22
- Magenta ,
23
- Cyan ,
24
- White
25
- }
26
-
27
- #[ derive( Debug , PartialEq , Eq ) ]
28
- enum Styles {
29
- Clear = 0b0000_0000 ,
30
- Bold = 0b0000_0001 ,
31
- Underline = 0b0000_0010 ,
32
- Reversed = 0b0000_0100 ,
33
- Italic = 0b0000_1000 ,
34
- Blink = 0b0001_0000 ,
35
- Hidden = 0b0010_0000
36
- }
37
-
38
- #[ derive( Debug , PartialEq , Eq ) ]
39
- struct Style ( u8 ) ;
40
-
41
20
/// Colored mean both color or styled
42
21
#[ derive( Debug , PartialEq , Eq ) ]
43
22
pub struct ColoredString {
44
23
input : String ,
45
24
fgcolor : Option < Color > ,
46
25
bgcolor : Option < Color > ,
47
- style : Style
26
+ style : style :: Style
48
27
}
49
28
50
29
pub trait Colorize {
51
30
// Colors
52
- // fn black(self) -> ColoredString;
31
+ fn black ( self ) -> ColoredString ;
53
32
fn red ( self ) -> ColoredString ;
54
- /*
55
33
fn green ( self ) -> ColoredString ;
56
34
fn yellow ( self ) -> ColoredString ;
57
35
fn blue ( self ) -> ColoredString ;
@@ -60,6 +38,7 @@ pub trait Colorize {
60
38
fn cyan ( self ) -> ColoredString ;
61
39
fn white ( self ) -> ColoredString ;
62
40
// Styles
41
+ /*
63
42
fn clear(self) -> ColoredString;
64
43
fn normal(self) -> ColoredString;
65
44
fn bold(self) -> ColoredString;
@@ -75,7 +54,7 @@ pub trait Colorize {
75
54
impl ColoredString {
76
55
pub fn is_plain ( & self ) -> bool {
77
56
( self . bgcolor . is_none ( ) && self . fgcolor . is_none ( )
78
- && self . style == Style ( Styles :: Clear as u8 ) )
57
+ && self . style == style :: CLEAR )
79
58
}
80
59
}
81
60
@@ -85,7 +64,7 @@ impl Default for ColoredString {
85
64
input : String :: default ( ) ,
86
65
fgcolor : None ,
87
66
bgcolor : None ,
88
- style : Style ( 0 )
67
+ style : style :: CLEAR
89
68
}
90
69
}
91
70
}
@@ -105,58 +84,60 @@ impl<'a> From<&'a str> for ColoredString {
105
84
}
106
85
}
107
86
87
+ macro_rules! def_color {
88
+ ( $name: ident => $color: path) => {
89
+ fn $name( self ) -> ColoredString {
90
+ ColoredString {
91
+ fgcolor: Some ( $color) , .. self
92
+ }
93
+ }
94
+ } ;
95
+ }
96
+
108
97
impl Colorize for ColoredString {
98
+ def_color ! ( black => Color :: Black ) ;
109
99
fn red ( self ) -> ColoredString {
110
100
ColoredString {
111
101
fgcolor : Some ( Color :: Red ) , .. self
112
102
}
113
103
}
104
+ def_color ! ( green => Color :: Green ) ;
105
+ def_color ! ( yellow => Color :: Yellow ) ;
106
+ def_color ! ( blue => Color :: Blue ) ;
107
+ def_color ! ( magenta => Color :: Magenta ) ;
108
+ def_color ! ( purple => Color :: Magenta ) ;
109
+ def_color ! ( cyan => Color :: Cyan ) ;
110
+ def_color ! ( white => Color :: White ) ;
111
+ }
112
+
113
+ macro_rules! def_str_color {
114
+ ( $name: ident => $color: path) => {
115
+ fn $name( self ) -> ColoredString {
116
+ ColoredString {
117
+ input: String :: from( self ) ,
118
+ fgcolor: Some ( $color) ,
119
+ .. ColoredString :: default ( )
120
+ }
121
+ }
122
+ }
114
123
}
115
124
116
125
impl < ' a > Colorize for & ' a str {
126
+ def_str_color ! ( black => Color :: Black ) ;
117
127
fn red ( self ) -> ColoredString {
118
128
ColoredString {
119
129
input : String :: from ( self ) ,
120
130
fgcolor : Some ( Color :: Red ) ,
121
131
.. ColoredString :: default ( )
122
132
}
123
133
}
124
- }
125
-
126
- impl Color {
127
- fn to_fg_str ( & self ) -> & str {
128
- use Color :: * ;
129
- match * self {
130
- Black => "30" ,
131
- Red => "31" ,
132
- Green => "32" ,
133
- Yellow => "33" ,
134
- Blue => "34" ,
135
- Magenta => "35" ,
136
- Cyan => "36" ,
137
- White => "37"
138
- }
139
- }
140
-
141
- fn to_bg_str ( & self ) -> & str {
142
- use Color :: * ;
143
- match * self {
144
- Black => "40" ,
145
- Red => "41" ,
146
- Green => "42" ,
147
- Yellow => "43" ,
148
- Blue => "44" ,
149
- Magenta => "45" ,
150
- Cyan => "46" ,
151
- White => "47"
152
- }
153
- }
154
- }
155
-
156
- impl Style {
157
- fn to_str ( & self ) -> & str {
158
- unreachable ! ( )
159
- }
134
+ def_str_color ! ( green => Color :: Green ) ;
135
+ def_str_color ! ( yellow => Color :: Yellow ) ;
136
+ def_str_color ! ( blue => Color :: Blue ) ;
137
+ def_str_color ! ( magenta => Color :: Magenta ) ;
138
+ def_str_color ! ( purple => Color :: Magenta ) ;
139
+ def_str_color ! ( cyan => Color :: Cyan ) ;
140
+ def_str_color ! ( white => Color :: White ) ;
160
141
}
161
142
162
143
impl fmt:: Display for ColoredString {
@@ -168,7 +149,7 @@ impl fmt::Display for ColoredString {
168
149
169
150
try!( f. write_str ( "\x1B [" ) ) ;
170
151
171
- if self . style != Style ( 0 ) {
152
+ if self . style != style :: CLEAR {
172
153
try!( f. write_str ( self . style . to_str ( ) ) ) ;
173
154
try!( f. write_str ( ";" ) )
174
155
}
0 commit comments