Skip to content

Commit fd29456

Browse files
author
Thomas Wickham
committed
put colors and style in their own modules
1 parent 492186c commit fd29456

File tree

4 files changed

+110
-68
lines changed

4 files changed

+110
-68
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target
22
Cargo.lock
3+
*.bk

src/color.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#[derive(Debug,PartialEq, Eq)]
3+
pub enum Color {
4+
Black,
5+
Red,
6+
Green,
7+
Yellow,
8+
Blue,
9+
Magenta,
10+
Cyan,
11+
White
12+
}
13+
impl Color {
14+
pub fn to_fg_str(&self) -> &str {
15+
match *self {
16+
Color::Black => "30",
17+
Color::Red => "31",
18+
Color::Green => "32",
19+
Color::Yellow => "33",
20+
Color::Blue => "34",
21+
Color::Magenta => "35",
22+
Color::Cyan => "36",
23+
Color::White => "37"
24+
}
25+
}
26+
27+
pub fn to_bg_str(&self) -> &str {
28+
match *self {
29+
Color::Black => "40",
30+
Color::Red => "41",
31+
Color::Green => "42",
32+
Color::Yellow => "43",
33+
Color::Blue => "44",
34+
Color::Magenta => "45",
35+
Color::Cyan => "46",
36+
Color::White => "47"
37+
}
38+
}
39+
}

src/lib.rs

+49-68
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,29 @@
77
#[macro_use(expect)]
88
extern crate expectest;
99

10+
mod color;
11+
mod style;
12+
13+
use color::*;
14+
1015
use std::convert::From;
1116
use std::ops::Deref;
1217
use std::string::String;
1318
use std::fmt;
1419

15-
#[derive(Debug,PartialEq, Eq)]
16-
enum Color {
17-
Black,
18-
Red,
19-
Green,
20-
Yellow,
21-
Blue,
22-
Magenta,
23-
Cyan,
24-
White
25-
}
26-
27-
#[derive(Debug,PartialEq, Eq)]
28-
enum Styles {
29-
Clear = 0b0000_0000,
30-
Bold = 0b0000_0001,
31-
Underline = 0b0000_0010,
32-
Reversed = 0b0000_0100,
33-
Italic = 0b0000_1000,
34-
Blink = 0b0001_0000,
35-
Hidden = 0b0010_0000
36-
}
37-
38-
#[derive(Debug,PartialEq,Eq)]
39-
struct Style(u8);
40-
4120
/// Colored mean both color or styled
4221
#[derive(Debug,PartialEq, Eq)]
4322
pub struct ColoredString {
4423
input: String,
4524
fgcolor: Option<Color>,
4625
bgcolor: Option<Color>,
47-
style: Style
26+
style: style::Style
4827
}
4928

