-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.awk
328 lines (319 loc) · 10 KB
/
library.awk
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
BEGIN {
# color & background_color
_ENUM["color", black="black" ] = 0; # black
_ENUM["color", red="red" ] = 1;
_ENUM["color", green="green" ] = 2;
_ENUM["color", yellow="yellow" ] = 3;
_ENUM["color", blue="blue" ] = 4;
_ENUM["color", magenta="magenta"] = 5;
_ENUM["color", cyan="cyan" ] = 6;
_ENUM["color", white="white" ] = 7; # white
# bright colors
_ENUM["color", gray="gray" ] = 60;
_ENUM["color", bright_black="bright_black" ] = 60;
_ENUM["color", bright_red="bright_red" ] = 61;
_ENUM["color", bright_green="bright_green" ] = 62;
_ENUM["color", bright_yellow="bright_yellow" ] = 63;
_ENUM["color", bright_blue="bright_blue" ] = 64;
_ENUM["color", bright_magenta="bright_magenta"] = 65;
_ENUM["color", bright_cyan="bright_cyan" ] = 66;
_ENUM["color", bright_white="bright_white" ] = 67; # bright_white
# border_style (value is comma-splittable array, first value is encoding-type)
# 1: telephone number pad, 1..9 (5 is empty) defines edge characters
_ENUM["border_style", none="none"] = "0";
_ENUM["border_style", ascii="ascii"] = "1,+,-,+,|,,|,+,-,+";
_ENUM["border_style", ascii_round="ascii_rounded"] = "1,/,-,\\,|,,|,\\,_,/";
# display
_ENUM["display", block="block"] = block;
_ENUM["display", none="none" ] = none;
# text_decoration_line
_ENUM["text_decoration_line", none="none" ] = 24;
_ENUM["text_decoration_line", underline="underline"] = 4;
_ENUM["text_decoration_line", blink="blink" ] = 5;
# font_weight
_ENUM["font_weight", normal="normal"] = 21;
_ENUM["font_weight", bold="bold" ] = 1;
# white_space
_ENUM["white_space", pre_wrap="pre_wrap"] = pre_wrap;
_ENUM["white_space", pre="pre" ] = pre;
# text_overflow
_ENUM["text_overflow", clip="clip" ] = "";
_ENUM["text_overflow", ellipsis="ellipsis"] = "1,…"; #because of UTF-8, "<char-lenght>,<characters>"
# Initialize
select();
_STATE["last_vertical_margin"] = 0; # for use with margin collapse, and block continuation
_STATE["last_block_name"] = ""; # determine if a line is a block continuation
_STATE["border_row"] = 0; # 0 undefined, 1 border-top, 2 inside border, 3 border-bottom
}
# section is gemodelleerd naar console.group()
# https://developer.mozilla.org/en-US/docs/Web/API/Console#using_groups_in_the_console
# TODO: optioneel een sectionEnd (die wordt inpliciet aangeroepen bij een nieuwe "value" binnen "scope")
function section(scope, value) {
if (scope in _section)
if (_section[scope] == value)
return 0;
_section[scope] = value;
return ! 0;
}
function count(ch , a) {
split($0, a, ch);
return length(a) - 1;
}
function str_mul(str, nr) {
res = sprintf("%" nr "s", " ");
gsub(" ", str, res);
return res;
}
function _no_arg_(value) {
return (value=="") && (value==0)
}
# -=[ internal functions ]=-
function _warning(property, value, message) {
if (_no_arg_(message))
printf "‼️ %s value '%s' is not recognized and will be ignored\n", property, value > "/dev/stderr";
else
printf "‼️ %s with value '%s': %s\n", property, value, message > "/dev/stderr";
}
# _BAT == Big AwkCss Table
function _bat_debug(dump_line ,title, key_combined, key_separate, property_value) {
title = sprintf("---[ BAT DUMP %s ]---", dump_line);
print title str_mul("-", COLS - length(title));
for ( key_combined in _BAT) {
split(key_combined, key_separate, SUBSEP);
if (dump_line == "*" || dump_line == key_separate[1]) {
property_value = _BAT[key_separate[1], key_separate[2], key_separate[3]];
gsub("\t", "\\t", property_value)
printf "%s,%s,%s=='%s' (len:%s)\n", key_separate[1], key_separate[2], key_separate[3], property_value, length(property_value);
}
}
}
# Property setters/getters
function _has_property_bare(nr, query, property_name) {
return (nr, query, property_name) in _BAT;
}
function _get_property_bare(nr, query, property_name) {
return _BAT[nr, query, property_name];
}
function _set_property_bare(nr, query, property_name, property_value) {
_BAT[nr, query, property_name] = property_value;
}
function _has_property(property_name) {
return _has_property_bare(NR, _QUERY, property_name);
}
function _get_property(property_name) {
if (_has_property_bare(NR, _QUERY, property_name))
return _get_property_bare(NR, _QUERY, property_name);
if (_has_property_bare(NR, "", property_name))
return _get_property_bare(NR, "", property_name);
if (_has_property_bare(0, "", property_name))
return _get_property_bare(0, "", property_name);
}
function _set_property(property_name, property_value) {
_set_property_bare(NR, _QUERY, property_name, property_value)
}
function get_border_glyph(style, part , items, glyph_index) {
split(_ENUM["border_style", style], items, ",");
if (items[0] == "0" || _STATE["border_row"] == 0) {
return "";
}
glyph_index = (_STATE["border_row"] - 1) * 3 + (part + 1);
#printf("(%s,%s,%s,%s,%s)", style, _STATE["border_row"], part, glyph_index, items[glyph_index])
return items[glyph_index];
}
# -=[ Public functions ]=-
function select(query) {
if (query) {
if (query ~ /^::(before|after)$/) {
_QUERY = query;
}
else {
_warning("select", query);
return 0; # False
}
}
else {
_QUERY = "";
}
return ! 0;
}
# -=[ Public properties ]=-
function block_name(value) {
if (value)
_set_property("block_name", value);
else
_warning("block_name", value, "You need to supply a value");
}
function _get_current_block_name() {
if (_has_property_bare(NR, _QUERY, "block_name"))
return _get_property_bare(NR, _QUERY, "block_name");
return "" NR; # when no name specified, use line number as name
}
# stylize text
function color(value) {
if (("color", value) in _ENUM)
_set_property("color", 30+_ENUM["color", value]);
else
_warning("color", value);
}
function background_color(value) {
if (("color", value) in _ENUM)
_set_property("background_color", 40+_ENUM["color", value]);
else
_warning("background_color", value);
}
function _text_decoration_line2(value, sequence_nr) {
if (("text_decoration_line", value) in _ENUM)
_set_property("text_decoration_line-" sequence_nr, _ENUM["text_decoration_line", value]);
else
_warning("text_decoration_line", value);
}
function text_decoration_line(value1, value2) {
_text_decoration_line2(value1, 1);
if (value2) _text_decoration_line2(value2, 2);
}
function text_decoration(value1, value2) {
text_decoration_line(value1, value2);
}
function font_weight(value) {
if (("font_weight", value) in _ENUM)
_set_property("background_color", _ENUM["font_weight", value]);
else
_warning("font_weight", value);
}
function _set_property__length(name, value, default_value) {
if (value == "")
value = default_value
if (value==int(value))
_set_property(name, int(value));
else
_warning(name, value);
}
function width(value) {
if (value == "")
_set_property("width", COLS);
else if (value > 0 && value==int(value))
_set_property("width", int(value));
else
_warning("width", value);
}
function tab_size(value) {
if (value == "")
_set_property("tab_size", 8);
else if (value >= 0 && value==int(value))
_set_property("tab_size", int(value));
else
_warning("tab_size", value);
}
function margin_top(value) {
_set_property__length("margin_top", value);
}
function margin_right(value) {
_set_property__length("margin_right", value);
}
function margin_bottom(value) {
_set_property__length("margin_bottom", value);
}
function margin_left(value) {
_set_property__length("margin_left", value);
}
function margin(top, right, bottom, left) {
if (_no_arg_(top))
_warning("margin", ""); # margin needs at least one value
else if (_no_arg_(right)) {
margin_top(top);
margin_right(top);
margin_bottom(top);
margin_left(top);
}
else if (_no_arg_(bottom)) {
margin_top(top);
margin_right(right);
margin_bottom(top);
margin_left(right);
}
else if (_no_arg_(left)) {
margin_top(top);
margin_right(right);
margin_bottom(bottom);
margin_left(right);
}
else {
margin_top(top);
margin_right(right);
margin_bottom(bottom);
margin_left(left);
}
}
function border_style_top(style) {
if (style==unset || ("border_style", style) in _ENUM)
_set_property("border_style_top", style);
else
_warning("border_style_top", style);
}
function border_style_right(style) {
if (style==unset || ("border_style", style) in _ENUM)
_set_property("border_style_right", style);
else
_warning("border_style_right", style);
}
function border_style_bottom(style) {
if (style==unset || ("border_style", style) in _ENUM)
_set_property("border_style_bottom", style);
else
_warning("border_style_bottom", style);
}
function border_style_left(style) {
if (style==unset || ("border_style", style) in _ENUM)
_set_property("border_style_left", style);
else
_warning("border_style_left", style);
}
function border_style(style) {
if (style==unset || ("border_style", style) in _ENUM) {
border_style_top(style);
border_style_right(style);
border_style_bottom(style);
border_style_left(style);
}
else
_warning("border_style", value);
}
function border(style) {
if (_no_arg_(style))
_warning("border", ""); # border needs at least one value
else {
border_style(style);
}
}
function display(value) {
if (("display", value) in _ENUM)
_set_property("display", _ENUM["display",value]);
else
_warning("display", value);
}
function white_space(value) {
#printf "WS[%s:%s]", NR, value
if (("white_space", value) in _ENUM)
_set_property("white_space", _ENUM["white_space", value]);
else
_warning("white_space", value);
}
function text_overflow(value) {
if (("text_overflow", value) in _ENUM)
_set_property("text_overflow", _ENUM["text_overflow", value]);
else
_set_property("text_overflow", value); # Use supplied string as text-overflow (experimental)
}
function content(value , display_value) {
_set_property("content", value);
if (_QUERY ~ /^::(before|after)$/) {
# Optimization, so renderer can quickly see if there is before/after-work.
if (_has_property("display")) {
display_value = _get_property("display");
}
else {
display_value = "block"; # default value
}
_set_property_bare(NR, "", _QUERY, display_value);
}
}