Introduce simple, preliminary TERM capability query.
When erlware_commons logs to the command_line, it assumes the environment has common modern capabilities and color display. In general, this is not the case and then color codes are sent verbatim to the terminal. This patch introduces a new field in #state_t{}, term_cap, encoding if the terminal runs with 'full' or 'dumb' capabilities. In the latter case, color display is suppressed. Initialization of the #state_t{} record queries the environment once for the TERM variable in order to figure out what it supports. The default is 'full' capability to be fully backwards compatible.
This commit is contained in:
parent
6079300634
commit
aa373dddbe
1 changed files with 20 additions and 3 deletions
|
@ -50,7 +50,8 @@
|
||||||
-define(PREFIX, "===> ").
|
-define(PREFIX, "===> ").
|
||||||
|
|
||||||
-record(state_t, {log_level=0 :: int_log_level(),
|
-record(state_t, {log_level=0 :: int_log_level(),
|
||||||
caller=api :: caller()}).
|
caller=api :: caller(),
|
||||||
|
term_cap=full :: full | dumb }).
|
||||||
|
|
||||||
%%============================================================================
|
%%============================================================================
|
||||||
%% types
|
%% types
|
||||||
|
@ -86,7 +87,7 @@ new(LogLevel) ->
|
||||||
|
|
||||||
-spec new(log_level(), caller()) -> t().
|
-spec new(log_level(), caller()) -> t().
|
||||||
new(LogLevel, Caller) when LogLevel >= 0, LogLevel =< 3 ->
|
new(LogLevel, Caller) when LogLevel >= 0, LogLevel =< 3 ->
|
||||||
#state_t{log_level=LogLevel, caller=Caller};
|
#state_t{log_level=LogLevel, caller=Caller, term_cap=query_term_env()};
|
||||||
new(AtomLogLevel, Caller)
|
new(AtomLogLevel, Caller)
|
||||||
when AtomLogLevel =:= error;
|
when AtomLogLevel =:= error;
|
||||||
AtomLogLevel =:= warn;
|
AtomLogLevel =:= warn;
|
||||||
|
@ -217,11 +218,27 @@ format(Log) ->
|
||||||
<<")">>].
|
<<")">>].
|
||||||
|
|
||||||
-spec colorize(t(), color(), boolean(), string()) -> string().
|
-spec colorize(t(), color(), boolean(), string()) -> string().
|
||||||
colorize(#state_t{caller=command_line}, Color, false, Msg) when is_integer(Color) ->
|
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]));
|
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]));
|
||||||
colorize(_LogState, _Color, _Bold, Msg) ->
|
colorize(_LogState, _Color, _Bold, Msg) ->
|
||||||
Msg.
|
Msg.
|
||||||
|
|
||||||
|
%% @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.
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% Test Functions
|
%%% Test Functions
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue