Skip to content

Commit

Permalink
Merge pull request #2 from edkolev/master
Browse files Browse the repository at this point in the history
Add support for background colors
  • Loading branch information
julianduque committed Feb 5, 2014
2 parents c4cd566 2ddc86a commit 72fc48f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/color.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 15,14 @@
-define(WHITE, "37").
-define(DEFAULT, "39").

%% Background colors
-define(BLACK_BG, "40").
-define(RED_BG, "41").
-define(GREEN_BG, "42").
-define(YELLOW_BG, "43").
-define(BLUE_BG, "44").
-define(MAGENTA_BG, "45").
-define(CYAN_BG, "46").
-define(WHITE_BG, "47").
-define(DEFAULT_BG, "49").

28 changes: 28 additions & 0 deletions src/color.erl
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
-module(color).
-export([black/1, blackb/1, red/1, redb/1, green/1, greenb/1, blue/1, blueb/1]).
-export([yellow/1, yellowb/1, magenta/1, magentab/1, cyan/1, cyanb/1, white/1, whiteb/1]).
-export([on_black/1, on_red/1, on_green/1, on_blue/1, on_yellow/1, on_magenta/1, on_cyan/1, on_white/1]).

-include("color.hrl").

Expand Down Expand Up @@ -52,6 53,30 @@ white(Text) ->
whiteb(Text) ->
colorb(?WHITE) Text reset().

on_black(Text) ->
color(?BLACK_BG) Text reset_bg().

on_red(Text) ->
color(?RED_BG) Text reset_bg().

on_green(Text) ->
color(?GREEN_BG) Text reset_bg().

on_blue(Text) ->
color(?BLUE_BG) Text reset_bg().

on_yellow(Text) ->
color(?YELLOW_BG) Text reset_bg().

on_magenta(Text) ->
color(?MAGENTA_BG) Text reset_bg().

on_cyan(Text) ->
color(?CYAN_BG) Text reset_bg().

on_white(Text) ->
color(?WHITE_BG) Text reset_bg().

%% Internal
color(Color) ->
?ESC Color ?END.
Expand All @@ -61,3 86,6 @@ colorb(Color) ->

reset() ->
?ESC ?RST ?END.

reset_bg() ->
?ESC ?DEFAULT_BG ?END.
17 changes: 17 additions & 0 deletions test/color_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 52,20 @@ cyanb_test() ->
whiteb_test() ->
?assertEqual("\e[37;1mwhite\e[0m", color:whiteb("white")).

on_black_test() ->
?assertEqual("\e[40mon_black\e[49m", color:on_black("on_black")).
on_red_test() ->
?assertEqual("\e[41mon_red\e[49m", color:on_red("on_red")).
on_green_test() ->
?assertEqual("\e[42mon_green\e[49m", color:on_green("on_green")).
on_yellow_test() ->
?assertEqual("\e[43mon_yellow\e[49m", color:on_yellow("on_yellow")).
on_blue_test() ->
?assertEqual("\e[44mon_blue\e[49m", color:on_blue("on_blue")).
on_magenta_test() ->
?assertEqual("\e[45mon_magenta\e[49m", color:on_magenta("on_magenta")).
on_cyan_test() ->
?assertEqual("\e[46mon_cyan\e[49m", color:on_cyan("on_cyan")).
on_white_test() ->
?assertEqual("\e[47mon_white\e[49m", color:on_white("on_white")).

0 comments on commit 72fc48f

Please sign in to comment.