-
Notifications
You must be signed in to change notification settings - Fork 3
/
page.c
532 lines (498 loc) · 14.6 KB
/
page.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "les.h"
#include <term.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *buf;
size_t buf_size;
size_t buf_len;
char *left;
size_t left_size;
size_t left_len;
int left_width;
char *right;
size_t right_size;
size_t right_len;
int right_width;
char *tty;
size_t tty_size;
size_t tty_len;
char *matches;
size_t matches_size;
size_t matches_len;
} status_t;
status_t *status = NULL;
int atmatch = 0;
int athighlight = 0;
char *escapes = NULL;
size_t escapes_len = 0;
size_t escapes_size = 0;
char *human_readable (double size) {
static char buf[32];
static char power[] = "BKMGTPEZY";
int i;
for (i = 0; i < sizeof power / sizeof power[0]; i++) {
if (size < 1024)
break;
size /= 1024;
}
int hundredths = (int)(size * 100) % 100;
if (hundredths) {
snprintf(buf, sizeof buf, "%.2f%c", size, power[i]);
}
else {
snprintf(buf, sizeof buf, "%.0f%c", size, power[i]);
}
return buf;
}
void stage_status () {
status->matches_len = 0;
if (active_search && tabb->search_version == search_version) {
if (tabb->matches_len == 0) {
status->matches_len = snprintf(status->matches, status->matches_size, " 0 matches");
}
else if (tabb->matches_len == 1) {
status->matches_len = snprintf(status->matches, status->matches_size, " 1 match");
}
else {
status->matches_len = snprintf(status->matches, status->matches_size, " %lu/%lu matches", (long unsigned int) tabb->current_match + 1, (long unsigned int) tabb->matches_len);
}
}
char *hrsize = human_readable(tabb->buf_len);
status->right_len = snprintf(
status->right, status->right_size,
"%.*s%.*s %d/%d %s",
(int) status->tty_len, status->tty,
(int) status->matches_len, status->matches,
tabb->line, tabb->nlines, hrsize);
status->right_width = strnwidth(status->right, status->right_len);
status->left_len = snprintf(
status->left, status->left_size,
"%s%.*s",
tabb->name, (int) tabb->mesg_len, tabb->mesg);
status->buf_len = 0;
while (status->buf_size < columns + status->right_len + status->left_len) {
status->buf_size *= 2;
status->buf = realloc(status->buf, status->buf_size);
}
int i;
int width = 0;
charinfo_t cinfo;
int right = columns - status->right_width;
for (i = 0; i < status->left_len;) {
if (width >= right) {
break;
}
get_char_info(&cinfo, status->left, i);
memcpy(status->buf + status->buf_len, status->left + i, cinfo.len);
status->buf_len += cinfo.len;
width += cinfo.width;
i += cinfo.len;
}
while (width < right) {
status->buf[status->buf_len] = ' ';
status->buf_len++;
width++;
}
for (i = 0; i < status->right_len; i++) {
status->buf[status->buf_len] = status->right[i];
status->buf_len++;
}
stage_cat(tparm(cursor_address, lines - 1, 0));
stage_cat(tparm(set_a_background, 16 + 36*0 + 6*0 + 2));
width = 0;
int j = 0;
for (i = 0; i < status->buf_len;) {
get_char_info(&cinfo, status->buf, i);
if (j == 0 && (!tabb->buf_len || width >= columns * tabb->pos / tabb->buf_len)) {
stage_cat(tparm(set_a_background, 16 + 36*0 + 6*1 + 5));
j++;
}
else if (j == 1 && tabb->buf_len && width >= columns * tlines[tlines_len - 1].end_pos / tabb->buf_len) {
stage_cat(exit_attribute_mode);
j++;
}
stage_ncat(status->buf + i, cinfo.len);
width += cinfo.width;
i += cinfo.len;
}
stage_cat(exit_attribute_mode);
}
void draw_status () {
stage_status();
stage_write();
}
// In man page output, text is underlined by placing and underscore
// followed by a backspace followed by the character to be underlined.
// text is bolded by placing the character followed by a backspace
// then the same character again.
void stage_backspace (charinfo_t *cinfo, char *buf, int i) {
if (i == 0) {
cinfo->width = 2;
cinfo->len = 1;
stage_cat("^H");
}
else if (buf[i - 1] == '_') {
get_char_info(cinfo, buf, i + 1);
stage_cat("\b");
stage_cat(enter_underline_mode);
stage_ncat(buf + i + 1, cinfo->len);
stage_cat(exit_underline_mode);
cinfo->width = 0;
cinfo->len += 1;
}
else if (buf[i - 1] == buf[i + 1] || buf[i - 1] == '+') {
get_char_info(cinfo, buf, i + 1);
stage_cat("\b");
stage_cat(enter_bold_mode);
stage_ncat(buf + i + 1, cinfo->len);
stage_cat(exit_attribute_mode);
cinfo->width = 0;
cinfo->len += 1;
}
else {
cinfo->width = 2;
cinfo->len = 1;
stage_cat("^H");
}
}
void escapes_record (charinfo_t *cinfo, char *buf, int i) {
if (buf[i] == '\e' && cinfo->len > 1) {
if (strncmp(buf + i, "\e[0m", cinfo->len) == 0 ||
strncmp(buf + i, "\e[m", cinfo->len) == 0) {
escapes_len = 0;
}
else {
if (escapes_len + cinfo->len <= escapes_size) {
strncpy(escapes + escapes_len, buf + i, cinfo->len);
escapes_len += cinfo->len;
}
}
}
}
void escapes_start () {
if (escapes_len) {
stage_ncat(escapes, escapes_len);
}
}
void escapes_end () {
if (escapes_len) {
stage_cat(exit_attribute_mode);
}
}
void highlight_match3 () {
if (escapes_len) {
stage_cat(exit_attribute_mode);
}
stage_cat(tparm(set_a_foreground, 0));
stage_cat(enter_bold_mode);
if (atmatch == tabb->current_match) {
stage_cat(tparm(set_a_background, 10));
}
else {
stage_cat(tparm(set_a_background, 12));
}
}
void highlight_match_start (char *buf, int i) {
if (!tabb->matches_len || atmatch >= tabb->matches_len) {
return;
}
if (i > tabb->matches[atmatch].start && i < tabb->matches[atmatch].end) {
highlight_match3();
}
}
void highlight_match_end (char *buf, int i) {
if (!tabb->matches_len || atmatch >= tabb->matches_len) {
return;
}
if (i > tabb->matches[atmatch].start && i <= tabb->matches[atmatch].end) {
stage_cat(exit_attribute_mode);
}
}
void highlight_match (char *buf, int i) {
if (!tabb->matches_len) {
return;
}
if (atmatch < tabb->matches_len && tabb->matches[atmatch].start != tabb->matches[atmatch].end && i == tabb->matches[atmatch].end) {
stage_cat(exit_attribute_mode);
escapes_start();
atmatch++;
}
if (atmatch < tabb->matches_len && i == tabb->matches[atmatch].start) {
highlight_match3();
}
}
void highlight_match2 (char *buf, int i) {
if (!tabb->matches_len || atmatch >= tabb->matches_len) {
return;
}
if (i == tabb->matches[atmatch].end) {
stage_cat(exit_attribute_mode);
escapes_start();
atmatch++;
}
}
void highlight2 () {
if (tabb->highlights[athighlight].type == UNDERLINED) {
stage_cat(enter_underline_mode);
}
else {
stage_cat(enter_bold_mode);
}
}
void highlight_start (char *buf, int i) {
if (!tabb->highlights_len || athighlight >= tabb->highlights_len) {
return;
}
if (i > tabb->highlights[athighlight].start && i < tabb->highlights[athighlight].end) {
highlight2();
}
}
void highlight_end (char *buf, int i) {
if (!tabb->highlights_len || athighlight >= tabb->highlights_len) {
return;
}
if (i > tabb->highlights[athighlight].start && i <= tabb->highlights[athighlight].end) {
stage_cat(exit_attribute_mode);
}
}
void highlight (char *buf, int i) {
if (!tabb->highlights_len) {
return;
}
if (athighlight < tabb->highlights_len && i == tabb->highlights[athighlight].end) {
stage_cat(exit_attribute_mode);
escapes_start();
athighlight++;
}
if (athighlight < tabb->highlights_len && i == tabb->highlights[athighlight].start) {
highlight2();
}
}
void stage_character (charinfo_t *cinfo, char *buf, int i) {
static char str[16];
int j;
unsigned char c = buf[i];
if (cinfo->error) {
sprintf(str, "[%02X]", c);
stage_cat(str);
}
else if (c == '\t') {
for (j = 0; j < tab_width; j++) {
stage_cat(" ");
}
}
else if (c == '\b') {
stage_backspace(cinfo, buf, i);
}
else if (buf[i] == '\e' && cinfo->len > 1) {
stage_ncat(buf + i, cinfo->len);
}
else if (c == 0x00) {
stage_cat("·");
}
else if (c < 0x20) {
sprintf(str, "^%c", 0x40 + c);
stage_cat(str);
}
else if (c == 0x7f) {
sprintf(str, "^%c", -0x40 + c);
stage_cat(str);
}
else if (cinfo->codepoint >= 0x80 && cinfo->codepoint <= 0x9f) {
sprintf(str, "*%c", 0x40 + cinfo->codepoint - 0x80);
stage_cat(str);
}
else {
stage_ncat(buf + i, cinfo->len);
}
}
void stage_line_wrap (tline_t *tline) {
charinfo_t cinfo;
int width = 0;
int i = tline->pos;
escapes_start();
highlight_start(tabb->buf, i);
highlight_match_start(tabb->buf, i);
while (i < tline->end_pos) {
get_char_info(&cinfo, tabb->buf, i);
highlight(tabb->buf, i);
highlight_match(tabb->buf, i);
if ((tabb->buf[i] == '\r' && tabb->buf[i + 1] == '\n') || tabb->buf[i] == '\n') {
highlight_match2(tabb->buf, i);
break;
}
escapes_record(&cinfo, tabb->buf, i);
stage_character(&cinfo, tabb->buf, i);
highlight_match2(tabb->buf, i);
width += cinfo.width;
i += cinfo.len;
}
highlight_end(tabb->buf, i);
highlight_match_end(tabb->buf, i);
escapes_end();
if (width < columns) {
stage_cat(clr_eol);
}
}
void stage_line_nowrap (tline_t *tline) {
int width = 0;
charinfo_t cinfo;
int i = tline->pos;
escapes_start();
if (atmatch < tabb->matches_len && tabb->matches[atmatch].end < i) {
atmatch++;
}
if (athighlight < tabb->highlights_len && tabb->highlights[athighlight].end < i) {
athighlight++;
}
highlight_start(tabb->buf, i);
highlight_match_start(tabb->buf, i);
while (i < tline->end_pos) {
get_char_info(&cinfo, tabb->buf, i);
if (width >= tabb->column && (!tabb->column || cinfo.width)) {
break;
}
if (tabb->buf[i] == 0x1b && cinfo.len > 1) {
escapes_record(&cinfo, tabb->buf, i);
stage_ncat(tabb->buf + i, cinfo.len);
}
highlight(tabb->buf, i);
highlight_match(tabb->buf, i);
highlight_match2(tabb->buf, i);
width += cinfo.width;
i += cinfo.len;
}
if (i == tline->end_pos) {
highlight_end(tabb->buf, i);
highlight_match_end(tabb->buf, i);
escapes_end();
stage_cat(clr_eol);
return;
}
while (i < tline->end_pos) {
get_char_info(&cinfo, tabb->buf, i);
if (width + cinfo.width > columns + tabb->column) {
break;
}
if (tabb->buf[i] == 0x1b && cinfo.len > 1) {
escapes_record(&cinfo, tabb->buf, i);
}
highlight(tabb->buf, i);
highlight_match(tabb->buf, i);
if ((tabb->buf[i] == '\r' && tabb->buf[i + 1] == '\n') || tabb->buf[i] == '\n') {
highlight_match2(tabb->buf, i);
break;
}
stage_character(&cinfo, tabb->buf, i);
highlight_match2(tabb->buf, i);
width += cinfo.width;
i += cinfo.len;
}
highlight_end(tabb->buf, i);
highlight_match_end(tabb->buf, i);
escapes_end();
if (width < columns + tabb->column) {
stage_cat(clr_eol);
}
}
void stage_tab2 (int n, tline_t *tlines, size_t tlines_len) {
int i;
if (tabb->matches_len) {
atmatch = 0;
for (i = 0; i < tabb->matches_len; i++) {
if (tabb->matches[i].end >= tlines[0].pos) {
atmatch = i;
break;
}
}
}
if (tabb->highlights_len) {
athighlight = 0;
for (i = 0; i < tabb->highlights_len; i++) {
if (tabb->highlights[i].end >= tlines[0].pos) {
athighlight = i;
break;
}
}
}
escapes_len = 0;
for (i = 0; i < n; i++) {
if (i < tlines_len) {
if (line_wrap) {
stage_line_wrap(tlines + i);
}
else {
stage_line_nowrap(tlines + i);
}
}
else {
stage_cat(tparm(set_a_foreground, 4));
stage_cat("~");
stage_cat(clr_eol);
stage_cat(exit_attribute_mode);
}
if (i != n - 1) {
stage_cat("\n");
}
}
}
void stage_tab () {
stage_cat(tparm(cursor_address, line1, 0));
get_tlines(tabb->buf, tabb->buf_len, tabb->pos, lines - line1 - 1, &tlines, &tlines_len, &tlines_size);
stage_tab2(lines - line1 - 1, tlines, tlines_len);
stage_status();
}
void draw_tab () {
stage_tab();
stage_write();
}
void init_page () {
status = malloc(sizeof (status_t));
status->buf_size = 1024;
status->buf_len = 0;
status->buf = malloc(status->buf_size);
status->left_size = 1024;
status->left_len = 0;
status->left = malloc(status->left_size);
status->right_size = 1024;
status->right_len = 0;
status->right = malloc(status->right_size);
status->tty_size = 64;
status->tty_len = 0;
status->tty = malloc(status->tty_size);
status->matches_size = 64;
status->matches_len = 0;
status->matches = malloc(status->matches_size);
escapes_size = 256;
escapes_len = 0;
escapes = malloc(escapes_size);
}
void set_ttybuf (charinfo_t *cinfo, char *buf, int len) {
int i;
status->tty[0] = ' ';
status->tty_len = 1;
int retval;
for (i = 0; i < len; i++) {
if (status->tty_len + 3 > status->tty_size) {
break;
}
unsigned char c = buf[i];
if (c < 0x20) {
retval = sprintf(status->tty + status->tty_len, "^%c", 0x40 + c);
status->tty_len += retval;
}
else if (c == 0x7f) {
retval = sprintf(status->tty + status->tty_len, "^?");
status->tty_len += retval;
}
else if (c == 0x20) {
retval = sprintf(status->tty + status->tty_len, "␣");
status->tty_len += retval;
}
else {
status->tty[status->tty_len] = c;
status->tty_len++;
}
}
}