-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.awk
213 lines (210 loc) · 6.76 KB
/
render.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
function _escape_ansi_codes(ansi_codes) {
return length(ansi_codes)>0 ? sprintf("\033[%sm", ansi_codes) : "";
}
function _get_ansi_codes(nr, query , properties, i, ansi_codes) {
split("color,background_color,text_decoration_line-1,text_decoration_line-2,font_weight", properties, ",");
for (i in properties) {
if (_has_property_bare(nr, query, properties[i])) {
ansi_codes = ansi_codes ";" _get_property_bare(nr, query, properties[i]);
}
}
return substr(ansi_codes, 2);
}
function _handle_text_overflow(text, width , text_overflow, text_overflow_parts) {
if (length(text) > width) {
text_overflow = _get_property("text_overflow")
if (text_overflow) {
if (index(text_overflow, ",") > 0) {
split(text_overflow, text_overflow_parts, ",") # because of UTF-8
}
else {
text_overflow_parts[1] = length(text_overflow)
text_overflow_parts[2] = text_overflow
}
return substr(text, 0, width - text_overflow_parts[1]) text_overflow_parts[2]
}
return substr(text, 0, width); # just clip the text
}
return text;
}
function _print_inline_block(text, from_index, width , terminal_line, pos, tab_size, tab_width, nr_tabs_expanded, ansi_codes) {
tab_size = _get_property("tab_size");
terminal_line = substr(text, from_index + 1);
# expand tabs
nr_tabs_expanded = 0
while (1) {
pos = index(terminal_line, "\t");
if (pos == 0 || pos > width - tab_size)
break;
tab_width = tab_size - (pos - 1) % tab_size;
sub("\t", sprintf("%" tab_width "s", ""), terminal_line);
nr_tabs_expanded += 1;
}
terminal_line = _handle_text_overflow(terminal_line, width);
# output the line
printf _escape_ansi_codes(_get_ansi_codes(0, _QUERY));
printf _escape_ansi_codes(_get_ansi_codes(NR, _QUERY));
printf "%-" width "s", terminal_line;
printf _escape_ansi_codes("0");
# return the number of characters of the original "text" variable where consumed
return width - ( (tab_size - 1) * nr_tabs_expanded)
}
function _print_border_line(text, from_index, current_width , border_part, border_glyph, chars_consumed) {
border_part = _get_property("border_style_left");
if (border_part) {
if (current_width <= 2) {
_warning("border_style_left", border_part, "Ignored, because width is too small.")
}
else {
border_glyph = get_border_glyph(border_part, 1);
printf("%s", border_glyph);
current_width -= length(border_glyph);
}
}
border_part = _get_property("border_style_right");
if (border_part) {
if (current_width <= 2) {
_warning("border_style_right", border_part, "Ignored, because width is too small.")
}
else {
border_glyph = get_border_glyph(border_part, 3);
current_width -= length(border_glyph);
}
}
chars_consumed = _print_inline_block(text, from_index, current_width)
if (border_part) {
printf("%s", border_glyph);
}
return chars_consumed;
}
function _print_margin_line(text, from_index , current_width, left_margin, right_margin, chars_consumed) {
current_width = _get_property("width");
left_margin = _get_property("margin_left");
if (left_margin >= current_width) {
_warning("margin_left", left_margin, "Ignored, because width is too small.")
left_margin = 0;
}
else if (left_margin > 0) {
printf("%" left_margin "s", " ");
current_width -= left_margin
}
right_margin = _get_property("margin_right");
if (right_margin >= current_width) {
_warning("margin_right", right_margin, "Ignored, because width is too small.")
right_margin = 0;
}
else if (right_margin > 0) {
current_width -= right_margin
}
chars_consumed = _print_border_line(text, from_index, current_width)
if (right_margin > 0) {
printf("%" right_margin "s", " ");
}
printf "\n";
return chars_consumed;
}
function _print_vertical_margin(margin_property_name , value) {
if (margin_property_name == "margin_bottom") {
value = _STATE["last_vertical_margin"];
NR -= 1; # margin_bottom belongs to previous line
}
else { #if (margin_property_name == "margin_top")
value = _get_property(margin_property_name);
value = value - _STATE["last_vertical_margin"]
_STATE["last_vertical_margin"] = _get_property("margin_bottom");
}
while (value > 0) {
_print_margin_line("", 0);
value -= 1;
}
if (margin_property_name == "margin_bottom") {
NR += 1; # restore
}
}
function _print_vertical_border(border_side , border_style, border_width, border_glyph, border_corner_width) {
if (border_side == "bottom") {
NR -= 1;
}
border_width = _get_property("width") - _get_property("margin_left") - _get_property("margin_right");
if (border_side == "bottom") {
border_style = _get_property("border_style_bottom");
_STATE["border_row"] = 3;
border_glyph = get_border_glyph(border_style, 2);
if (border_glyph) {
border_corner_width = length(get_border_glyph(_get_property("border_style_left"), 1) get_border_glyph(_get_property("border_style_right"), 3))
_print_margin_line(str_mul(border_glyph, border_width - border_corner_width), 0);
}
}
else { # border_side == "top"
border_style = _get_property("border_style_top");
_STATE["border_row"] = 1;
border_glyph = get_border_glyph(border_style, 2);
if (border_glyph) {
border_corner_width = length(get_border_glyph(_get_property("border_style_left"), 1) get_border_glyph(_get_property("border_style_right"), 3))
_print_margin_line(str_mul(border_glyph, border_width - border_corner_width), 0);
}
}
if (border_side == "bottom") {
NR += 1;
}
}
function _close_box() {
_print_vertical_border("bottom");
_print_vertical_margin("margin_bottom");
}
function _print_margin_box( text, last_block_name, current_block_name) {
if (_get_property("display") == block) {
if (_has_property("content")) {
text = _get_property("content");
}
else {
text = $0;
}
last_block_name = _STATE["last_block_name"];
current_block_name = _get_current_block_name();
# We only know whether to print a bottom_margin, when the next line DOESN'T belong
# to the next block, so we need to buffer this.
if (NR > 1 && last_block_name != current_block_name) {
_close_box();
}
if (last_block_name != current_block_name) {
_print_vertical_margin("margin_top");
_print_vertical_border("top");
}
_STATE["border_row"] = 2;
if (_get_property("white_space") == pre_wrap) {
_index = 0;
while (_index == 0 || _index < length(text)) {
_index += _print_margin_line(text, _index);
}
}
else { # white_space == pre, just print what fits
_print_margin_line(text, 0);
}
_STATE["last_block_name"] = current_block_name;
}
}
# Main render rule
{
select();
if (_get_property("::before") == "block") {
select("::before");
_print_margin_box();
select();
}
_print_margin_box(); # handle main line
if (_get_property("::after") == "block") {
select("::after");
_print_margin_box();
select();
}
}
END {
# We need to close the last box
NR += 1;
_close_box();
NR -= 1;
if (_DUMP != "") {
_bat_debug(_DUMP);
}
}