-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpalettes
executable file
·72 lines (60 loc) · 2.2 KB
/
palettes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env escript
%%! -pa ebin
%% File: palettes
%% Vt100 color names
-define(COLORS, [
default,black,red,green,blue,yellow,magenta,cyan,lightgray,darkgray,
lightred,lightgreen,lightblue,lightyellow,lightmagenta,lightcyan,
white ]).
%% VT100 text attributes
-define(STYLES, [norm,bold,dim,under,blink,reverse]).
main(Args) ->
IsSymbols = length(Args) =:= 1 andalso lists:nth(1,Args) =:= "symbols",
IsFore = length(Args) =:= 1 andalso lists:nth(1,Args) =:= "foreground",
IsBack = length(Args) =:= 1 andalso lists:nth(1,Args) =:= "background",
if IsSymbols orelse length(Args) =:= 0 ->
%% Explain symbolic colors and styles visually
explain("Forground Colors", fg, ?COLORS),
explain("Background Colors", bg, ?COLORS),
explain("Styles", ta, ?STYLES);
true -> ok
end,
if IsFore orelse length(Args) =:= 0 ->
%% Explode foreground numeric color palette visually
palette("Foreground Color Palette", fg);
true -> ok
end,
if IsBack orelse length(Args) =:= 0 ->
%% Explode background numeric color palette visually
palette("Background Color Palette", bg);
true -> ok
end.
explain(Cat,Fn,Sym) ->
io:format("~s:~n", [Cat]),
[ io:format(" - ~s~n", [cake:Fn(X,atom_to_list(X))]) || X <- Sym],
io:format("~n",[]).
%% Create a full palette of 256 cells in 32 rows
palette(S, Fn) ->
%% header
io:format("~s:~n~n", [S]),
%% Generate 32 rows of 8 color codes
Palette = split(8,lists:seq(0,255)),
%% Paint colorized rows
[ row(Z) || Z <- [[ colorize(Y,Fn) || Y <- X] || X <- Palette ]],
%% footer
io:format("~n~n").
%% create a row of 8 fixed with colorized cells
row(A) ->
io:format("~s~s~s~s~s~s~s~s~n", A).
%% create a fixed width colorized cell
colorize(N,Fn) ->
S = integer_to_list(N),
Pad = lists:flatten([ " " || _ <- lists:seq(1,8 - (length(S)+1))]),
cake:Fn(N, io_lib:format("~s#~s", [Pad, S])).
%% Split a list into a list of lists of size N
%% The terminal list may contain less than N elements
split(N,L) when is_list(L) ->
case length(L) >= N of
false -> L;
true -> {H,Rest} = lists:split(N,L), [H] ++ split(N,Rest)
end.