5029
pub trait Colorize {
5130
// Colors
52-
//fn black(self) -> ColoredString;
31+
fn black(self) -> ColoredString;
5332
fn red(self) -> ColoredString;
54-
/*
5533
fn green(self) -> ColoredString;
5634
fn yellow(self) -> ColoredString;
5735
fn blue(self) -> ColoredString;
@@ -60,6 +38,7 @@ pub trait Colorize {
6038
fn cyan(self) -> ColoredString;
6139
fn white(self) -> ColoredString;
6240
// Styles
41+
/*
6342
fn clear(self) -> ColoredString;
6443
fn normal(self) -> ColoredString;
6544
fn bold(self) -> ColoredString;
@@ -75,7 +54,7 @@ pub trait Colorize {
7554
impl ColoredString {
7655
pub fn is_plain(&self) -> bool {
7756
(self.bgcolor.is_none() && self.fgcolor.is_none()
78-
&& self.style == Style(Styles::Clear as u8))
57+
&& self.style == style::CLEAR)
7958
}
8059
}
8160

@@ -85,7 +64,7 @@ impl Default for ColoredString {
8564
input: String::default(),
8665
fgcolor: None,
8766
bgcolor: None,
88-
style: Style(0)
67+
style: style::CLEAR
8968
}
9069
}
9170
}
@@ -105,58 +84,60 @@ impl<'a> From<&'a str> for ColoredString {
10584
}
10685
}
10786

87+
macro_rules! def_color {
88+
($name: ident => $color: path) => {
89+
fn $name(self) -> ColoredString {
90+
ColoredString {
91+
fgcolor: Some($color), .. self
92+
}
93+
}
94+
};
95+
}
96+
10897
impl Colorize for ColoredString {
98+
def_color!(black => Color::Black);
10999
fn red(self) -> ColoredString {
110100
ColoredString {
111101
fgcolor: Some(Color::Red), .. self
112102
}
113103
}
104+
def_color!(green => Color::Green);
105+
def_color!(yellow => Color::Yellow);
106+
def_color!(blue => Color::Blue);
107+
def_color!(magenta => Color::Magenta);
108+
def_color!(purple => Color::Magenta);
109+
def_color!(cyan => Color::Cyan);
110+
def_color!(white => Color::White);
111+
}
112+
113+
macro_rules! def_str_color {
114+
($name: ident => $color: path) => {
115+
fn $name(self) -> ColoredString {
116+
ColoredString {
117+
input: String::from(self),
118+
fgcolor: Some($color),
119+
.. ColoredString::default()
120+
}
121+
}
122+
}
114123
}
115124

116125
impl<'a> Colorize for &'a str {
126+
def_str_color!(black => Color::Black);
117127
fn red(self) -> ColoredString {
118128
ColoredString {
119129
input: String::from(self),
120130
fgcolor: Some(Color::Red),
121131
.. ColoredString::default()
122132
}
123133
}
124-
}
125-
126-
impl Color {
127-
fn to_fg_str(&self) -> &str {
128-
use Color::*;
129-
match *self {
130-
Black => "30",
131-
Red => "31",
132-
Green => "32",
133-
Yellow => "33",
134-
Blue => "34",
135-
Magenta => "35",
136-
Cyan => "36",
137-
White => "37"
138-
}
139-
}
140-
141-
fn to_bg_str(&self) -> &str {
142-
use Color::*;
143-
match *self {
144-
Black => "40",
145-
Red => "41",
146-
Green => "42",
147-
Yellow => "43",
148-
Blue => "44",
149-
Magenta => "45",
150-
Cyan => "46",
151-
White => "47"
152-
}
153-
}
154-
}
155-
156-
impl Style {
157-
fn to_str(&self) -> &str {
158-
unreachable!()
159-
}
134+
def_str_color!(green => Color::Green);
135+
def_str_color!(yellow => Color::Yellow);
136+
def_str_color!(blue => Color::Blue);
137+
def_str_color!(magenta => Color::Magenta);
138+
def_str_color!(purple => Color::Magenta);
139+
def_str_color!(cyan => Color::Cyan);
140+
def_str_color!(white => Color::White);
160141
}
161142

162143
impl fmt::Display for ColoredString {
@@ -168,7 +149,7 @@ impl fmt::Display for ColoredString {
168149

169150
try!(f.write_str("\x1B["));
170151

171-
if self.style != Style(0) {
152+
if self.style != style::CLEAR {
172153
try!(f.write_str(self.style.to_str()));
173154
try!(f.write_str(";"))
174155
}

src/style.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
use std::ops::Deref;
3+
4+
const CLEARV : u8 = 0b0000_0000;
5+
const BOLD : u8 = 0b0000_0001;
6+
const UNDERLINE : u8 = 0b0000_0010;
7+
const REVERSED : u8 = 0b0000_0100;
8+
const ITALIC : u8 = 0b0000_1000;
9+
const BLINK : u8 = 0b0001_0000;
10+
const HIDDEN : u8 = 0b0010_0000;
11+
12+
#[derive(Debug,PartialEq,Eq,Clone,Copy)]
13+
pub struct Style(u8);
14+
15+
impl Style {
16+
pub fn to_str(&self) -> &str {
17+
unreachable!()
18+
}
19+
}
20+
21+
pub static CLEAR : Style = Style(CLEARV);

0 commit comments

Comments
 (0)