Skip to content

Commit ff9e504

Browse files
committed
refactor: Use Macros to create basic colors
1 parent d4beec4 commit ff9e504

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

src/styles/basic_color.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,46 @@ impl Stylify for BasicColor {
1919
}
2020
}
2121

22-
pub const BLACK: Styles = Styles::StyleBasicColor(BasicColor { fg: 30, bg: 40 });
23-
pub const RED: Styles = Styles::StyleBasicColor(BasicColor { fg: 31, bg: 41 });
24-
pub const GREEN: Styles = Styles::StyleBasicColor(BasicColor { fg: 32, bg: 42 });
25-
pub const YELLOW: Styles = Styles::StyleBasicColor(BasicColor { fg: 33, bg: 43 });
26-
pub const BLUE: Styles = Styles::StyleBasicColor(BasicColor { fg: 34, bg: 44 });
27-
pub const MAGENTA: Styles = Styles::StyleBasicColor(BasicColor { fg: 35, bg: 45 });
28-
pub const CYAN: Styles = Styles::StyleBasicColor(BasicColor { fg: 36, bg: 46 });
29-
pub const WHITE: Styles = Styles::StyleBasicColor(BasicColor { fg: 37, bg: 47 });
30-
pub const GRAY: Styles = Styles::StyleBasicColor(BasicColor { fg: 90, bg: 10 });
31-
pub const BRIGHT_RED: Styles = Styles::StyleBasicColor(BasicColor { fg: 91, bg: 101 });
32-
pub const BRIGHT_GREEN: Styles = Styles::StyleBasicColor(BasicColor { fg: 92, bg: 102 });
33-
pub const BRIGHT_YELLOW: Styles = Styles::StyleBasicColor(BasicColor { fg: 93, bg: 103 });
34-
pub const BRIGHT_BLUE: Styles = Styles::StyleBasicColor(BasicColor { fg: 94, bg: 104 });
35-
pub const BRIGHT_MAGENTA: Styles = Styles::StyleBasicColor(BasicColor { fg: 95, bg: 105 });
36-
pub const BRIGHT_CYAN: Styles = Styles::StyleBasicColor(BasicColor { fg: 96, bg: 106 });
37-
pub const BRIGHT_WHITE: Styles = Styles::StyleBasicColor(BasicColor { fg: 97, bg: 107 });
22+
macro_rules! color_code {
23+
($name:ident, { fg: $fg:expr, bg: $bg:expr }) => {
24+
pub const $name:Styles = Styles::StyleBasicColor(BasicColor { fg: $fg, bg: $bg });
25+
};
26+
}
27+
28+
color_code!(BLACK,{fg: 30, bg: 40 });
29+
color_code!(RED,{fg: 31, bg: 41});
30+
color_code!(GREEN,{ fg: 32, bg: 42 });
31+
color_code!(YELLOW,{ fg: 33, bg: 43 });
32+
color_code!(BLUE,{fg: 34, bg: 44});
33+
color_code!(MAGENTA,{ fg: 35, bg: 45});
34+
color_code!(CYAN,{fg:36,bg:46});
35+
color_code!(WHITE,{fg:37,bg:47});
36+
color_code!(GRAY,{fg:90,bg:100});
37+
color_code!(BRIGHT_RED,{fg:91,bg:101});
38+
color_code!(BRIGHT_GREEN,{fg:92,bg:102});
39+
color_code!(BRIGHT_YELLOW,{fg:93,bg:103});
40+
color_code!(BRIGHT_BLUE,{fg:94,bg:104});
41+
color_code!(BRIGHT_MAGENTA,{fg:95,bg:105});
42+
color_code!(BRIGHT_CYAN,{fg:96,bg:106});
43+
color_code!(BRIGHT_WHITE,{fg:97,bg:107});
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
#[test]
50+
fn black_color() {
51+
match BLACK {
52+
Styles::StyleBasicColor(BasicColor { fg: 30, bg: 40 }) => assert!(true),
53+
_ => panic!("It's not a `Black` color! black color should have fg:30 and bg:40"),
54+
}
55+
}
56+
57+
#[test]
58+
fn red_color() {
59+
match RED {
60+
Styles::StyleBasicColor(BasicColor { fg: 31, bg: 41 }) => assert!(true),
61+
_ => panic!("It's not a `Red` color! red color should have fg:31 and bg:41"),
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)