From a6b395759e423aa0844825fb1d8bd2d022fc8ab5 Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Mon, 19 Oct 2015 04:20:00 +0200 Subject: [PATCH 1/5] Simplify color capability detection. --- Makefile | 18 +++ mk-termcap.escript | 99 ++++++++++++ rebar.config | 2 +- rebar.lock | 2 +- src/cf.app.src | 2 +- src/cf.erl | 3 +- src/cf_term.erl | 370 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 491 insertions(+), 5 deletions(-) create mode 100644 Makefile create mode 100755 mk-termcap.escript create mode 100644 src/cf_term.erl diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e07afeb --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +TERMCAP_FILE=http://code.metager.de/source/raw/OpenBSD/src/share/termtypes/termtypes.master + +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 + +termtypes.master.clean: termtypes.master + cat termtypes.master | grep -v '^#' | grep -v '^\s*$$' > termtypes.master.clean + +termtypes.master: + curl -O $(TERMCAP_FILE) diff --git a/mk-termcap.escript b/mk-termcap.escript new file mode 100755 index 0000000..53b0ed4 --- /dev/null +++ b/mk-termcap.escript @@ -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]). + diff --git a/rebar.config b/rebar.config index d79f462..2656fd5 100644 --- a/rebar.config +++ b/rebar.config @@ -1,2 +1,2 @@ {erl_opts, [debug_info]}. -{deps, [{termcap, "0.1.0"}]}. +{deps, []}. diff --git a/rebar.lock b/rebar.lock index 754c999..57afcca 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1 +1 @@ -[{<<"termcap">>,{pkg,<<"termcap">>,<<"0.1.0">>},0}]. +[]. diff --git a/src/cf.app.src b/src/cf.app.src index 7043182..7c7e981 100644 --- a/src/cf.app.src +++ b/src/cf.app.src @@ -1,6 +1,6 @@ {application,cf, [{description,"Terminal colour helper"}, - {vsn,"0.2.0"}, + {vsn,"0.2.1"}, {registered,[]}, {applications,[kernel,stdlib,termcap]}, {env,[]}, diff --git a/src/cf.erl b/src/cf.erl index 11322c6..ab05dd8 100644 --- a/src/cf.erl +++ b/src/cf.erl @@ -102,8 +102,7 @@ colour_term() -> V; undefined -> Term = os:getenv("TERM"), - Caps = termcap:cap(Term), - V = proplists:is_defined("colors", Caps), + V = cf_term:has_color(Term), application:set_env(cf, colour_term, V), V end. diff --git a/src/cf_term.erl b/src/cf_term.erl new file mode 100644 index 0000000..6d6176c --- /dev/null +++ b/src/cf_term.erl @@ -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. From 641d0a009b043a3a19e0ec56c1f4f4dfe918e135 Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Wed, 6 Jul 2016 03:58:24 +0200 Subject: [PATCH 2/5] Add link to GH, closes #1 --- src/cf.app.src | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cf.app.src b/src/cf.app.src index 7c7e981..e85142d 100644 --- a/src/cf.app.src +++ b/src/cf.app.src @@ -1,10 +1,10 @@ {application,cf, [{description,"Terminal colour helper"}, - {vsn,"0.2.1"}, + {vsn,"0.2.2"}, {registered,[]}, - {applications,[kernel,stdlib,termcap]}, + {applications,[kernel,stdlib]}, {env,[]}, {modules,[]}, {maintainers,["Heinz N. Gies "]}, {licenses,["MIT"]}, - {links,[]}]}. + {links,[{"github","https://github.com/project-fifo/cf"}]}]}. From 2ea1755cf22dd4025a961f4a2266d4851a801405 Mon Sep 17 00:00:00 2001 From: Alexey Lebedeff Date: Tue, 2 Aug 2016 14:06:37 +0300 Subject: [PATCH 3/5] 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 =) --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e07afeb..8a7d568 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,10 @@ 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 From 03c34c8e9aab2c7e075f994b92b1c2798cc3534e Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Wed, 14 Jun 2017 21:44:42 +0200 Subject: [PATCH 4/5] Add truecolor --- Makefile | 3 +++ rebar.config | 6 ++++++ src/cf.app.src | 2 +- src/cf.erl | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index e07afeb..928ae6b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,8 @@ 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 diff --git a/rebar.config b/rebar.config index 2656fd5..1873664 100644 --- a/rebar.config +++ b/rebar.config @@ -1,2 +1,8 @@ {erl_opts, [debug_info]}. {deps, []}. + +{profiles, [ + {shell, [ + {deps, [sync]} + ]} + ]}. diff --git a/src/cf.app.src b/src/cf.app.src index e85142d..c6b6826 100644 --- a/src/cf.app.src +++ b/src/cf.app.src @@ -1,6 +1,6 @@ {application,cf, [{description,"Terminal colour helper"}, - {vsn,"0.2.2"}, + {vsn, git}, {registered,[]}, {applications,[kernel,stdlib]}, {env,[]}, diff --git a/src/cf.erl b/src/cf.erl index ab05dd8..ceb350d 100644 --- a/src/cf.erl +++ b/src/cf.erl @@ -38,6 +38,11 @@ %% c,C - cyan %% w,W - white %% +%% true color is supported by using +%% ~!# 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 %% @end print(Fmt, Args) -> @@ -113,12 +118,36 @@ cfmt(S) -> cfmt(S, Enabled) -> lists:flatten(cfmt_(S, Enabled)). -cfmt_([$~,$!, $_, _C | S], false) -> +cfmt_([$~, $!, $_, _C | S], false) -> cfmt_(S, false); -cfmt_([$~,$#, _C | S], false) -> +cfmt_([$~, $#, _C | S], false) -> cfmt_(S, false); -cfmt_([$~,$!, _C | S], false) -> +cfmt_([$~, $!, _C | S], false) -> cfmt_(S, false); +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_([$~, $!, $#, 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) -> [?U |cfmt_(S, Enabled)]; @@ -126,6 +155,7 @@ cfmt_([$~,$!, $^ | S], Enabled) -> [?B | cfmt_(S, Enabled)]; cfmt_([$~,$!, $_, $^ | S], Enabled) -> [?U, ?B | cfmt_(S, Enabled)]; + ?CFMT($!, ?R); ?CFMT($x, ?NX); ?CFMT($X, ?BX); From 2bcf00402db9ca5a4790de7f82c8139baaf8856c Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Wed, 14 Jun 2017 21:57:32 +0200 Subject: [PATCH 5/5] Fix hiding truecolor --- src/cf.erl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cf.erl b/src/cf.erl index ceb350d..bc1f0e7 100644 --- a/src/cf.erl +++ b/src/cf.erl @@ -118,16 +118,17 @@ cfmt(S) -> 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_(S, false); cfmt_([$~, $#, _C | S], false) -> cfmt_(S, false); cfmt_([$~, $!, _C | S], false) -> cfmt_(S, false); -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_([$~, $!, $#, R1, R2, G1, G2, B1, B2 | S], Enabled) -> R = list_to_integer([R1, R2], 16),