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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! This crate provides macros for ergonomically wrapping text in ANSI control
//! sequences with SGR ("Select Graphic Rendition") parameters. These
//! parameters are used to color text, as well as apply styling such as
//! italics and underlining. Extensive information on the specific sequences
//! is available on the Wikipedia page for [ANSI escape codes].
//!
//! [ANSI escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
//!
//! ## Modes
//!
//! There are three "output modes" to every macro in this crate: Literal,
//! Format, and String. These determine the output type of the macro, and
//! whether it can be called in `const` contexts. Additionally, there are
//! also three "reversion modes": Single, Total, and None. These determine
//! what is to be done at the end of a macro call --- the formatting state
//! that should be *reverted*.
//!
//! ### Output Modes
//!
//! The simplest output mode is Literal Mode. A string literal must be supplied,
//! and all formatting is applied directly, at compile-time. The output of
//! a Literal Mode macro invocation is a string literal, suitable for the
//! value of a `const`, or as input to compile-time macros (such as
//! [`concat!`] or another SGR macro).
//! ```
//! use sgr_macros::*;
//!
//! let green: &'static str = green!("Green Text");
//! assert_eq!(green, "\x1B[32mGreen Text\x1B[39m");
//!
//! let bold: &'static str = sgr_bold!(green!("Bold Green Text"));
//! assert_eq!(bold, "\x1B[1m\x1B[32mBold Green Text\x1B[39m\x1B[22m");
//!
//! let concat: &'static str = concat!(sgr_bold!("Bold Text"), ", Normal Text");
//! assert_eq!(concat, "\x1B[1mBold Text\x1B[22m, Normal Text");
//! ```
//!
//! The second mode is Format Mode. An invocation in this mode will resolve to a
//! call to [`format_args!`]. This will return [`Arguments`] suitable as
//! input to formatting macros such as [`format!`], [`println!`], and
//! [`write!`]. This mode is enabled by placing a `%` sigil at the beginning
//! of the call. After the sigil, a template literal may be provided.
//!
//! [`Arguments`]: std::fmt::Arguments
//! ```
//! use sgr_macros::*;
//!
//! fn lights(number: &str) -> String {
//! format!("There are {} lights.", sgr_uline!(% number))
//! }
//!
//! let text: String = lights("five");
//! assert_eq!(text, "There are \x1B[4mfive\x1B[24m lights.");
//!
//! fn lights_alt(number: &str) -> String {
//! format!("There are {}.", sgr_italic!(%"{} lights", number))
//! }
//!
//! let text: String = lights_alt("four");
//! assert_eq!(text, "There are \x1B[3mfour lights\x1B[23m.");
//! ```
//!
//! The third mode is String Mode. An invocation in this mode will resolve to a
//! call to [`format!`], returning a fully-formed heap-allocated [`String`].
//! This mode is enabled with a `@` sigil at the beginning of the call, and
//! it may also be provided a template literal.
//! ```
//! use sgr_macros::*;
//!
//! fn status(ok: bool, msg: &str) -> String {
//! if ok {
//! blue_bright!(@ msg)
//! } else {
//! red!(@"ERROR: {}", msg)
//! }
//! }
//!
//! let text: String = status(true, "Success.");
//! assert_eq!(text, "\x1B[94mSuccess.\x1B[99m");
//!
//! let text: String = status(false, "System is on fire.");
//! assert_eq!(text, "\x1B[31mERROR: System is on fire.\x1B[39m");
//! ```
//!
//! [`const_format::formatcp!`]: https://docs.rs/const_format/0.2.26/const_format/macro.formatcp.html
//!
//! If the "const" Cargo Feature is enabled, a fourth mode is available: Const
//! Format Mode. An invocation in this mode will resolve to a call to
//! [`const_format::formatcp!`], returning a static string slice. This
//! output is NOT a string literal, however, and is not suitable as input to
//! [`concat!`]. This mode is enabled with a `#` sigil at the beginning of
//! the call.
//! ```
//! #[cfg(feature = "const")] {
//! use sgr_macros::*;
//!
//! const TEXT: &'static str = sgr_italic!(#*,
//! "italic {r} {} {b}",
//! green!(! "green"),
//! b = blue!(! "blue"),
//! r = red!(! "red"),
//! );
//!
//! assert_eq!(
//! TEXT,
//! "\x1B[3mitalic \x1B[31mred \x1B[32mgreen \x1B[34mblue\x1B[m",
//! );
//! }
//! ```
//!
//! ### Reversion Modes
//!
//! By default, the result of every macro in this crate will end with another
//! control sequence that undoes whatever formatting was set at the start.
//! For example, the [`sgr_bold!`] macro will emit a control sequence to
//! set bold intensity, the input parameters to the macro, and then a second
//! control sequence to set normal intensity. Similarly, all coloring macros
//! will set the default text color when they end.
//!
//! Some styles share a revert sequence, meaning that they cannot be safely
//! nested; The end of the inner style will also revert the outer style.
//! This is true of the following groups of macros:
//! - [`sgr_bold!`] and [`sgr_faint!`]
//! - [`sgr_blink!`] and [`sgr_blink2!`]
//! - [`sgr_super!`] and [`sgr_sub!`]
//! - All color macros (basic, indexed, and RGB) that do not end in `*_bg`.
//! - All color macros (basic, indexed, and RGB) that **do** end in `*_bg`.
//!
//! To control the behavior of revert sequences, there are two more sigils: `!`
//! to prevent reverting *any* formatting, and `*` to revert *all*
//! formatting. Like the Output Mode sigils, these are placed at the
//! beginning of a macro call. If an output sigil and a revert sigil are
//! *both* used, the output sigil must be placed first (e.g. `@*` or `%!`).
//! ```
//! use sgr_macros::*;
//!
//! assert_eq!(
//! // Here, several layered formatting codes are applied, and then cleared
//! // individually. This uses a considerable number of bytes compared
//! // to how it might be done manually.
//! sgr_bold!(sgr_italic!(sgr_uline!("WHAM"))),
//! "\x1B[1m\x1B[3m\x1B[4mWHAM\x1B[24m\x1B[23m\x1B[22m",
//! );
//! assert_eq!(
//! // One way to address this is to specify that `sgr_uline!` and
//! // `sgr_italic!` should NOT revert, and that `sgr_bold!` should
//! // revert ALL formatting.
//! sgr_bold!(* sgr_italic!(! sgr_uline!(! "WHAM"))),
//! "\x1B[1m\x1B[3m\x1B[4mWHAM\x1B[m",
//! );
//!
//! assert_eq!(
//! // Here, the color is reset to default twice: Once at the end of Blue,
//! // and again at the end of (Not) Red.
//! red!(@ "Red, {}, (Not) Red", blue!("Blue")),
//! "\x1B[31mRed, \x1B[34mBlue\x1B[39m, (Not) Red\x1B[39m",
//! );
//! assert_eq!(
//! // Here, the color is not reset at the end of Blue, resulting in its
//! // blue formatting spilling out to the end of the string. This sort
//! // of conflict cannot be solved at compilation while using multiple
//! // macros, so it is best to avoid nesting colors whenever possible.
//! red!(@ "Red, {}, Still Blue", blue!(! "Blue")),
//! "\x1B[31mRed, \x1B[34mBlue, Still Blue\x1B[39m",
//! );
//! ```
//!
//! A comma is accepted, but not required, after sigils. This may be helpful for
//! clarity, or in a case of a dereferenced or inverted argument:
//! ```
//! let text: &&str = &"Doubly-Referenced String";
//!
//! assert_eq!(
//! sgr_macros::sgr_bold!(@**text), // Very unclear.
//! "\x1B[1mDoubly-Referenced String\x1B[m",
//! );
//! assert_eq!(
//! sgr_macros::sgr_bold!(@*, *text), // Much clearer.
//! "\x1B[1mDoubly-Referenced String\x1B[m",
//! );
//!
//! let mask: u8 = 0b01001001;
//!
//! assert_eq!(
//! sgr_macros::sgr_uline!(@!!mask), // Very unclear.
//! "\x1B[4m182",
//! );
//! assert_eq!(
//! sgr_macros::sgr_uline!(@!, !mask), // Much clearer.
//! "\x1B[4m182",
//! );
//! ```
//!
//! ## Macros
//!
//! ### Basic Color
//!
//! There are eight fundamental colors supported by SGR: Black, red, green,
//! yellow, blue, magenta, cyan, and white. Each of these 8 colors has a
//! "bright" variant, leading to 16 named colors. In addition, each of these
//! 16 named colors has two macros: One for *foreground* color, and one for
//! *background* color.
//!
//! This results in 32 basic color macros; Four macros for each of the eight
//! fundamental colors.
//! ```
//! assert_eq!(
//! sgr_macros::cyan!("Bright Cyan Text"),
//! "\x1B[36mBright Cyan Text\x1B[39m",
//! );
//! assert_eq!(
//! sgr_macros::cyan_bg!("Text on Bright Cyan"),
//! "\x1B[46mText on Bright Cyan\x1B[49m",
//! );
//! assert_eq!(
//! sgr_macros::cyan_bright!("Bright Cyan Text"),
//! "\x1B[96mBright Cyan Text\x1B[99m",
//! );
//! assert_eq!(
//! sgr_macros::cyan_bright_bg!("Text on Bright Cyan"),
//! "\x1B[106mText on Bright Cyan\x1B[109m",
//! );
//! ```
//!
// //! In addition, [`grey!`] and [`grey_bg!`] are provided as more clearly-named
// //! versions of [`bright_black!`] and [`bright_black_bg!`].
//!
//! ### Indexed Color
//! [8-bit]: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
//!
//! Two macros are provided for [8-bit] SGR color codes: [`color_256!`] and
//! [`color_256_bg!`]. These macros use all the same mode sigils as detailed
//! [above](#modes), but the first argument of the macro must be an 8-bit
//! integer, specifying the color index, followed by a semicolon.
//! ```
//! assert_eq!(
//! sgr_macros::color_256!(173; "Orange Text"),
//! "\x1B[38;5;173mOrange Text\x1B[39m",
//! );
//! assert_eq!(
//! sgr_macros::color_256_bg!(173; "Text on Orange"),
//! "\x1B[48;5;173mText on Orange\x1B[49m",
//! );
//! assert_eq!(
//! sgr_macros::color_256_bg!(173; *, "Text on Orange"),
//! "\x1B[48;5;173mText on Orange\x1B[m",
//! );
//! ```
//!
//! ### RGB Color
//! [24-bit]: https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit
//!
//! Two macros are provided for [24-bit] SGR color codes: [`color_rgb!`] and
//! [`color_rgb_bg!`]. These macros use all the same mode sigils as detailed
//! [above](#modes), but the first argument of the macro must be an RGB
//! color value followed by a semicolon.
//! ```
//! assert_eq!(
//! sgr_macros::color_rgb!(0x420311; "Maroon Text"),
//! "\x1B[38;2;66;3;17mMaroon Text\x1B[39m",
//! );
//! assert_eq!(
//! sgr_macros::color_rgb_bg!(0x420311; "Text on Maroon"),
//! "\x1B[48;2;66;3;17mText on Maroon\x1B[49m",
//! );
//! ```
//!
//! For more information on the RGB color specification, see the documentation
//! on the [`color_rgb!`] macro.
//!
//! ### Style
//!
//! Eleven macros are provided for various "styles" of text. These typically do
//! not alter text color, but some aspects, such as text intensity, may be
//! implemented by a terminal as changing color brightness or vividness.
use TokenStream;
use quote;
use *;
/// Color text with an 8-bit indexed color value.
///
/// # Usage
/// ```
/// assert_eq!(
/// sgr_macros::color_256!(173; "Orange Text"),
/// "\x1B[38;5;173mOrange Text\x1B[39m",
/// );
/// ```
///
/// Refer to the [crate] documentation for more information on more advanced
/// macro syntax.
/// Color the background with an 8-bit indexed color value.
///
/// # Usage
/// ```
/// assert_eq!(
/// sgr_macros::color_256_bg!(173; "Text on Orange"),
/// "\x1B[48;5;173mText on Orange\x1B[49m",
/// );
/// ```
///
/// Refer to the [crate] documentation for more information on more advanced
/// macro syntax.
/// Color text with a 24-bit RGB value.
///
/// # Usage
///
/// There are several accepted formats for the color specification:
/// ```
/// use sgr_macros::*;
///
/// // Integer Literal:
/// assert_eq!(
/// color_rgb!(0xAABBCC; "Blue-Grey Text"),
/// "\x1B[38;2;170;187;204mBlue-Grey Text\x1B[39m",
/// );
///
/// // String Literal:
/// assert_eq!(
/// color_rgb!("#AABBCC"; "Blue-Grey Text"),
/// "\x1B[38;2;170;187;204mBlue-Grey Text\x1B[39m",
/// );
///
/// // String Literal (3-digit):
/// assert_eq!(
/// color_rgb!("#ABC"; "Blue-Grey Text"),
/// "\x1B[38;2;170;187;204mBlue-Grey Text\x1B[39m",
/// );
///
/// // Integer Tuple:
/// assert_eq!(
/// color_rgb!((255, 127, 63); "Orange Text"),
/// "\x1B[38;2;255;127;63mOrange Text\x1B[39m",
/// );
///
/// // Float Tuple:
/// assert_eq!(
/// color_rgb!((1.0, 0.5, 0.25); "Orange Text"),
/// "\x1B[38;2;255;127;63mOrange Text\x1B[39m",
/// );
///
/// // Mixed Tuple:
/// assert_eq!(
/// color_rgb!((0xFF, 127, 0.25); "Orange Text"),
/// "\x1B[38;2;255;127;63mOrange Text\x1B[39m",
/// );
/// ```
///
/// Only three channels are supported, as an Alpha channel is not applicable to
/// text in a terminal. The Integer Literal input format could hold a four
/// byte value, since it is a [`u32`], but use of the top byte will result
/// in a compile error.
///
/// Refer to the [crate] documentation for more information on more advanced
/// macro syntax.
/// Color the background with a 24-bit RGB value.
///
/// Refer to the [`color_rgb!`] macro for more information on the color format.
///
/// Refer to the [crate] documentation for more information on more advanced
/// macro syntax.
)*
};
}
def_sgr!
def_sgr!
def_sgr!
def_sgr!