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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! Colol is a supersimple terminal color library.
//! It's almost completely macro based,
//! and has little overhead. It consists of a
//! struct of 2-long arrays of `u8` integers,
//! and two macros. The first macro, `color!`,
//! sets the color or formatting of the terminal.
//! For example:
//! ```
//! # #[macro_use] extern crate colol;
//! # fn main() {
//! color!(red);
//! println!("Red text!");
//! color!(reset);
//! # }
//! ```
//! However, you may find that you don't want to
//! reset everything to default. This is where the
//! second macro comes in: `close_color!`.
//! This will reset only a specific formatting.
//! For example:
//! ```
//! # #[macro_use] extern crate colol;
//! # fn main() {
//! color!(red);
//! color!(bg_green);
//! println!("Red text with a green background!");
//! close_color!(bg_green);
//! println!("Red text with no green background!");
//! close_color!(red);
//! # }
//! ```
//! Pretty good!
//!
//! The reason this has an advantage over the
//! multitude of other terminal color crates out there
//! is that this is significantly more lightweight, and
//! does not incur much runtime cost at all.
//! It is also useful for when you are printing out large amounts
//! of individual strings, like so:
//! ```
//! # #[macro_use] extern crate colol;
//! # macro_rules! loopn {
//! #   ($n:expr, $body:block) => {
//! #       for _ in 0..$n {
//! #           $body
//! #       }
//! #   }
//! # }
//! # fn main() {
//! color!(red);
//! loopn!(5, {
//!     println!("Hello world!");
//! });
//! close_color!(red);
//! # }
//! ```
//! For a list of colors, see `colors::COLORS`.

pub mod colors;

#[cfg(feature = "colol")]
#[macro_export]
macro_rules! color {
    ($name: ident) => {
        print!("\u{1B}[{}m", $crate::colors::COLORS.$name[0]);
    };
}

#[cfg(feature = "colol")]
#[macro_export]
macro_rules! close_color {
    ($name: ident) => {
        print!("\u{1B}[{}m", $crate::colors::COLORS.$name[1]);
    };
}

#[cfg(not(feature = "colol"))]
#[macro_export]
macro_rules! color {
    ($name: ident) => {};
}

#[cfg(not(feature = "colol"))]
#[macro_export]
macro_rules! close_color {
    ($name: ident) => {};
}

#[cfg(test)]
mod tests {
    #[test]
    fn is_enabled() {
        assert_eq!(true, cfg!(feature = "colol"));
    }

    #[test]
    fn basic() {
        color!(blue);
        println!("Test basic!");
        color!(reset);
    }

    #[test]
    fn closing_colors() {
        color!(blue);
        println!("Test closing colors!");
        close_color!(blue);
    }

    #[test]
    fn multiple_colors() {
        color!(blue);
        print!("Test ");
        close_color!(blue);
        color!(red);
        color!(underline);
        print!("multiple");
        close_color!(underline);
        println!(" colors!");
        close_color!(red);
    }

    #[test]
    fn logo() {
        color!(bold);
        color!(bg_red);
        color!(white);
        print!("co");
        close_color!(bg_red);
        color!(bg_blue);
        println!("lol");
        close_color!(bg_blue);
        close_color!(bold);
    }

    #[test]
    fn every_color() {
        color!(bold);
        println!("bold");
        close_color!(bold);

        color!(dim);
        println!("dim");
        close_color!(dim);

        color!(italic);
        println!("italic");
        close_color!(italic);

        color!(underline);
        println!("underline");
        close_color!(underline);

        color!(inverse);
        println!("inverse");
        close_color!(inverse);

        color!(hidden);
        println!("hidden");
        close_color!(hidden);

        color!(strikethrough);
        println!("strikethrough");
        close_color!(strikethrough);

        color!(black);
        println!("black");
        close_color!(black);

        color!(red);
        println!("red");
        close_color!(red);

        color!(green);
        println!("green");
        close_color!(green);

        color!(yellow);
        println!("yellow");
        close_color!(yellow);

        color!(blue);
        println!("blue");
        close_color!(blue);

        color!(magenta);
        println!("magenta");
        close_color!(magenta);

        color!(cyan);
        println!("cyan");
        close_color!(cyan);

        color!(white);
        println!("white");
        close_color!(white);

        color!(gray);
        println!("gray");
        close_color!(gray);

        color!(grey);
        println!("grey");
        close_color!(grey);

        color!(lt_black);
        println!("lt_black");
        close_color!(lt_black);

        color!(lt_red);
        println!("lt_red");
        close_color!(lt_red);

        color!(lt_green);
        println!("lt_green");
        close_color!(lt_green);

        color!(lt_yellow);
        println!("lt_yellow");
        close_color!(lt_yellow);

        color!(lt_blue);
        println!("lt_blue");
        close_color!(lt_blue);

        color!(lt_magenta);
        println!("lt_magenta");
        close_color!(lt_magenta);

        color!(lt_cyan);
        println!("lt_cyan");
        close_color!(lt_cyan);

        color!(lt_white);
        println!("lt_white");
        close_color!(lt_white);

        color!(bg_black);
        println!("bg_black");
        close_color!(bg_black);

        color!(bg_red);
        println!("bg_red");
        close_color!(bg_red);

        color!(bg_green);
        println!("bg_green");
        close_color!(bg_green);

        color!(bg_yellow);
        println!("bg_yellow");
        close_color!(bg_yellow);

        color!(bg_blue);
        println!("bg_blue");
        close_color!(bg_blue);

        color!(bg_magenta);
        println!("bg_magenta");
        close_color!(bg_magenta);

        color!(bg_cyan);
        println!("bg_cyan");
        close_color!(bg_cyan);

        color!(bg_white);
        println!("bg_white");
        close_color!(bg_white);

        color!(bg_lt_black);
        println!("bg_lt_black");
        close_color!(bg_lt_black);

        color!(bg_lt_red);
        println!("bg_lt_red");
        close_color!(bg_lt_red);

        color!(bg_lt_green);
        println!("bg_lt_green");
        close_color!(bg_lt_green);

        color!(bg_lt_yellow);
        println!("bg_lt_yellow");
        close_color!(bg_lt_yellow);

        color!(bg_lt_blue);
        println!("bg_lt_blue");
        close_color!(bg_lt_blue);

        color!(bg_lt_magenta);
        println!("bg_lt_magenta");
        close_color!(bg_lt_magenta);

        color!(bg_lt_cyan);
        println!("bg_lt_cyan");
        close_color!(bg_lt_cyan);

        color!(bg_lt_white);
        println!("bg_lt_white");
        close_color!(bg_lt_white);
    }
}