mirror of
https://github.com/yamadapc/d-colorize
synced 2024-11-14 21:14:00 +01:00
Start drafting string API support and add aliases.
This changes the way colorize works and is still a *really* rough draft. I didn't want to repeat the color codes, but also don't feel much like wasting time making such a trivial use case non-verbose. This relates to #1, but, as I said is still just an API idea, not something I'd like to push to dub yet.
This commit is contained in:
parent
b47a79deb2
commit
4d8473495a
|
@ -81,11 +81,11 @@ unittest
|
|||
|
||||
ret = "This is yellow".colorize(fg.yellow);
|
||||
writeln(ret);
|
||||
assert(ret == "\033[0;33;49mThis is yellow\033[0m");
|
||||
assert(ret == "\033[33mThis is yellow\033[0m");
|
||||
|
||||
ret = "This is light green".colorize(fg.light_green);
|
||||
writeln(ret);
|
||||
assert(ret == "\033[0;92;49mThis is light green\033[0m");
|
||||
assert(ret == "\033[92mThis is light green\033[0m");
|
||||
|
||||
ret = "This is light blue with red background".colorize(fg.light_blue, bg.red);
|
||||
writeln(ret);
|
||||
|
@ -94,18 +94,91 @@ unittest
|
|||
ret = "This is red on blue blinking".colorize(fg.red, bg.blue, mode.blink);
|
||||
writeln(ret);
|
||||
assert(ret == "\033[5;31;44mThis is red on blue blinking\033[0m");
|
||||
|
||||
ret = colorize("This is magenta", "magenta");
|
||||
writeln(ret);
|
||||
assert(ret == "\033[35mThis is magenta\033[0m");
|
||||
}
|
||||
|
||||
string colorHelper(T)(const string str, const T t=T.init)
|
||||
if(is(T : fg) || is(T : bg) || is(T : mode))
|
||||
string colorHelper(T)(const string str, const T t=T.init) pure
|
||||
if(is(T : fg) || is(T : bg) || is(T : mode) || is(T : string))
|
||||
{
|
||||
return format("\033[%dm%s\033[0m", t, str);
|
||||
static if(is(T : string))
|
||||
{
|
||||
int code;
|
||||
|
||||
switch(t)
|
||||
{
|
||||
case "init": code = 39; break;
|
||||
|
||||
case "black" : code = 30; break;
|
||||
case "red" : code = 31; break;
|
||||
case "green" : code = 32; break;
|
||||
case "yellow" : code = 33; break;
|
||||
case "blue" : code = 34; break;
|
||||
case "magenta": code = 35; break;
|
||||
case "cyan" : code = 36; break;
|
||||
case "white" : code = 37; break;
|
||||
|
||||
case "light_black" : code = 90; break;
|
||||
case "light_red" : code = 91; break;
|
||||
case "light_green" : code = 92; break;
|
||||
case "light_yellow" : code = 93; break;
|
||||
case "light_blue" : code = 94; break;
|
||||
case "light_magenta": code = 95; break;
|
||||
case "light_cyan" : code = 96; break;
|
||||
case "light_white" : code = 97; break;
|
||||
|
||||
case "bg_init": code = 49; break;
|
||||
|
||||
case "bg_black" : code = 40; break;
|
||||
case "bg_red" : code = 41; break;
|
||||
case "bg_green" : code = 42; break;
|
||||
case "bg_yellow" : code = 43; break;
|
||||
case "bg_blue" : code = 44; break;
|
||||
case "bg_magenta": code = 45; break;
|
||||
case "bg_cyan" : code = 46; break;
|
||||
case "bg_white" : code = 47; break;
|
||||
|
||||
case "bg_light_black" : code = 100; break;
|
||||
case "bg_light_red" : code = 101; break;
|
||||
case "bg_light_green" : code = 102; break;
|
||||
case "bg_light_yellow" : code = 103; break;
|
||||
case "bg_light_blue" : code = 104; break;
|
||||
case "bg_light_magenta": code = 105; break;
|
||||
case "bg_light_cyan" : code = 106; break;
|
||||
case "bg_light_white" : code = 107; break;
|
||||
|
||||
case "mode_init": code = 0; break;
|
||||
case "mode_bold" : code = 1; break;
|
||||
case "mode_underline": code = 4; break;
|
||||
case "mode_blink" : code = 5; break;
|
||||
case "mode_swap" : code = 7; break;
|
||||
case "mode_hide" : code = 8; break;
|
||||
|
||||
default:
|
||||
throw new Exception(
|
||||
"Unknown fg color, bg color or mode \"" ~ t ~ "\""
|
||||
);
|
||||
}
|
||||
|
||||
return format("\033[%dm%s\033[0m", code, str);
|
||||
}
|
||||
else
|
||||
{
|
||||
return format("\033[%dm%s\033[0m", t, str);
|
||||
}
|
||||
}
|
||||
|
||||
alias colorHelper!bg background;
|
||||
alias colorHelper!fg foreground;
|
||||
alias colorHelper!mode style;
|
||||
|
||||
alias background colorize;
|
||||
alias foreground colorize;
|
||||
alias style colorize;
|
||||
alias colorHelper!string colorize;
|
||||
|
||||
unittest
|
||||
{
|
||||
import std.stdio;
|
||||
|
|
Loading…
Reference in a new issue