-
Notifications
You must be signed in to change notification settings - Fork 3
/
color.d
47 lines (40 loc) · 789 Bytes
/
color.d
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
import std.typecons : Flag;
alias UseColor = Flag!"UseColor";
enum Escapes {
none,
bash,
zsh
}
mixin(makeColorFunction("cyan", 36));
mixin(makeColorFunction("green", 32));
mixin(makeColorFunction("yellow", 33));
mixin(makeColorFunction("red", 31));
mixin(makeColorFunction("resetColor", 39));
private:
string makeColorFunction(string name, int code)
{
import std.conv : to;
return
`
string ` ~ name ~ `(Escapes escapes)
{
string ret = "\33[` ~ code.to!string ~ `m";
final switch (escapes) {
case Escapes.none:
return ret;
case Escapes.bash:
return bashEscape(ret);
case Escapes.zsh:
return zshEscape(ret);
}
}
`;
}
string zshEscape(string code)
{
return "%{" ~ code ~ "%}";
}
string bashEscape(string code)
{
return "\001" ~ code ~ "\002";
}