From 3c917be234a3a994880c871b1d63c37b0a04696a Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Mon, 21 Sep 2015 22:42:44 +0200 Subject: Split out colour printing code in own module --- src/rebar_colour.erl | 101 ++++++++++++++++++++++++++++++++++++++++++ src/rebar_dialyzer_format.erl | 99 ++--------------------------------------- 2 files changed, 104 insertions(+), 96 deletions(-) create mode 100644 src/rebar_colour.erl diff --git a/src/rebar_colour.erl b/src/rebar_colour.erl new file mode 100644 index 0000000..bff080d --- /dev/null +++ b/src/rebar_colour.erl @@ -0,0 +1,101 @@ +-module(rebar_colour). + +-export([format/1, format/2]). + +-define(NR, "\033[0;31m"). +-define(NG, "\033[0;32m"). +-define(NY, "\033[0;33m"). +-define(NB, "\033[0;34m"). +-define(NM, "\033[0;35m"). +-define(NC, "\033[0;36m"). +-define(NW, "\033[0;37m"). +-define(BR, "\033[1;31m"). +-define(BG, "\033[1;32m"). +-define(BY, "\033[1;33m"). +-define(BB, "\033[1;34m"). +-define(BM, "\033[1;35m"). +-define(BC, "\033[1;36m"). +-define(BW, "\033[1;37m"). +-define(R, "\033[0m"). + +format(Fmt) -> + format(Fmt, []). +format(Fmt, Args) -> + io_lib:format(cfmt(Fmt), Args). + +%% FROM https://github.com/erlware/erlware_commons/blob/49bc69e35a282bde4a0a6a8f211b5f77d8585256/src/ec_cmd_log.erl +%% @doc Query the term enviroment +%% For reasons of simplicity, we don't parse terminal capabilities yet, although +%% a later version could do so. Rather, we provide a simple match-list of terminal +%% capabilities. +%% @end +-spec query_term_env() -> full | dumb. +query_term_env() -> + term_capabilities(os:getenv("TERM")). + +-spec term_capabilities(string()) -> full | dumb. +term_capabilities("xterm") -> full; +term_capabilities("dumb") -> dumb; +term_capabilities(_) -> full. %% Default to the backwards compatible version. + + +cfmt(S) -> + cfmt(S, query_term_env() =:= full). + +cfmt(S, Enabled) -> + lists:flatten(cfmt_(S, ?R, Enabled)). + +cfmt_([$~,$!,_C | S], Last, false) -> + cfmt_(S, Last, false); + +cfmt_([$~,$!,$! | S], _Last, Enabled) -> + [?R | cfmt_(S, ?R, Enabled)]; + +cfmt_([$~,$!,$r | S], _Last, Enabled) -> + [?NR | cfmt_(S, ?NR, Enabled)]; +cfmt_([$~,$!,$R | S], _Last, Enabled) -> + [?BR | cfmt_(S, ?BR, Enabled)]; + +cfmt_([$~,$!,$g | S], _Last, Enabled) -> + [?NG | cfmt_(S, ?NG, Enabled)]; +cfmt_([$~,$!,$G | S], _Last, Enabled) -> + [?BG | cfmt_(S, ?BG, Enabled)]; + +cfmt_([$~,$!,$y | S], _Last, Enabled) -> + [?NY | cfmt_(S, ?NY, Enabled)]; +cfmt_([$~,$!,$Y | S], _Last, Enabled) -> + [?BY | cfmt_(S, ?BY, Enabled)]; + +cfmt_([$~,$!,$b | S], _Last, Enabled) -> + [?NB | cfmt_(S, ?NB, Enabled)]; +cfmt_([$~,$!,$B | S], _Last, Enabled) -> + [?BB | cfmt_(S, ?BB, Enabled)]; + +cfmt_([$~,$!,$m | S], _Last, Enabled) -> + [?NM | cfmt_(S, ?NM, Enabled)]; +cfmt_([$~,$!,$M | S], _Last, Enabled) -> + [?BM | cfmt_(S, ?BM, Enabled)]; + +cfmt_([$~,$!,$c | S], _Last, Enabled) -> + [?NC | cfmt_(S, ?NC, Enabled)]; +cfmt_([$~,$!,$C | S], _Last, Enabled) -> + [?BC | cfmt_(S, ?BC, Enabled)]; + +cfmt_([$~,$!,$w | S], _Last, Enabled) -> + [?NW | cfmt_(S, ?NW, Enabled)]; +cfmt_([$~,$!,$W | S], _Last, Enabled) -> + [?BW | cfmt_(S, ?BW, Enabled)]; + +cfmt_([$~,$~ | S], Last, Enabled) -> + [$~,$~ | cfmt_(S, Last, Enabled)]; + +cfmt_([$~,$s| S], Last, Enabled) -> + [$~,$s, Last | cfmt_(S, Last, Enabled)]; + +cfmt_([C | S], Last, Enabled) -> + [C | cfmt_(S, Last, Enabled)]; + +cfmt_([], _Last, false) -> + ""; +cfmt_([], _Last, _Enabled) -> + ?R. diff --git a/src/rebar_dialyzer_format.erl b/src/rebar_dialyzer_format.erl index ef8eab1..282f359 100644 --- a/src/rebar_dialyzer_format.erl +++ b/src/rebar_dialyzer_format.erl @@ -4,21 +4,7 @@ -export([format/1]). --define(NR, "\033[0;31m"). --define(NG, "\033[0;32m"). --define(NY, "\033[0;33m"). --define(NB, "\033[0;34m"). --define(NM, "\033[0;35m"). --define(NC, "\033[0;36m"). --define(NW, "\033[0;37m"). --define(BR, "\033[1;31m"). --define(BG, "\033[1;32m"). --define(BY, "\033[1;33m"). --define(BB, "\033[1;34m"). --define(BM, "\033[1;35m"). --define(BC, "\033[1;36m"). --define(BW, "\033[1;37m"). --define(R, "\033[0m"). + format(Warning) -> Str = try @@ -39,15 +25,10 @@ format(Warning) -> strip(Warning) -> string:strip(Warning, right, $\n). -%%fmt(Fmt, Args) -> -%% Args2 = [fmt("~s\033[1;37m", [A]) || A <- Args], -%% fmt(Fmt, Args2). - fmt(Fmt) -> - fmt(Fmt, []). + rebar_colour:format(Fmt, []). fmt(Fmt, Args) -> - io_lib:format(cfmt(Fmt), Args). - + rebar_colour:format(Fmt, Args). format_warning({Tag, {File, Line, _MFA}, Msg}, FOpt) -> format_warning({Tag, {File, Line}, Msg}, FOpt); @@ -60,80 +41,6 @@ format_warning({_Tag, {File, Line}, Msg}, FOpt) when is_list(File), String = lists:flatten(message_to_string(Msg)), lists:flatten(fmt("~s:~w~n~s", [F, Line, String])). - -%% FROM https://github.com/erlware/erlware_commons/blob/49bc69e35a282bde4a0a6a8f211b5f77d8585256/src/ec_cmd_log.erl -%% @doc Query the term enviroment -%% For reasons of simplicity, we don't parse terminal capabilities yet, although -%% a later version could do so. Rather, we provide a simple match-list of terminal -%% capabilities. -%% @end --spec query_term_env() -> full | dumb. -query_term_env() -> - term_capabilities(os:getenv("TERM")). - --spec term_capabilities(string()) -> full | dumb. -term_capabilities("xterm") -> full; -term_capabilities("dumb") -> dumb; -term_capabilities(_) -> full. %% Default to the backwards compatible version. - - -cfmt(S) -> - cfmt(S, query_term_env() =:= full). -cfmt(S, true) -> - lists:flatten(cfmt_(S, true)) ++ ?R; -cfmt(S, false) -> - lists:flatten(cfmt_(S, false)). - -cfmt_([$~,$!,_C | S], false) -> - cfmt_(S, false); - -cfmt_([$~,$!,$! | S], Enabled) -> - [?R | cfmt_(S, Enabled)]; - -cfmt_([$~,$!,$r | S], Enabled) -> - [?NR | cfmt_(S, Enabled)]; -cfmt_([$~,$!,$R | S], Enabled) -> - [?BR | cfmt_(S, Enabled)]; - -cfmt_([$~,$!,$g | S], Enabled) -> - [?NG | cfmt_(S, Enabled)]; -cfmt_([$~,$!,$G | S], Enabled) -> - [?BG | cfmt_(S, Enabled)]; - -cfmt_([$~,$!,$y | S], Enabled) -> - [?NY | cfmt_(S, Enabled)]; -cfmt_([$~,$!,$Y | S], Enabled) -> - [?BY | cfmt_(S, Enabled)]; - -cfmt_([$~,$!,$b | S], Enabled) -> - [?NB | cfmt_(S, Enabled)]; -cfmt_([$~,$!,$B | S], Enabled) -> - [?BB | cfmt_(S, Enabled)]; - -cfmt_([$~,$!,$m | S], Enabled) -> - [?NM | cfmt_(S, Enabled)]; -cfmt_([$~,$!,$M | S], Enabled) -> - [?BM | cfmt_(S, Enabled)]; - -cfmt_([$~,$!,$c | S], Enabled) -> - [?NC | cfmt_(S, Enabled)]; -cfmt_([$~,$!,$C | S], Enabled) -> - [?BC | cfmt_(S, Enabled)]; - -cfmt_([$~,$!,$w | S], Enabled) -> - [?NW | cfmt_(S, Enabled)]; -cfmt_([$~,$!,$W | S], Enabled) -> - [?BW | cfmt_(S, Enabled)]; - -cfmt_([$~,$~ | S], Enabled) -> - [$~,$~ | cfmt_(S, Enabled)]; - -cfmt_([C | S], Enabled) -> - [C | cfmt_(S, Enabled)]; - -cfmt_([], _Enabled) -> - []. - %%----------------------------------------------------------------------------- %% Message classification and pretty-printing below. Messages appear in %% categories and in more or less alphabetical ordering within each category. -- cgit v1.1