1+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
2+
3+ static COLOR_ENABLED : AtomicBool = AtomicBool :: new ( true ) ;
4+
5+ pub fn disable_color ( ) {
6+ COLOR_ENABLED . store ( false , Ordering :: Relaxed ) ;
7+ }
8+
9+ pub fn enable_color ( ) {
10+ COLOR_ENABLED . store ( true , Ordering :: Relaxed ) ;
11+ }
12+
13+ #[ inline]
14+ fn color_enabled ( ) -> bool {
15+ COLOR_ENABLED . load ( Ordering :: Relaxed )
16+ }
17+
18+ #[ macro_export]
19+ macro_rules! color {
20+ ( "Black" ) => {
21+ if $crate:: color_enabled( ) { "\x1b [0;30m" } else { "" }
22+ } ;
23+ ( "Red" ) => {
24+ if $crate:: color_enabled( ) { "\x1b [0;31m" } else { "" }
25+ } ;
26+ ( "Green" ) => {
27+ if $crate:: color_enabled( ) { "\x1b [0;32m" } else { "" }
28+ } ;
29+ ( "Orange" ) => {
30+ if $crate:: color_enabled( ) { "\x1b [0;33m" } else { "" }
31+ } ;
32+ ( "Blue" ) => {
33+ if $crate:: color_enabled( ) { "\x1b [0;34m" } else { "" }
34+ } ;
35+ ( "Purple" ) => {
36+ if $crate:: color_enabled( ) { "\x1b [0;35m" } else { "" }
37+ } ;
38+ ( "Cyan" ) => {
39+ if $crate:: color_enabled( ) { "\x1b [0;36m" } else { "" }
40+ } ;
41+ ( "LightGray" ) => {
42+ if $crate:: color_enabled( ) { "\x1b [0;37m" } else { "" }
43+ } ;
44+ ( "DarkGray" ) => {
45+ if $crate:: color_enabled( ) { "\x1b [1;30m" } else { "" }
46+ } ;
47+ ( "LightRed" ) => {
48+ if $crate:: color_enabled( ) { "\x1b [1;31m" } else { "" }
49+ } ;
50+ ( "LightGreen" ) => {
51+ if $crate:: color_enabled( ) { "\x1b [1;32m" } else { "" }
52+ } ;
53+ ( "Yellow" ) => {
54+ if $crate:: color_enabled( ) { "\x1b [1;33m" } else { "" }
55+ } ;
56+ ( "LightBlue" ) => {
57+ if $crate:: color_enabled( ) { "\x1b [1;34m" } else { "" }
58+ } ;
59+ ( "LightPurple" ) => {
60+ if $crate:: color_enabled( ) { "\x1b [1;35m" } else { "" }
61+ } ;
62+ ( "LightCyan" ) => {
63+ if $crate:: color_enabled( ) { "\x1b [1;36m" } else { "" }
64+ } ;
65+ ( "White" ) => {
66+ if $crate:: color_enabled( ) { "\x1b [1;37m" } else { "" }
67+ } ;
68+ ( "nc" ) => {
69+ if $crate:: color_enabled( ) { "\x1b [0m" } else { "" }
70+ } ;
71+ ( "ClearScreen" ) => {
72+ if $crate:: color_enabled( ) { "\x1b c" } else { "" }
73+ } ;
74+ ( $unknown: tt) => {
75+ compile_error!( concat!(
76+ "Unknown color name: '" ,
77+ $unknown,
78+ "'. Valid options are: \
79+ Black, Red, Green, Orange, Blue, Purple, Cyan, LightGray, \
80+ DarkGray, LightRed, LightGreen, Yellow, LightBlue, \
81+ LightPurple, LightCyan, White, nc, ClearScreen"
82+ ) )
83+ } ;
84+ }
0 commit comments