Skip to content

Commit fc07f26

Browse files
Brevity, and more colors
1 parent b7abd51 commit fc07f26

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

src/background.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::colors::*;
22
use core::fmt;
33

44
pub trait Background: Sized {
5-
fn background(self, rgb: Rgb) -> WithBackground<Self>;
5+
fn bg(self, rgb: Rgb) -> WithBackground<Self>;
66
}
77

88
pub struct WithBackground<T> {
@@ -11,7 +11,7 @@ pub struct WithBackground<T> {
1111
}
1212

1313
impl<T> Background for &T {
14-
fn background(self, rgb: Rgb) -> WithBackground<Self> {
14+
fn bg(self, rgb: Rgb) -> WithBackground<Self> {
1515
WithBackground {
1616
t: self,
1717
rgb

src/colors.rs

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pub struct Rgb {
88
}
99

1010
impl Rgb {
11+
pub const fn default() -> Self {
12+
Self::new(0, 0, 0)
13+
}
14+
1115
pub const fn new(r: u8, g: u8, b: u8) -> Self {
1216
Self {
1317
r,
@@ -23,46 +27,58 @@ impl Default for Rgb {
2327
}
2428
}
2529

26-
pub struct Colors();
30+
pub const fn white() -> Rgb {
31+
Rgb::new(255, 255, 255)
32+
}
2733

28-
impl Colors {
29-
pub fn white() -> Rgb {
30-
Rgb::new(255, 255, 255)
31-
}
34+
pub const fn black() -> Rgb {
35+
Rgb::default()
36+
}
3237

33-
pub fn black() -> Rgb {
34-
Default::default()
35-
}
38+
pub const fn red() -> Rgb {
39+
Rgb::new(255, 0, 0)
40+
}
3641

37-
pub fn red() -> Rgb {
38-
Rgb::new(255, 0, 0)
39-
}
42+
pub const fn orange() -> Rgb {
43+
Rgb::new(255, 128, 0)
44+
}
4045

41-
pub fn orange() -> Rgb {
42-
Rgb::new(255, 127, 0)
43-
}
46+
pub const fn yellow() -> Rgb {
47+
Rgb::new(255, 255, 0)
48+
}
4449

45-
pub fn yellow() -> Rgb {
46-
Rgb::new(255, 255, 0)
47-
}
50+
pub const fn yellow_green() -> Rgb {
51+
Rgb::new(128, 255, 0)
52+
}
4853

49-
pub fn green() -> Rgb {
50-
Rgb::new(0, 255, 0)
51-
}
54+
pub const fn green() -> Rgb {
55+
Rgb::new(0, 255, 0)
56+
}
5257

53-
pub fn cyan() -> Rgb {
54-
Rgb::new(0, 255, 255)
55-
}
58+
pub const fn green_cyan() -> Rgb {
59+
Rgb::new(0, 255, 128)
60+
}
5661

57-
pub fn blue() -> Rgb {
58-
Rgb::new(0, 0, 255)
59-
}
62+
pub const fn cyan() -> Rgb {
63+
Rgb::new(0, 255, 255)
64+
}
6065

61-
pub fn purple() -> Rgb {
62-
Rgb::new(127, 0, 127)
63-
}
66+
pub const fn cyan_blue() -> Rgb {
67+
Rgb::new(0, 128, 255)
68+
}
6469

65-
pub fn violet() -> Rgb {
66-
Rgb::new(127, 0, 255)
67-
}
70+
pub const fn blue() -> Rgb {
71+
Rgb::new(0, 0, 255)
72+
}
73+
74+
pub const fn blue_magenta() -> Rgb {
75+
Rgb::new(128, 0, 255)
76+
}
77+
78+
pub const fn magenta() -> Rgb {
79+
Rgb::new(255, 0, 255)
80+
}
81+
82+
pub const fn magenta_pink() -> Rgb {
83+
Rgb::new(255, 0, 128)
6884
}

src/foreground.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::colors::*;
22
use core::fmt;
33

44
pub trait Foreground: Sized {
5-
fn foreground(self, rgb: Rgb) -> WithForeground<Self>;
5+
fn fg(self, rgb: Rgb) -> WithForeground<Self>;
66
}
77

88
pub struct WithForeground<T> {
@@ -11,7 +11,7 @@ pub struct WithForeground<T> {
1111
}
1212

1313
impl<T> Foreground for &T {
14-
fn foreground(self, rgb: Rgb) -> WithForeground<Self> {
14+
fn fg(self, rgb: Rgb) -> WithForeground<Self> {
1515
WithForeground {
1616
t: self,
1717
rgb

src/main.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
pub use colored::*;
22

33
fn main() {
4-
println!("{}, {}, {}, {}, {}, {}, {}, {}",
5-
"Red".foreground(Colors::black()).background(Colors::red()),
6-
"Orange".foreground(Colors::black()).background(Colors::orange()),
7-
"Yellow".foreground(Colors::black()).background(Colors::yellow()),
8-
"Green".foreground(Colors::black()).background(Colors::green()),
9-
"Cyan".foreground(Colors::black()).background(Colors::cyan()),
10-
"Blue".foreground(Colors::white()).background(Colors::blue()),
11-
"Purple".foreground(Colors::white()).background(Colors::purple()),
12-
"Violet".foreground(Colors::white()).background(Colors::violet())
4+
println!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
5+
"Red".fg(red()),
6+
"Orange".fg(orange()),
7+
"Yellow".fg(yellow()),
8+
"Yellow green".fg(yellow_green()),
9+
"Green".fg(green()),
10+
"Green cyan".fg(green_cyan()),
11+
"Cyan".fg(cyan()),
12+
"Cyan blue".fg(white()).bg(cyan_blue()),
13+
"Blue".fg(white()).bg(blue()),
14+
"Blue magenta".fg(white()).bg(blue_magenta()),
15+
"Magenta".fg(magenta()),
16+
"Magenta pink".fg(magenta_pink())
1317
);
1418
}

0 commit comments

Comments
 (0)