-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
skin_conf.rs
85 lines (82 loc) · 2.39 KB
/
skin_conf.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::errors::ConfError;
use regex::Regex;
/// Manage conversion of a user provided string
/// defining foreground and background colors into
/// a string with TTY colors
///
/// The code is ugly. Really. I know.
/// I found no clean way to deal with termion colors.
use std::result::Result;
use termion::color::*;
fn parse_gray(raw: &str) -> Result<Option<u8>, ConfError> {
lazy_static! {
static ref RE: Regex = Regex::new(r"^grayscale\((?P<level>\d+)\)$").unwrap();
}
if let Some(c) = RE.captures(raw) {
if let Some(level) = c.name("level") {
if let Ok(level) = level.as_str().parse() {
return if level < 24 {
Ok(Some(level))
} else {
Err(ConfError::InvalidSkinEntry {
reason: "gray level must be between 0 and 23".to_string(),
})
};
}
}
}
Ok(None)
}
macro_rules! make_parseurs {
(
$($name:tt,)*
) => {
pub fn parse_fg(raw: &str) -> Result<String, ConfError> {
if raw.eq_ignore_ascii_case("none") {
return Ok(format!("{}", Fg(Reset)));
}
$(
if raw.eq_ignore_ascii_case(stringify!($name)) {
return Ok(format!("{}", Fg($name)));
}
)*
if let Some(level) = parse_gray(raw)? {
return Ok(format!("{}", Fg(AnsiValue::grayscale(level))));
}
return Err(ConfError::InvalidSkinEntry{reason:raw.to_string()});
}
pub fn parse_bg(raw: &str) -> Result<String, ConfError> {
if raw.eq_ignore_ascii_case("none") {
return Ok(format!("{}", Bg(Reset)));
}
$(
if raw.eq_ignore_ascii_case(stringify!($name)) {
return Ok(format!("{}", Bg($name)));
}
)*
if let Some(level) = parse_gray(raw)? {
return Ok(format!("{}", Bg(AnsiValue::grayscale(level))));
}
return Err(ConfError::InvalidSkinEntry{reason:raw.to_string()});
}
}
}
make_parseurs! {
Black,
Blue,
Cyan,
Green,
LightBlack,
LightBlue,
LightCyan,
LightGreen,
LightMagenta,
LightRed,
LightWhite,
LightYellow,
Magenta,
Red,
Reset,
White,
Yellow,
}