Make colorHelper overloading cleaner.

Using `static if`'s for type checking inside of a constrained template
function didn't make much sense after all.
This commit is contained in:
yamadapc 2014-07-13 15:12:50 -03:00
parent 4d8473495a
commit 42a779cfc3

View file

@ -100,74 +100,72 @@ unittest
assert(ret == "\033[35mThis is magenta\033[0m");
}
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))
string colorHelper(const string str, const string name) pure
{
static if(is(T : string))
int code;
switch(name)
{
int code;
case "init": code = 39; break;
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 "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 "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_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_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 "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;
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);
default:
throw new Exception(
"Unknown fg color, bg color or mode \"" ~ name ~ "\""
);
}
return format("\033[%dm%s\033[0m", code, str);
}
string colorHelper(T)(const string str, const T t=T.init) pure
if(is(T : fg) || is(T : bg) || is(T : mode))
{
return format("\033[%dm%s\033[0m", t, str);
}
alias colorHelper!bg background;
@ -177,7 +175,7 @@ alias colorHelper!mode style;
alias background colorize;
alias foreground colorize;
alias style colorize;
alias colorHelper!string colorize;
alias colorHelper colorize;
unittest
{