Compare commits

..

7 commits

Author SHA1 Message Date
Heinz N. Gies
2bcf00402d Fix hiding truecolor 2017-06-14 21:57:32 +02:00
Heinz N. Gies
5d1a3c6783 Merge branch 'master' of github.com:project-fifo/cf 2017-06-14 21:44:51 +02:00
Heinz N. Gies
03c34c8e9a Add truecolor 2017-06-14 21:44:42 +02:00
Heinz N. Gies
52f39d9be0 Merge pull request #2 from binarin/erlang-mk-workaround
Add `compile` target to Makefile
2017-03-22 18:48:27 +01:00
Alexey Lebedeff
2ea1755cf2 Add compile target to Makefile
To make it usable with stock version of `erlang.mk`.

I'm trying to use `cf` from withing erlang.mk-based project, but it
fails to properly autopatch it. erlang.mk magic works properly when
Makefile contains some references to `rebar`.

Proper fix will be available within
https://github.com/ninenines/erlang.mk/issues/586

But currently it's far more easier to add single target to this project,
than to delve into shell script/make spaghetti =)
2016-08-02 14:06:37 +03:00
Heinz N. Gies
641d0a009b Add link to GH, closes #1 2016-07-06 03:58:24 +02:00
Heinz N. Gies
a6b395759e Simplify color capability detection. 2015-10-19 04:20:00 +02:00
7 changed files with 539 additions and 10 deletions

24
Makefile Normal file
View file

@ -0,0 +1,24 @@
TERMCAP_FILE=http://code.metager.de/source/raw/OpenBSD/src/share/termtypes/termtypes.master
all: src/termcap.erl
rebar3 compile
src/termcap.erl: termtypes.master.clean mk-termcap.escript termcap.erl
./mk-termcap.escript termtypes.master.clean > src/cf_term.erl
mk-termcap.escript:
termcap.erl:
clean:
[ -f termtypes.master ] && rm termtypes.master || true
[ -f termtypes.master.clean ] && rm termtypes.master.clean || true
compile:
rebar3 compile
termtypes.master.clean: termtypes.master
cat termtypes.master | grep -v '^#' | grep -v '^\s*$$' > termtypes.master.clean
termtypes.master:
curl -O $(TERMCAP_FILE)

99
mk-termcap.escript Executable file
View file

