From f9067af37f8c6636db1865188bdec2b8644f4591 Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Tue, 22 Sep 2015 02:03:01 +0200 Subject: [PATCH] first commit --- .gitignore | 19 ++++++ LICENSE | 29 +++++++++ README.md | 32 ++++++++++ rebar.config | 2 + rebar.lock | 1 + src/cf.app.src | 14 +++++ src/cf.erl | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 259 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 rebar.config create mode 100644 rebar.lock create mode 100644 src/cf.app.src create mode 100644 src/cf.erl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a939dce --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +.rebar3 +_* +.eunit +*.o +*.beam +*.plt +*.swp +*.swo +.erlang.cookie +ebin +log +erl_crash.dump +.rebar +_rel +_deps +_plugins +_tdeps +logs +_build \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a82fbe2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +Copyright (c) 2015, Project-FiFo UG +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +* The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..23c2d92 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +cf +===== + +A helper library for termial colour printing extending the io:format +syntax to add colours. + +```erlang +%% Effectively the same as io:format just takes the additional color +%% console text colour can be set by ~!****. ~_**** +%% will produce underlined text and ~#**** will change the +%% background. Both ~# and ~_ only work with lowercase colours. +%% An uppercase letersindicate bold colours. +%% +%% The colour can be one of: +%% +%% ! - resets the output +%% x,X - black +%% r,R - red +%% g,G - greeen +%% y,Y - yellow +%% b,B - blue +%% m,M - magenta +%% c,C - cyan +%% w,W - white +%% +%% The function will disable colours on non x term termials +``` + +Build +----- + + $ rebar3 compile diff --git a/rebar.config b/rebar.config new file mode 100644 index 0000000..f618f3e --- /dev/null +++ b/rebar.config @@ -0,0 +1,2 @@ +{erl_opts, [debug_info]}. +{deps, []}. \ No newline at end of file diff --git a/rebar.lock b/rebar.lock new file mode 100644 index 0000000..57afcca --- /dev/null +++ b/rebar.lock @@ -0,0 +1 @@ +[]. diff --git a/src/cf.app.src b/src/cf.app.src new file mode 100644 index 0000000..f833e0a --- /dev/null +++ b/src/cf.app.src @@ -0,0 +1,14 @@ +{application, 'cf', + [{description, "Terminal colour helper"}, + {vsn, "0.1.0"}, + {registered, []}, + {applications, + [kernel, + stdlib + ]}, + {env,[]}, + {modules, []}, + {contributors, ["Heinz N. Gies "]}, + {licenses, ["MIT"]}, + {links, []} + ]}. diff --git a/src/cf.erl b/src/cf.erl new file mode 100644 index 0000000..dfc0038 --- /dev/null +++ b/src/cf.erl @@ -0,0 +1,162 @@ +%%%------------------------------------------------------------------- +%%% @author Heinz Nikolaus Gies +%%% @copyright (C) 2015, Project-FiFo UG +%%% @doc +%%% Printing library for coloured console output, extends the format +%%% strings by adding ~! (forground) ~# (background) and ~_ (underline) +%%% terminal colours. +%%% @end +%%% Created : 22 Sep 2015 by Heinz Nikolaus Gies +%%%------------------------------------------------------------------- +-module(cf). + + +%% API exports +-export([format/1, format/2]). +-export([print/1, print/2]). + +%%==================================================================== +%% API functions +%%==================================================================== +%% @doc Prints a coloured string. +%% Effectively the same as io:format just takes the additional color +%% console text colour can be set by ~!****. ~_**** +%% will produce underlined text and ~#**** will change the +%% background. Both ~# and ~_ only work with lowercase colours. +%% An uppercase letersindicate bold colours. +%% +%% The colour can be one of: +%% +%% ! - resets the output +%% x,X - black +%% r,R - red +%% g,G - greeen +%% y,Y - yellow +%% b,B - blue +%% m,M - magenta +%% c,C - cyan +%% w,W - white +%% +%% The function will disable colours on non x term termials +%% @end +print(Fmt, Args) -> + io:format(cfmt(Fmt), Args). + +%% @doc Formates a coloured string +%% Arguments are the same as for print/2, just returns a string as +%% io_lib:format/2 does instead of printing it to stdout. +%% @end +format(Fmt, Args) -> + io_lib:format(cfmt(Fmt), Args). + + +print(Fmt) -> + print(Fmt, []). +format(Fmt) -> + format(Fmt, []). + +%%==================================================================== +%% Internal functions +%%==================================================================== + + +-define(NX, "\033[0;30m"). +-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(BX, "\033[1;30m"). +-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(UX, "\033[4;30m"). +-define(UR, "\033[4;31m"). +-define(UG, "\033[4;32m"). +-define(UY, "\033[4;33m"). +-define(UB, "\033[4;34m"). +-define(UM, "\033[4;35m"). +-define(UC, "\033[4;36m"). +-define(UW, "\033[4;37m"). +-define(BGX, "\033[40m"). +-define(BGR, "\033[41m"). +-define(BGG, "\033[42m"). +-define(BGY, "\033[43m"). +-define(BGB, "\033[44m"). +-define(BGM, "\033[45m"). +-define(BGC, "\033[46m"). +-define(BGW, "\033[47m"). +-define(R, "\033[0m"). +-define(CFMT(Char, Colour), + cfmt_([$~, $!, Char | S], Enabled) -> [Colour | cfmt_(S, Enabled)]). +-define(CFMT_BG(Char, Colour), + cfmt_([$~, $#, Char | S], Enabled) -> [Colour | cfmt_(S, Enabled)]). +-define(CFMT_U(Char, Colour), + cfmt_([$~, $_, Char | S], Enabled) -> [Colour | cfmt_(S, Enabled)]). + + +cfmt(S) -> + cfmt(S, os:getenv("TERM") =:= "xterm"). + +cfmt(S, Enabled) -> + lists:flatten(cfmt_(S, Enabled)). + +cfmt_([$~,$!, _C | S], false) -> + cfmt_(S, false); +cfmt_([$~,$_, _C | S], false) -> + cfmt_(S, false); +cfmt_([$~,$#, _C | S], false) -> + cfmt_(S, false); + +?CFMT($!, ?R); +?CFMT($x, ?NX); +?CFMT($X, ?BX); +?CFMT($r, ?NR); +?CFMT($R, ?BR); +?CFMT($g, ?NG); +?CFMT($G, ?BG); +?CFMT($y, ?NY); +?CFMT($Y, ?BY); +?CFMT($b, ?NB); +?CFMT($B, ?BB); +?CFMT($m, ?NM); +?CFMT($M, ?BM); +?CFMT($c, ?NC); +?CFMT($C, ?BC); +?CFMT($w, ?NW); +?CFMT($W, ?BW); + +?CFMT_BG($x, ?BGX); +?CFMT_BG($r, ?BGR); +?CFMT_BG($g, ?BGG); +?CFMT_BG($y, ?BGY); +?CFMT_BG($b, ?BGB); +?CFMT_BG($m, ?BGM); +?CFMT_BG($c, ?BGC); +?CFMT_BG($w, ?BGW); + +?CFMT_U($x, ?UX); +?CFMT_U($r, ?UR); +?CFMT_U($g, ?UG); +?CFMT_U($y, ?UY); +?CFMT_U($b, ?UB); +?CFMT_U($m, ?UM); +?CFMT_U($c, ?UC); +?CFMT_U($w, ?UW); + +cfmt_([$~,$~ | S], Enabled) -> + [$~,$~ | cfmt_(S, Enabled)]; + +cfmt_([C | S], Enabled) -> + [C | cfmt_(S, Enabled)]; + +cfmt_([], false) -> + ""; +cfmt_([], _Enabled) -> + ?R.