Skip to content

Commit

Permalink
Merge pull request #91 from project-fifo/less-colours
Browse files Browse the repository at this point in the history
Alow less colourful output
  • Loading branch information
tsloughter committed Oct 31, 2015
2 parents 49bc69e + 4aedc36 commit 95a8e3c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ otp_release:
- R16B01
- R16B
- R15B02
script: "./rebar3 compile && ./rebar3 ct"
script: "./rebar3 update && ./rebar3 compile && ./rebar3 ct"
branches:
only:
- master
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*-

%% Dependencies ================================================================
{deps, []}.
{deps, [cf]}.

{erl_first_files, ["ec_dictionary", "ec_vsn"]}.

Expand Down
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[].
[{<<"cf">>,{pkg,<<"cf">>,<<"0.1.2">>},0}].
58 changes: 43 additions & 15 deletions src/ec_cmd_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

-export([new/1,
new/2,
new/3,
log/4,
should/2,
debug/2,
Expand All @@ -40,17 +41,18 @@

-include("ec_cmd_log.hrl").

-define(RED, 31).
-define(GREEN, 32).
-define(YELLOW, 33).
-define(BLUE, 34).
-define(MAGENTA, 35).
-define(CYAN, 36).
-define(RED, $r).
-define(GREEN, $g).
-define(YELLOW, $y).
-define(BLUE, $b).
-define(MAGENTA, $m).
-define(CYAN, $c).

-define(PREFIX, "===> ").

-record(state_t, {log_level=0 :: int_log_level(),
caller=api :: caller(),
intensity=low :: low | high,
term_cap=full :: full | dumb }).

%%============================================================================
Expand All @@ -71,9 +73,11 @@

-type atom_log_level() :: error | warn | info | debug.

-type intensity() :: low | high.

-type log_fun() :: fun(() -> iolist()).

-type color() :: 31..36.
-type color() :: char().

-opaque t() :: #state_t{}.

Expand All @@ -86,9 +90,17 @@ new(LogLevel) ->
new(LogLevel, api).

-spec new(log_level(), caller()) -> t().
new(LogLevel, Caller) when LogLevel >= 0, LogLevel =< 3 ->
#state_t{log_level=LogLevel, caller=Caller, term_cap=query_term_env()};
new(AtomLogLevel, Caller)
new(LogLevel, Caller) ->
new(LogLevel, Caller, high).


-spec new(log_level(), caller(), intensity()) -> t().
new(LogLevel, Caller, Intensity) when (Intensity =:= low orelse
Intensity =:= high),
LogLevel >= 0, LogLevel =< 3 ->
#state_t{log_level=LogLevel, caller=Caller, term_cap=query_term_env(),
intensity=Intensity};
new(AtomLogLevel, Caller, Intensity)
when AtomLogLevel =:= error;
AtomLogLevel =:= warn;
AtomLogLevel =:= info;
Expand All @@ -99,7 +111,8 @@ new(AtomLogLevel, Caller)
info -> 2;
debug -> 3
end,
new(LogLevel, Caller).
new(LogLevel, Caller, Intensity).


%% @doc log at the debug level given the current log state with a string or
%% function that returns a string
Expand Down Expand Up @@ -218,10 +231,25 @@ format(Log) ->
<<")">>].

-spec colorize(t(), color(), boolean(), string()) -> string().
colorize(#state_t{caller=command_line, term_cap=full}, Color, false, Msg) when is_integer(Color) ->
lists:flatten(io_lib:format("\033[~B;~Bm~s~s\033[0m", [0, Color, ?PREFIX, Msg]));
colorize(#state_t{caller=command_line, term_cap=dumb}, Color, _Bold, Msg) when is_integer(Color) ->
lists:flatten(io_lib:format("~s~s", [?PREFIX, Msg]));

-define(VALID_COLOR(C),
C =:= $r orelse C =:= $g orelse C =:= $y orelse
C =:= $b orelse C =:= $m orelse C =:= $c orelse
C =:= $R orelse C =:= $G orelse C =:= $Y orelse
C =:= $B orelse C =:= $M orelse C =:= $C).

%% We're sneaky we can substract 32 to get the uppercase character
colorize(State, Color, true, Msg) when ?VALID_COLOR(Color) ->
colorize(State, Color - 32, fase, Msg);
colorize(#state_t{caller=command_line, term_cap=full, intensity = high},
Color, false, Msg) when ?VALID_COLOR(Color) ->
lists:flatten(cf:format("~!" ++ [Color] ++"~s~s", [?PREFIX, Msg]));
colorize(#state_t{caller=command_line, term_cap=full, intensity = low},
Color, false, Msg) when ?VALID_COLOR(Color) ->
lists:flatten(cf:format("~!" ++ [Color] ++"~s~!!~s", [?PREFIX, Msg]));
colorize(#state_t{caller=command_line, term_cap=dumb}, Color, _Bold, Msg)
when ?VALID_COLOR(Color) ->
lists:flatten(cf:format("~s~s", [?PREFIX, Msg]));
colorize(_LogState, _Color, _Bold, Msg) ->
Msg.

Expand Down
2 changes: 1 addition & 1 deletion src/erlware_commons.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application,erlware_commons,
[{description,"Additional standard library for Erlang"},
{vsn,"0.16.0"},
{vsn,"0.16.1"},
{modules,[]},
{registered,[]},
{applications,[kernel,stdlib]},
Expand Down

0 comments on commit 95a8e3c

Please sign in to comment.