@ -0,0 +1,99 @@
#!/usr/bin/env escript
main([In]) ->
{ok, File} = file:open(In, [read]),
case io:get_line(File, "") of
eof ->
file:close(File);
Cap ->
io:format("-module(cf_term).~n"),
io:format("-export([has_color/1]).~n"),
Terms = read_lines(File, {parse_name(Cap), ""}, []),
Terms1 = [{T, has_color(Vs, Terms)} || {T, Vs} <- Terms],
[io:format("has_color(~p) -> true;~n", [T]) || {T, true} <- Terms1],
io:format("has_color(_) -> false.~n")
end.
read_lines(File, {H, Acc}, FAcc) ->
case io:get_line(File, "") of
eof ->
file:close(File),
lists:sort([{H, parse_caps(Acc)} | FAcc]);
" " ++ Rest ->
case Acc of
"" ->
read_lines(File, {H, remove_newline(strip_ws(Rest))}, FAcc);
_ ->
read_lines(File, {H, [Acc, " ", remove_newline(strip_ws(Rest))]}, FAcc)
end;
"\t" ++ Rest ->
case Acc of
"" ->
read_lines(File, {H, remove_newline(strip_ws(Rest))}, FAcc);
_ ->
read_lines(File, {H, [Acc, " ", remove_newline(strip_ws(Rest))]}, FAcc)
end;
Cap ->
read_lines(File, {parse_name(Cap), ""}, [{H, parse_caps(Acc)} | FAcc])
end.
has_color([], _Ts) ->
false;
has_color([{colors, _} | _], _Ts) ->
true;
has_color([{use, T} | R], Ts) ->
case orddict:find(T, Ts) of
error ->
has_color(R, Ts);
{ok, V} ->
has_color(V, Ts) orelse
has_color(R, Ts)
end.
remove_newline(S) ->
remove_newline(S, []).
remove_newline("\n", Acc) ->
lists:reverse(Acc);
remove_newline("", Acc) ->
lists:reverse(Acc);
remove_newline([C | R], Acc) ->
remove_newline(R, [C | Acc]).
strip_ws([$\s | R]) ->
strip_ws(R);
strip_ws([$\t | R]) ->
strip_ws(R);
strip_ws(R) ->
R.
filter_caps([]) ->
[];
filter_caps([{colors, _} = C | R]) ->
[C | filter_caps(R)];
filter_caps([{use, _} = C | R]) ->
[C | filter_caps(R)];
filter_caps([_ | R]) ->
filter_caps(R).
remove_colon(S) ->
re:replace(S, ",[\\s\\n\\t]*$", "", [{return,list}]).
parse_name(S) ->
[Term | _Rest] = re:split(remove_colon(S), "\\|", [{return,list}]),
Term.
parse_caps(S) ->
Caps = re:split(remove_colon(S), ",[\\s\\t]+", [{return,list}]),
filter_caps([parse_cap(C, []) || C <- Caps, C =/= ""]).
parse_cap([], Name) ->
list_to_atom(lists:reverse(Name));
parse_cap([$= | V], Name) ->
{list_to_atom(lists:reverse(Name)), V};
parse_cap([$# | V], Name) ->
{list_to_atom(lists:reverse(Name)), list_to_integer(V)};
parse_cap([C | R], Name) ->
parse_cap(R, [ C | Name]).

View file

@ -1,2 +1,8 @@
{erl_opts, [debug_info]}. {erl_opts, [debug_info]}.
{deps, [{termcap, "0.1.0"}]}. {deps, []}.
{profiles, [
{shell, [
{deps, [sync]}
]}
]}.

View file

@ -1 +1 @@
[{<<"termcap">>,{pkg,<<"termcap">>,<<"0.1.0">>},0}]. [].

View file

@ -1,10 +1,10 @@
{application,cf, {application,cf,
[{description,"Terminal colour helper"}, [{description,"Terminal colour helper"},
{vsn,"0.2.0"}, {vsn, git},
{registered,[]}, {registered,[]},
{applications,[kernel,stdlib,termcap]}, {applications,[kernel,stdlib]},
{env,[]}, {env,[]},
{modules,[]}, {modules,[]},
{maintainers,["Heinz N. Gies <heinz@project-fifo.net>"]}, {maintainers,["Heinz N. Gies <heinz@project-fifo.net>"]},
{licenses,["MIT"]}, {licenses,["MIT"]},
{links,[]}]}. {links,[{"github","https://github.com/project-fifo/cf"}]}]}.

View file

@ -38,6 +38,11 @@
%% c,C - cyan %% c,C - cyan
%% w,W - white %% w,W - white
%% %%
%% true color is supported by using
%% ~!#<rr><gg><bb> as each as hex values so ~!#ff6402
%%
%% the same can be done for the background by yusign ~##
%%
%% The function will disable colours on non x term termials %% The function will disable colours on non x term termials
%% @end %% @end
print(Fmt, Args) -> print(Fmt, Args) ->
@ -102,8 +107,7 @@ colour_term() ->
V; V;
undefined -> undefined ->
Term = os:getenv("TERM"), Term = os:getenv("TERM"),
Caps = termcap:cap(Term), V = cf_term:has_color(Term),
V = proplists:is_defined("colors", Caps),
application:set_env(cf, colour_term, V), application:set_env(cf, colour_term, V),
V V
end. end.
@ -114,6 +118,11 @@ cfmt(S) ->
cfmt(S, Enabled) -> cfmt(S, Enabled) ->
lists:flatten(cfmt_(S, Enabled)). lists:flatten(cfmt_(S, Enabled)).
cfmt_([$~, $!, $#, _R1, _R2, _G1, _G2, _B1, _B2 | S], false) ->
cfmt_(S, false);
cfmt_([$~, $#, $#, _R1, _R2, _G1, _G2, _B1, _B2 | S], false) ->
cfmt_(S, false);
cfmt_([$~, $!, $_, _C | S], false) -> cfmt_([$~, $!, $_, _C | S], false) ->
cfmt_(S, false); cfmt_(S, false);
cfmt_([$~, $#, _C | S], false) -> cfmt_([$~, $#, _C | S], false) ->
@ -121,12 +130,33 @@ cfmt_([$~,$#, _C | S], false) ->
cfmt_([$~, $!, _C | S], false) -> cfmt_([$~, $!, _C | S], false) ->
cfmt_(S, false); cfmt_(S, false);
cfmt_([$~, $!, $#, R1, R2, G1, G2, B1, B2 | S], Enabled) ->
R = list_to_integer([R1, R2], 16),
G = list_to_integer([G1, G2], 16),
B = list_to_integer([B1, B2], 16),
["\033[38;2;",
integer_to_list(R), $;,
integer_to_list(G), $;,
integer_to_list(B), $m |
cfmt_(S, Enabled)];
cfmt_([$~, $#, $#, R1, R2, G1, G2, B1, B2 | S], Enabled) ->
R = list_to_integer([R1, R2], 16),
G = list_to_integer([G1, G2], 16),
B = list_to_integer([B1, B2], 16),
["\033[48;2;",
integer_to_list(R), $;,
integer_to_list(G), $;,
integer_to_list(B), $m |
cfmt_(S, Enabled)];
cfmt_([$~, $!, $_, $_ | S], Enabled) -> cfmt_([$~, $!, $_, $_ | S], Enabled) ->
[?U |cfmt_(S, Enabled)]; [?U |cfmt_(S, Enabled)];
cfmt_([$~,$!, $^ | S], Enabled) -> cfmt_([$~,$!, $^ | S], Enabled) ->
[?B | cfmt_(S, Enabled)]; [?B | cfmt_(S, Enabled)];
cfmt_([$~,$!, $_, $^ | S], Enabled) -> cfmt_([$~,$!, $_, $^ | S], Enabled) ->
[?U, ?B | cfmt_(S, Enabled)]; [?U, ?B | cfmt_(S, Enabled)];
?CFMT($!, ?R); ?CFMT($!, ?R);
?CFMT($x, ?NX); ?CFMT($x, ?NX);
?CFMT($X, ?BX); ?CFMT($X, ?BX);

370
src/cf_term.erl Normal file
View file

@ -0,0 +1,370 @@
-module(cf_term).
-export([has_color/1]).
has_color("Eterm") -> true;
has_color("Eterm-256color") -> true;
has_color("Eterm-88color") -> true;
has_color("aixterm") -> true;
has_color("aixterm-16color") -> true;
has_color("amiga-vnc") -> true;
has_color("ansi") -> true;
has_color("ansi-color-2-emx") -> true;
has_color("ansi-color-3-emx") -> true;
has_color("ansi-emx") -> true;
has_color("ansi.sys") -> true;
has_color("ansi.sys-old") -> true;
has_color("ansi.sysk") -> true;
has_color("arm100") -> true;
has_color("arm100-w") -> true;
has_color("aterm") -> true;
has_color("att6386") -> true;
has_color("beterm") -> true;
has_color("bsdos-pc") -> true;
has_color("bsdos-pc-nobold") -> true;
has_color("bsdos-ppc") -> true;
has_color("bterm") -> true;
has_color("color_xterm") -> true;
has_color("cons25") -> true;
has_color("cons25-debian") -> true;
has_color("cons25-m") -> true;
has_color("cons25l1") -> true;
has_color("cons25l1-m") -> true;
has_color("cons25r") -> true;
has_color("cons25r-m") -> true;
has_color("cons25w") -> true;
has_color("cons30") -> true;
has_color("cons30-m") -> true;
has_color("cons43") -> true;
has_color("cons43-m") -> true;
has_color("cons50") -> true;
has_color("cons50-m") -> true;
has_color("cons50l1") -> true;
has_color("cons50l1-m") -> true;
has_color("cons50r") -> true;
has_color("cons50r-m") -> true;
has_color("cons60") -> true;
has_color("cons60-m") -> true;
has_color("cons60l1") -> true;
has_color("cons60l1-m") -> true;
has_color("cons60r") -> true;
has_color("cons60r-m") -> true;
has_color("crt") -> true;
has_color("ctrm") -> true;
has_color("cygwin") -> true;
has_color("cygwinB19") -> true;
has_color("cygwinDBG") -> true;
has_color("d220") -> true;
has_color("d220-7b") -> true;
has_color("d220-dg") -> true;
has_color("d230c") -> true;
has_color("d230c-dg") -> true;
has_color("d430c-dg") -> true;
has_color("d430c-dg-ccc") -> true;
has_color("d430c-unix") -> true;
has_color("d430c-unix-25") -> true;
has_color("d430c-unix-25-ccc") -> true;
has_color("d430c-unix-ccc") -> true;
has_color("d430c-unix-s") -> true;
has_color("d430c-unix-s-ccc") -> true;
has_color("d430c-unix-sr") -> true;
has_color("d430c-unix-sr-ccc") -> true;
has_color("d430c-unix-w") -> true;
has_color("d430c-unix-w-ccc") -> true;
has_color("d470c") -> true;
has_color("d470c-7b") -> true;
has_color("d470c-dg") -> true;
has_color("decansi") -> true;
has_color("dg+ccc") -> true;
has_color("dg+color") -> true;
has_color("dg+color8") -> true;
has_color("dg+fixed") -> true;
has_color("dgmode+color") -> true;
has_color("dgmode+color8") -> true;
has_color("dgunix+ccc") -> true;
has_color("dgunix+fixed") -> true;
has_color("djgpp") -> true;
has_color("djgpp204") -> true;
has_color("dtterm") -> true;
has_color("ecma+color") -> true;
has_color("emu") -> true;
has_color("emx-base") -> true;
has_color("eterm-color") -> true;
has_color("gnome") -> true;
has_color("gnome-2007") -> true;
has_color("gnome-2008") -> true;
has_color("gnome-2012") -> true;
has_color("gnome-256color") -> true;
has_color("gnome-fc5") -> true;
has_color("gnome-rh62") -> true;
has_color("gnome-rh72") -> true;
has_color("gnome-rh80") -> true;
has_color("gnome-rh90") -> true;
has_color("gs6300") -> true;
has_color("hft-c") -> true;
has_color("hft-c-old") -> true;
has_color("hft-old") -> true;
has_color("hp+color") -> true;
has_color("hp2397a") -> true;
has_color("hpterm-color") -> true;
has_color("hurd") -> true;
has_color("iTerm.app") -> true;
has_color("ibm+16color") -> true;
has_color("ibm+color") -> true;
has_color("ibm3164") -> true;
has_color("ibm5081") -> true;
has_color("ibm5154") -> true;
has_color("ibm6154") -> true;
has_color("ibm8503") -> true;
has_color("ibm8512") -> true;
has_color("ibmpc3") -> true;
has_color("interix") -> true;
has_color("iris-color") -> true;
has_color("jaixterm") -> true;
has_color("klone+color") -> true;
has_color("kon") -> true;
has_color("konsole") -> true;
has_color("konsole-16color") -> true;
has_color("konsole-256color") -> true;
has_color("konsole-base") -> true;
has_color("konsole-linux") -> true;
has_color("konsole-solaris") -> true;
has_color("konsole-vt100") -> true;
has_color("konsole-vt420pc") -> true;
has_color("konsole-xf3x") -> true;
has_color("konsole-xf4x") -> true;
has_color("kterm") -> true;
has_color("kterm-color") -> true;
has_color("kvt") -> true;
has_color("linux") -> true;
has_color("linux-16color") -> true;
has_color("linux-basic") -> true;
has_color("linux-c") -> true;
has_color("linux-c-nc") -> true;
has_color("linux-koi8") -> true;
has_color("linux-koi8r") -> true;
has_color("linux-lat") -> true;
has_color("linux-m") -> true;
has_color("linux-nic") -> true;
has_color("linux-vt") -> true;
has_color("linux2.2") -> true;
has_color("linux2.6") -> true;
has_color("linux2.6.26") -> true;
has_color("linux3.0") -> true;
has_color("mach-color") -> true;
has_color("mach-gnu-color") -> true;
has_color("mgt") -> true;
has_color("mgterm") -> true;
has_color("minitel1") -> true;
has_color("minitel1b") -> true;
has_color("minitel1b-80") -> true;
has_color("minix") -> true;
has_color("minix-3.0") -> true;
has_color("mlterm") -> true;
has_color("mlterm-256color") -> true;
has_color("mlterm2") -> true;
has_color("mlterm3") -> true;
has_color("mrxvt") -> true;
has_color("mrxvt-256color") -> true;
has_color("ms-vt-utf8") -> true;
has_color("ms-vt100+") -> true;
has_color("ms-vt100-color") -> true;
has_color("mvterm") -> true;
has_color("nansi.sys") -> true;
has_color("nansi.sysk") -> true;
has_color("ncr260intan") -> true;
has_color("ncr260intpp") -> true;
has_color("ncr260intwan") -> true;
has_color("ncr260intwpp") -> true;
has_color("ncr260wy325pp") -> true;
has_color("ncr260wy325wpp") -> true;
has_color("ncr260wy350pp") -> true;
has_color("ncr260wy350wpp") -> true;
has_color("ncsa") -> true;
has_color("ncsa-ns") -> true;
has_color("ncsa-vt220") -> true;
has_color("netbsd6") -> true;
has_color("nsterm") -> true;
has_color("nsterm+c") -> true;
has_color("nsterm+c41") -> true;
has_color("nsterm-16color") -> true;
has_color("nsterm-256color") -> true;
has_color("nsterm-7") -> true;
has_color("nsterm-7-c") -> true;
has_color("nsterm-acs") -> true;
has_color("nsterm-bce") -> true;
has_color("nsterm-build326") -> true;
has_color("nsterm-build343") -> true;
has_color("nsterm-c") -> true;
has_color("nsterm-c-acs") -> true;
has_color("nsterm-c-s") -> true;
has_color("nsterm-c-s-7") -> true;
has_color("nsterm-c-s-acs") -> true;
has_color("nsterm-old") -> true;
has_color("nsterm-s") -> true;
has_color("nsterm-s-7") -> true;
has_color("nsterm-s-acs") -> true;
has_color("pc-minix") -> true;
has_color("pc3") -> true;
has_color("pcansi") -> true;
has_color("pcansi-25") -> true;
has_color("pcansi-33") -> true;
has_color("pcansi-43") -> true;
has_color("pccon") -> true;
has_color("pccon+colors") -> true;
has_color("pccon0") -> true;
has_color("pcvt25-color") -> true;
has_color("putty") -> true;
has_color("putty-256color") -> true;
has_color("putty-sco") -> true;
has_color("putty-vt100") -> true;
has_color("qansi") -> true;
has_color("qansi-g") -> true;
has_color("qansi-m") -> true;
has_color("qansi-t") -> true;
has_color("qansi-w") -> true;
has_color("qnx") -> true;
has_color("rcons-color") -> true;
has_color("rxvt") -> true;
has_color("rxvt-16color") -> true;
has_color("rxvt-256color") -> true;
has_color("rxvt-88color") -> true;
has_color("rxvt-color") -> true;
has_color("rxvt-cygwin") -> true;
has_color("rxvt-cygwin-native") -> true;
has_color("rxvt-unicode") -> true;
has_color("rxvt-unicode-256color") -> true;
has_color("rxvt-xpm") -> true;
has_color("scoansi") -> true;
has_color("scoansi-new") -> true;
has_color("scoansi-old") -> true;
has_color("screen") -> true;
has_color("screen-16color") -> true;
has_color("screen-16color-bce") -> true;
has_color("screen-16color-bce-s") -> true;
has_color("screen-16color-s") -> true;
has_color("screen-256color") -> true;
has_color("screen-256color-bce") -> true;
has_color("screen-256color-bce-s") -> true;
has_color("screen-256color-s") -> true;
has_color("screen-bce") -> true;
has_color("screen-bce.Eterm") -> true;
has_color("screen-bce.gnome") -> true;
has_color("screen-bce.konsole") -> true;
has_color("screen-bce.linux") -> true;
has_color("screen-bce.mrxvt") -> true;
has_color("screen-bce.rxvt") -> true;
has_color("screen-s") -> true;
has_color("screen-w") -> true;
has_color("screen.Eterm") -> true;
has_color("screen.gnome") -> true;
has_color("screen.konsole") -> true;
has_color("screen.konsole-256color") -> true;
has_color("screen.linux") -> true;
has_color("screen.mlterm") -> true;
has_color("screen.mlterm-256color") -> true;
has_color("screen.mrxvt") -> true;
has_color("screen.putty") -> true;
has_color("screen.putty-256color") -> true;
has_color("screen.rxvt") -> true;
has_color("screen.teraterm") -> true;
has_color("screen.vte") -> true;
has_color("screen.vte-256color") -> true;
has_color("screen.xterm-256color") -> true;
has_color("screen.xterm-xfree86") -> true;
has_color("simpleterm") -> true;
has_color("st") -> true;
has_color("st-16color") -> true;
has_color("st-256color") -> true;
has_color("st52-color") -> true;
has_color("sun-color") -> true;
has_color("tek4205") -> true;
has_color("teken") -> true;
has_color("teraterm") -> true;
has_color("teraterm2.3") -> true;
has_color("teraterm4.59") -> true;
has_color("terminator") -> true;
has_color("terminology") -> true;
has_color("ti928") -> true;
has_color("ti928-8") -> true;
has_color("ti_ansi") -> true;
has_color("tmux") -> true;
has_color("tmux-256color") -> true;
has_color("tw100") -> true;
has_color("tw52") -> true;
has_color("uwin") -> true;
has_color("vte") -> true;
has_color("vte-2007") -> true;
has_color("vte-2008") -> true;
has_color("vte-2012") -> true;
has_color("vte-2014") -> true;
has_color("vte-256color") -> true;
has_color("vwmterm") -> true;
has_color("wsvt25") -> true;
has_color("wsvt25m") -> true;
has_color("wy350") -> true;
has_color("wy350-vb") -> true;
has_color("wy350-w") -> true;
has_color("wy350-wvb") -> true;
has_color("wy370") -> true;
has_color("wy370-105k") -> true;
has_color("wy370-EPC") -> true;
has_color("wy370-nk") -> true;
has_color("wy370-rv") -> true;
has_color("wy370-vb") -> true;
has_color("wy370-w") -> true;
has_color("wy370-wvb") -> true;
has_color("xfce") -> true;
has_color("xiterm") -> true;
has_color("xnuppc") -> true;
has_color("xnuppc+c") -> true;
has_color("xnuppc-100x37") -> true;
has_color("xnuppc-112x37") -> true;
has_color("xnuppc-128x40") -> true;
has_color("xnuppc-128x48") -> true;
has_color("xnuppc-144x48") -> true;
has_color("xnuppc-160x64") -> true;
has_color("xnuppc-200x64") -> true;
has_color("xnuppc-200x75") -> true;
has_color("xnuppc-256x96") -> true;
has_color("xnuppc-80x25") -> true;
has_color("xnuppc-80x30") -> true;
has_color("xnuppc-90x30") -> true;
has_color("xnuppc-b") -> true;
has_color("xnuppc-f") -> true;
has_color("xnuppc-f2") -> true;
has_color("xterm") -> true;
has_color("xterm+256color") -> true;
has_color("xterm+256setaf") -> true;
has_color("xterm+88color") -> true;
has_color("xterm-1002") -> true;
has_color("xterm-1003") -> true;
has_color("xterm-1005") -> true;
has_color("xterm-1006") -> true;
has_color("xterm-16color") -> true;
has_color("xterm-256color") -> true;
has_color("xterm-88color") -> true;
has_color("xterm-8bit") -> true;
has_color("xterm-basic") -> true;
has_color("xterm-color") -> true;
has_color("xterm-hp") -> true;
has_color("xterm-new") -> true;
has_color("xterm-nic") -> true;
has_color("xterm-noapp") -> true;
has_color("xterm-sco") -> true;
has_color("xterm-sun") -> true;
has_color("xterm-utf8") -> true;
has_color("xterm-vt220") -> true;
has_color("xterm-x10mouse") -> true;
has_color("xterm-x11hilite") -> true;
has_color("xterm-x11mouse") -> true;
has_color("xterm-xf86-v32") -> true;
has_color("xterm-xf86-v33") -> true;
has_color("xterm-xf86-v333") -> true;
has_color("xterm-xf86-v40") -> true;
has_color("xterm-xf86-v43") -> true;
has_color("xterm-xf86-v44") -> true;
has_color("xterm-xfree86") -> true;
has_color("xterm-xi") -> true;
has_color("xterm1") -> true;
has_color("xtermc") -> true;
has_color("xterms-sun") -> true;
has_color(_) -> false.