|
| 1 | +//! Implements ANSI foreground colors for printable types |
| 2 | +
|
| 3 | +use core::marker::PhantomData; |
| 4 | + |
| 5 | +/// Wrapper struct for generically selecting a color over something to be printed |
| 6 | +pub struct Styled<'a, T: Sized, C> { |
| 7 | + /// Original data to stylize |
| 8 | + original: &'a T, |
| 9 | + |
| 10 | + /// Color of the text |
| 11 | + color: PhantomData<C>, |
| 12 | +} |
| 13 | + |
| 14 | +/// Trait which provides the ANSI color string |
| 15 | +pub trait Color { |
| 16 | + /// The ANSI color string for this color |
| 17 | + const ANSI: &'static str; |
| 18 | +} |
| 19 | + |
| 20 | +/// Creates structs for each ANSI color and implemented the [`Color`] trait for them |
| 21 | +macro_rules! create_color { |
| 22 | + ($color:ident, $bold:expr, $num:expr) => { |
| 23 | + pub struct $color; |
| 24 | + |
| 25 | + impl Color for $color { |
| 26 | + const ANSI: &'static str = |
| 27 | + concat!("\x1b[", stringify!($bold), ";", stringify!($num), "m"); |
| 28 | + } |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +create_color!(Black, 0, 30); |
| 33 | +create_color!(Red, 0, 31); |
| 34 | +create_color!(Green, 0, 32); |
| 35 | +create_color!(Yellow, 0, 33); |
| 36 | +create_color!(Blue, 0, 34); |
| 37 | +create_color!(Magenta, 0, 35); |
| 38 | +create_color!(Cyan, 0, 36); |
| 39 | +create_color!(White, 0, 37); |
| 40 | +create_color!(Normal, 0, 39); |
| 41 | +create_color!(BrightBlack, 0, 90); |
| 42 | +create_color!(BrightRed, 0, 91); |
| 43 | +create_color!(BrightGreen, 0, 92); |
| 44 | +create_color!(BrightYellow, 0, 93); |
| 45 | +create_color!(BrightBlue, 0, 94); |
| 46 | +create_color!(BrightMagenta, 0, 95); |
| 47 | +create_color!(BrightCyan, 0, 96); |
| 48 | +create_color!(BrightWhite, 0, 97); |
| 49 | +create_color!(BoldBlack, 1, 30); |
| 50 | +create_color!(BoldRed, 1, 31); |
| 51 | +create_color!(BoldGreen, 1, 32); |
| 52 | +create_color!(BoldYellow, 1, 33); |
| 53 | +create_color!(BoldBlue, 1, 34); |
| 54 | +create_color!(BoldMagenta, 1, 35); |
| 55 | +create_color!(BoldCyan, 1, 36); |
| 56 | +create_color!(BoldWhite, 1, 37); |
| 57 | +create_color!(BoldNormal, 1, 39); |
| 58 | +create_color!(BoldBrightBlack, 1, 90); |
| 59 | +create_color!(BoldBrightRed, 1, 91); |
| 60 | +create_color!(BoldBrightGreen, 1, 92); |
| 61 | +create_color!(BoldBrightYellow, 1, 93); |
| 62 | +create_color!(BoldBrightBlue, 1, 94); |
| 63 | +create_color!(BoldBrightMagenta, 1, 95); |
| 64 | +create_color!(BoldBrightCyan, 1, 96); |
| 65 | +create_color!(BoldBrightWhite, 1, 97); |
| 66 | + |
| 67 | +/// Implements the various `core::fmt` traits |
| 68 | +macro_rules! impl_formats { |
| 69 | + ($ty:path) => { |
| 70 | + impl<'a, T: $ty, C: Color> $ty for Styled<'a, T, C> { |
| 71 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 72 | + let _ = f.write_str(C::ANSI); |
| 73 | + let _ = <T as $ty>::fmt(&self.original, f); |
| 74 | + f.write_str(Normal::ANSI) |
| 75 | + } |
| 76 | + } |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +/// Implements each function that is available for adding color |
| 81 | +macro_rules! trait_func { |
| 82 | + ($color:ident, $ty:ident) => { |
| 83 | + fn $color(&self) -> Styled<Self, $ty> |
| 84 | + where |
| 85 | + Self: Sized, |
| 86 | + { |
| 87 | + Styled { |
| 88 | + original: self, |
| 89 | + color: PhantomData, |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +/// Provides wrapper functions to apply foreground colors |
| 96 | +pub trait Colorized { |
| 97 | + trait_func!(black, Black); |
| 98 | + trait_func!(red, Red); |
| 99 | + trait_func!(green, Green); |
| 100 | + trait_func!(yellow, Yellow); |
| 101 | + trait_func!(blue, Blue); |
| 102 | + trait_func!(magenta, Magenta); |
| 103 | + trait_func!(cyan, Cyan); |
| 104 | + trait_func!(white, White); |
| 105 | + trait_func!(normal, Normal); |
| 106 | + trait_func!(default, Normal); |
| 107 | + trait_func!(bright_black, BrightBlack); |
| 108 | + trait_func!(bright_red, BrightRed); |
| 109 | + trait_func!(bright_green, BrightGreen); |
| 110 | + trait_func!(bright_yellow, BrightYellow); |
| 111 | + trait_func!(bright_blue, BrightBlue); |
| 112 | + trait_func!(bright_magenta, BrightMagenta); |
| 113 | + trait_func!(bright_cyan, BrightCyan); |
| 114 | + trait_func!(bright_white, BrightWhite); |
| 115 | + trait_func!(light_black, BrightBlack); |
| 116 | + trait_func!(light_red, BrightRed); |
| 117 | + trait_func!(light_green, BrightGreen); |
| 118 | + trait_func!(light_yellow, BrightYellow); |
| 119 | + trait_func!(light_blue, BrightBlue); |
| 120 | + trait_func!(light_magenta, BrightMagenta); |
| 121 | + trait_func!(light_cyan, BrightCyan); |
| 122 | + trait_func!(light_white, BrightWhite); |
| 123 | + trait_func!(bold_black, BoldBlack); |
| 124 | + trait_func!(bold_red, BoldRed); |
| 125 | + trait_func!(bold_green, BoldGreen); |
| 126 | + trait_func!(bold_yellow, BoldYellow); |
| 127 | + trait_func!(bold_blue, BoldBlue); |
| 128 | + trait_func!(bold_magenta, BoldMagenta); |
| 129 | + trait_func!(bold_cyan, BoldCyan); |
| 130 | + trait_func!(bold_white, BoldWhite); |
| 131 | + trait_func!(bold_normal, BoldNormal); |
| 132 | + trait_func!(bold_default, BoldNormal); |
| 133 | + trait_func!(bold_bright_black, BoldBrightBlack); |
| 134 | + trait_func!(bold_bright_red, BoldBrightRed); |
| 135 | + trait_func!(bold_bright_green, BoldBrightGreen); |
| 136 | + trait_func!(bold_bright_yellow, BoldBrightYellow); |
| 137 | + trait_func!(bold_bright_blue, BoldBrightBlue); |
| 138 | + trait_func!(bold_bright_magenta, BoldBrightMagenta); |
| 139 | + trait_func!(bold_bright_cyan, BoldBrightCyan); |
| 140 | + trait_func!(bold_bright_white, BoldBrightWhite); |
| 141 | + trait_func!(bold_light_black, BoldBrightBlack); |
| 142 | + trait_func!(bold_light_red, BoldBrightRed); |
| 143 | + trait_func!(bold_light_green, BoldBrightGreen); |
| 144 | + trait_func!(bold_light_yellow, BoldBrightYellow); |
| 145 | + trait_func!(bold_light_blue, BoldBrightBlue); |
| 146 | + trait_func!(bold_light_magenta, BoldBrightMagenta); |
| 147 | + trait_func!(bold_light_cyan, BoldBrightCyan); |
| 148 | + trait_func!(bold_light_white, BoldBrightWhite); |
| 149 | +} |
| 150 | + |
| 151 | +impl_formats!(core::fmt::Debug); |
| 152 | +impl_formats!(core::fmt::Display); |
| 153 | +impl_formats!(core::fmt::LowerHex); |
| 154 | + |
| 155 | +// Magic impl which gives any trait that implements Display color functions |
| 156 | +impl<T: core::fmt::Display> Colorized for T {} |
0 commit comments