forked from okbob/pspg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpager.c
1731 lines (1493 loc) · 38.4 KB
/
pager.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef __FreeBSD__
#define _WITH_GETLINE
#include <ncurses/curses.h>
#else
#include <curses.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <locale.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#define FILENAME "pg_class.txt"
#define STYLE 1
typedef struct LineBuffer
{
int first_row;
int nrows;
char *rows[1000];
struct LineBuffer *next;
} LineBuffer;
/*
* Available formats of headline chars
*
* L, R .. outer border
* I .. inner border
* d .. data
*/
/*
* This structure should be immutable
*/
typedef struct
{
int border_top_row; /* nrow of bootom outer border or -1 */
int border_head_row; /* nrow of head outer (required) */
int border_bottom_row; /* nrow of bottom outer border or -1 */
int border_type; /* detected type of border: 0, 1, 2 */
char linestyle; /* detected linestyle: a, u */
bool is_expanded_mode; /* true when data are in expanded mode */
int expanded_info_minx; /* begin of info in \x mode .. RECORD x */
char title[65]; /* detected title (trimmed) or NULL */
int title_rows; /* number of rows used as table title (skipped later) */
char filename[65]; /* filename (printed on top bar) */
LineBuffer rows; /* list of rows buffers */
int maxy; /* maxy of used pad area with data */
int maxx; /* maxx of used pad area with data */
LineBuffer footer; /* footer rows */
int footer_maxy; /* maxy of used pad area for footer */
int footer_maxx; /* maxx of used pad area for footer */
int maxbytes; /* max length of line in bytes */
char *headline; /* header separator line */
int headline_size; /* size of headerline in bytes */
char *headline_transl; /* translated headline */
int headline_char_size; /* size of headerline in chars */
int last_data_row; /* last line of data row */
int last_row; /* last not empty row */
} DataDesc;
/*
* This structure can be muttable - depends on displayed data
*/
typedef struct
{
int fix_rows_rows; /* number of fixed rows in window rows */
int fix_cols_cols; /* number of fixed colums in window rows */
int maxy; /* max y size of screen */
int maxx; /* max x size of screen */
WINDOW *luc; /* window for left upper corner */
WINDOW *fix_rows; /* window for fixed rows */
WINDOW *fix_cols; /* window for fixed columns */
WINDOW *rows; /* window for data */
WINDOW *footer; /* window for footer */
WINDOW *top_bar; /* top bar window */
WINDOW *bottom_bar; /* bottom bar window */
int cursor_y; /* y pos of virtual cursor */
int cursor_x; /* x pos of virtual cursor */
int theme; /* color theme number */
char searchterm[256]; /* currently active search input */
} ScrDesc;
static int
min_int(int a, int b)
{
if (a < b)
return a;
else
return b;
}
/*
* Returns length of utf8 string in chars.
*/
static size_t
utf8len(char *s)
{
size_t len = 0;
for (; *s; ++s)
if ((*s & 0xC0) != 0x80)
++len;
return len;
}
/*
* Returns length of utf8 char in bytes
*/
static int
utf8charlen(char ch)
{
if ((ch & 0xF0) == 0xF0)
return 4;
if ((ch & 0xE0) == 0xE0)
return 3;
if ((ch & 0xC0) == 0xC0)
return 2;
return 1;
}
#if NCURSES_MOUSE_VERSION > 1
#define MOUSE_WHEEL_BUTTONS 1
#endif
//#define DEBUG_COLORS 1
/*
* Translate from UTF8 to semantic characters.
*/
static bool
translate_headline(DataDesc *desc)
{
char *transl;
char *srcptr;
char *destptr;
char *last_black_char = NULL;
bool broken_format = false;
int processed_chars = 0;
bool is_expanded_info = false;
srcptr = desc->headline;
destptr = malloc(desc->headline_size + 1);
memset(destptr, 0, desc->headline_size + 1);
desc->headline_transl = destptr;
desc->linestyle = 'a';
desc->border_type = 0;
desc->expanded_info_minx = -1;
while (*srcptr != '\0' && *srcptr != '\n' && *srcptr != '\r')
{
/* only spaces can be after known right border */
if (last_black_char != NULL && *last_black_char == 'R')
{
if (*srcptr != ' ')
{
broken_format = true;
break;
}
}
if (*srcptr != ' ')
last_black_char = destptr;
if (desc->is_expanded_mode && *srcptr == '[')
{
if (desc->expanded_info_minx != -1)
{
broken_format = true;
break;
}
/* entry to expanded info mode */
is_expanded_info = true;
desc->expanded_info_minx = processed_chars;
*destptr++ = 'd';
srcptr += utf8charlen(*srcptr);
}
else if (is_expanded_info)
{
if (*srcptr == ']')
{
is_expanded_info = false;
}
*destptr++ = 'd';
srcptr += utf8charlen(*srcptr);
}
else if (strncmp(srcptr, "\342\224\214", 3) == 0 || /* ┌ */
strncmp(srcptr, "\342\225\224", 3) == 0) /* ╔ */
{
/* should be expanded mode */
if (processed_chars > 0 || !desc->is_expanded_mode)
{
broken_format = true;
break;
}
desc->linestyle = 'u';
desc->border_type = 2;
*destptr++ = 'L';
srcptr += 3;
}
else if (strncmp(srcptr, "\342\224\220", 3) == 0 || /* ┐ */
strncmp(srcptr, "\342\225\227", 3) == 0) /* ╗ */
{
if (desc->linestyle != 'u' || desc->border_type != 2 ||
!desc->is_expanded_mode)
{
broken_format = true;
break;
}
*destptr++ = 'R';
srcptr += 3;
}
else if (strncmp(srcptr, "\342\224\254", 3) == 0 || /* ┬╤ */
strncmp(srcptr, "\342\225\244", 3) == 0 ||
strncmp(srcptr, "\342\225\245", 3) == 0 || /* ╥╦ */
strncmp(srcptr, "\342\225\246", 3) == 0)
{
if (desc->linestyle != 'u' || !desc->is_expanded_mode)
{
broken_format = true;
break;
}
if (desc->border_type == 0)
desc->border_type = 1;
*destptr++ = 'I';
srcptr += 3;
}
else if (strncmp(srcptr, "\342\224\234", 3) == 0 || /* ├╟ */
strncmp(srcptr, "\342\225\237", 3) == 0 ||
strncmp(srcptr, "\342\225\236", 3) == 0 || /* ╞╠ */
strncmp(srcptr, "\342\225\240", 3) == 0)
{
if (processed_chars > 0)
{
broken_format = true;
break;
}
desc->linestyle = 'u';
desc->border_type = 2;
*destptr++ = 'L';
srcptr += 3;
}
else if (strncmp(srcptr, "\342\224\244", 3) == 0 || /* ┤╢ */
strncmp(srcptr, "\342\225\242", 3) == 0 ||
strncmp(srcptr, "\342\225\241", 3) == 0 || /* ╡╣ */
strncmp(srcptr, "\342\225\243", 3) == 0)
{
if (desc->linestyle != 'u' || desc->border_type != 2)
{
broken_format = true;
break;
}
*destptr++ = 'R';
srcptr += 3;
}
else if (strncmp(srcptr, "\342\224\274", 3) == 0 || /* ┼╪ */
strncmp(srcptr, "\342\225\252", 3) == 0 ||
strncmp(srcptr, "\342\225\253", 3) == 0 || /* ╫╬ */
strncmp(srcptr, "\342\225\254", 3) == 0)
{
if (desc->linestyle != 'u')
{
broken_format = true;
break;
}
if (desc->border_type == 0)
desc->border_type = 1;
*destptr++ = 'I';
srcptr += 3;
}
else if (strncmp(srcptr, "\342\224\200", 3) == 0 || /* ─ */
strncmp(srcptr, "\342\225\220", 3) == 0) /* ═ */
{
if (processed_chars == 0)
{
desc->linestyle = 'u';
}
else if (desc->linestyle != 'u')
{
broken_format = true;
break;
}
*destptr++ = 'd';
srcptr += 3;
}
else if (*srcptr == '+')
{
if (processed_chars == 0)
{
*destptr++ = 'L';
desc->linestyle = 'a';
desc->border_type = 2;
}
else
{
if (desc->linestyle != 'a')
{
broken_format = true;
break;
}
if (desc->border_type == 0)
desc->border_type = 1;
*destptr++ = (srcptr[1] == '-') ? 'I' : 'R';
}
srcptr += 1;
}
else if (*srcptr == '-')
{
if (processed_chars == 0)
{
desc->linestyle = 'a';
}
else if (desc->linestyle != 'a')
{
broken_format = true;
break;
}
*destptr++ = 'd';
srcptr += 1;
}
else if (*srcptr == ' ')
{
if (desc->border_type != 0)
{
broken_format = true;
break;
}
*destptr++ = 'I';
srcptr += 1;
}
else
{
broken_format = true;
break;
}
processed_chars += 1;
}
/* should not be - unclosed header */
if (is_expanded_info)
broken_format = true;
else if (desc->is_expanded_mode && desc->expanded_info_minx == -1)
broken_format = true;
/* trim ending spaces */
if (!broken_format && last_black_char != 0)
{
last_black_char[1] = '\0';
desc->headline_char_size = strlen(desc->headline_transl);
return true;
}
free(desc->headline_transl);
desc->headline_transl = NULL;
return false;
}
/*
* Returns true when char is left upper corner
*/
static bool
isTopLeftChar(char *str)
{
const char *u1 = "\342\224\214";
const char *u2 = "\342\225\224";
if (str[0] == '+')
return true;
if (strncmp(str, u1, 3) == 0)
return true;
if (strncmp(str, u2, 3) == 0)
return true;
return false;
}
/*
* Returns true when char is top left header char
*/
static bool
isHeadLeftChar(char *str)
{
const char *u1 = "\342\224\200";
const char *u2 = "\342\225\220";
const char *u3 = "\342\225\236";
const char *u4 = "\342\225\241";
if (str[0] == '+' || str[0] == '-')
return true;
if (strncmp(str, u1, 3) == 0)
return true;
if (strncmp(str, u2, 3) == 0)
return true;
if (strncmp(str, u3, 3) == 0)
return true;
if (strncmp(str, u4, 3) == 0)
return true;
return false;
}
/*
* Returns true when char is bottom left corner
*/
static bool
isBottomLeftChar(char *str)
{
const char *u1 = "\342\224\224";
const char *u2 = "\342\225\232";
if (str[0] == '+')
return true;
if (strncmp(str, u1, 3) == 0)
return true;
if (strncmp(str, u2, 3) == 0)
return true;
return false;
}
/*
* detect different faces of headline in extended mode
*/
static bool
is_expanded_header(char *str, int *ei_minx, int *ei_maxx)
{
int pos = 0;
if (*str == '+')
{
str += 1;
pos += 1;
}
else if (strncmp(str, "\342\224\214", 3) == 0 || /* ┌ */
strncmp(str, "\342\225\224", 3) == 0 || /* ╔ */
strncmp(str, "\342\224\234", 3) == 0 || /* ├╟ */
strncmp(str, "\342\225\237", 3) == 0 ||
strncmp(str, "\342\225\236", 3) == 0 || /* ╞╠ */
strncmp(str, "\342\225\240", 3) == 0)
{
str += 3;
pos += 1;
}
if (*str == '-')
{
str += 1;
pos += 1;
}
else if (strncmp(str, "\342\224\200", 3) == 0 || /* ─ */
strncmp(str, "\342\225\220", 3) == 0) /* ═ */
{
str += 3;
pos += 1;
}
if (strncmp(str, "[ ", 2) != 0)
return false;
if (ei_minx != NULL && ei_maxx != NULL)
{
pos += 2;
str += 2;
*ei_minx = pos - 1;
while (*str != ']' && *str != '\0')
{
pos += 1;
str += utf8charlen(*str);
}
*ei_maxx = pos - 1;
}
return true;
}
/*
* Copy trimmed string
*/
static void
strncpytrim(char *dest, const char *src,
size_t ndest, size_t nsrc)
{
const char *endptr;
endptr = src + nsrc - 1;
/* skip trailing spaces */
while (*src == ' ')
{
if (nsrc-- <= 0)
break;
src++;
}
/* skip ending spaces */
while (*endptr == ' ')
{
if (nsrc-- <= 0)
break;
endptr--;
}
while(nsrc > 0)
{
int clen;
if (*src == '\0')
break;
clen = utf8charlen(*src);
if (clen <= ndest && clen <= nsrc)
{
int i;
for (i = 0; i < clen; i++)
{
*dest++ = *src++;
ndest--;
nsrc--;
}
}
else
break;
}
*dest = '\0';
}
/*
* Set color pairs based on style
*/
static void
initialize_color_pairs(int theme)
{
if (theme == 0)
{
use_default_colors();
init_pair(1, -1, -1); /* default */
init_pair(2, COLOR_BLACK, COLOR_WHITE); /* top bar colors */
init_pair(3, COLOR_WHITE, COLOR_BLACK);
init_pair(4, -1, -1); /* fix rows, columns */
init_pair(5, COLOR_BLACK, COLOR_WHITE); /* active cursor over fixed cols */
init_pair(6, COLOR_BLACK, COLOR_WHITE); /* active cursor */
init_pair(8, COLOR_BLACK, COLOR_WHITE); /* expanded header */
}
else if (theme == 1)
{
assume_default_colors(COLOR_WHITE, COLOR_BLUE);
init_pair(1, -1, -1);
init_pair(2, COLOR_BLACK, COLOR_CYAN);
init_pair(3, COLOR_YELLOW, COLOR_WHITE);
init_pair(4, COLOR_YELLOW, COLOR_BLUE);
init_pair(5, COLOR_YELLOW, COLOR_CYAN);
init_pair(6, COLOR_WHITE, COLOR_CYAN);
init_pair(8, COLOR_RED, COLOR_BLUE);
}
else if (theme == 2)
{
assume_default_colors(COLOR_WHITE, COLOR_CYAN);
init_pair(1, -1, -1);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
init_pair(3, COLOR_BLACK, COLOR_WHITE);
init_pair(4, COLOR_WHITE, COLOR_CYAN);
init_pair(5, COLOR_WHITE, COLOR_BLUE);
init_pair(6, COLOR_WHITE, COLOR_BLUE);
init_pair(7, COLOR_YELLOW, COLOR_WHITE);
init_pair(8, COLOR_WHITE, COLOR_BLUE);
}
else if (theme == 3)
{
assume_default_colors(COLOR_BLACK, COLOR_CYAN);
init_pair(1, -1, -1);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
init_pair(3, COLOR_YELLOW, COLOR_WHITE);
init_pair(4, COLOR_WHITE, COLOR_CYAN);
init_pair(5, COLOR_WHITE, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(8, COLOR_WHITE, COLOR_CYAN);
}
else if (theme == 4)
{
assume_default_colors(COLOR_BLACK, COLOR_WHITE);
init_pair(1, -1, -1);
init_pair(2, COLOR_WHITE, COLOR_BLUE);
init_pair(3, COLOR_YELLOW, COLOR_WHITE);
init_pair(4, COLOR_BLACK, COLOR_WHITE);
init_pair(5, COLOR_WHITE, COLOR_BLUE);
init_pair(6, COLOR_WHITE, COLOR_BLUE);
init_pair(8, COLOR_WHITE, COLOR_BLUE);
}
else if (theme == 5)
{
use_default_colors();
init_pair(1, -1, -1);
init_pair(2, COLOR_GREEN, COLOR_BLUE);
init_pair(3, COLOR_YELLOW, COLOR_WHITE);
init_pair(4, COLOR_CYAN, -1);
init_pair(5, COLOR_BLACK, COLOR_CYAN);
init_pair(6, COLOR_BLACK, COLOR_CYAN);
init_pair(8, COLOR_BLACK, COLOR_BLUE);
init_pair(9, COLOR_BLACK, COLOR_CYAN);
}
}
/*
* Read data from file and fill ncurses pad. Increase
* pad when it it necessary
*/
static int
readfile(FILE *fp, DataDesc *desc)
{
char *line = NULL;
size_t len;
ssize_t read;
int nrows = 0;
bool use_stdin = false;
LineBuffer *rows;
LineBuffer *footer;
/* safe reset */
desc->filename[0] = '\0';
if (fp == NULL)
{
use_stdin = true;
fp = stdin;
}
else
{
int fno;
char proclnk[MAXPATHLEN + 1];
char path[MAXPATHLEN + 1];
ssize_t r;
fno = fileno(fp);
sprintf(proclnk, "/proc/self/fd/%d", fno);
r = readlink(proclnk, path, MAXPATHLEN);
if (r > 0)
{
char *name;
path[r] = '\0';
name = basename(path);
strncpy(desc->filename, name, 64);
desc->filename[64] = '\0';
}
}
desc->title[0] = '\0';
desc->title_rows = 0;
desc->border_top_row = -1;
desc->border_head_row = -1;
desc->border_bottom_row = -1;
desc->last_data_row = -1;
desc->is_expanded_mode = false;
desc->maxbytes = -1;
desc->maxx = -1;
memset(&desc->rows, 0, sizeof(LineBuffer));
rows = &desc->rows;
errno = 0;
while (( read = getline(&line, &len, fp)) != -1)
{
int nmaxx, nmaxy;
int clen = utf8len(line);
if (rows->nrows == 1000)
{
LineBuffer *newrows = malloc(sizeof(LineBuffer));
memset(newrows, 0, sizeof(LineBuffer));
rows->next = newrows;
rows = newrows;
}
rows->rows[rows->nrows++] = line;
/* save possible table name */
if (nrows == 0 && !isTopLeftChar(line))
{
strncpytrim(desc->title, line, 63, len);
desc->title_rows = 1;
}
if (desc->border_head_row == -1 && desc->border_top_row == -1 && isTopLeftChar(line))
{
desc->border_top_row = nrows;
desc->is_expanded_mode = is_expanded_header(line, NULL, NULL);
}
else if (desc->border_head_row == -1 && isHeadLeftChar(line))
{
desc->border_head_row = nrows;
if (!desc->is_expanded_mode)
desc->is_expanded_mode = is_expanded_header(line, NULL, NULL);
/* title surelly doesn't it there */
if ((!desc->is_expanded_mode && nrows == 1) ||
(desc->is_expanded_mode && nrows == 0))
{
desc->title[0] = '\0';
desc->title_rows = 0;
}
}
else if (!desc->is_expanded_mode && desc->border_bottom_row == -1 && isBottomLeftChar(line))
{
desc->border_bottom_row = nrows;
desc->last_data_row = nrows - 1;
}
else if (desc->is_expanded_mode && isBottomLeftChar(line))
{
/* Outer border is repeated in expanded mode, use last detected row */
desc->border_bottom_row = nrows;
desc->last_data_row = nrows - 1;
}
if ((int) len > desc->maxbytes)
desc->maxbytes = (int) len;
if ((int) clen > desc->maxx + 1)
desc->maxx = clen - 1;
if ((int) clen > 0)
desc->last_row = nrows;
nrows += 1;
line = NULL;
}
if (errno != 0)
{
endwin();
fprintf(stderr, "cannot to read file: %s\n", strerror(errno));
exit(1);
}
if (!use_stdin)
fclose(fp);
desc->maxy = nrows ;
desc->headline_char_size = 0;
if (desc->border_head_row != -1)
{
int i = 0;
desc->headline = desc->rows.rows[desc->border_head_row];
desc->headline_size = strlen(desc->headline);
if (desc->last_data_row == -1)
desc->last_data_row = desc->last_row - 1;
}
else if (desc->is_expanded_mode && desc->border_top_row != -1)
{
desc->headline = desc->rows.rows[desc->border_top_row];
desc->headline_size = strlen(desc->headline);
}
else
{
desc->headline = NULL;
desc->headline_size = 0;
desc->headline_char_size = 0;
/* there are not a data set */
desc->last_row = nrows;
desc->last_data_row = nrows;
desc->title_rows = 0;
desc->title[0] = '\0';
}
freopen("/dev/tty", "rw", stdin);
return 0;
}
static void
window_fill(WINDOW *win,
int srcy, int srcx, /* offset to displayed data */
int cursor_row, /* row of row cursor */
DataDesc *desc,
attr_t data_attr, /* colors for data (alphanums) */
attr_t line_attr, /* colors for borders */
attr_t expi_attr, /* colors for expanded headers */
attr_t cursor_data_attr, /* colors for cursor on data positions */
attr_t cursor_line_attr, /* colors for cursor on border position */
attr_t cursor_expi_attr) /* colors for cursor on expanded headers */
{
int maxy, maxx;
int row;
LineBuffer *lnb = &desc->rows;
int lnb_row;
attr_t active_attr;
int srcy_bak = srcy;
/* fast leaving */
if (win == NULL)
return;
/* skip first x LineBuffers */
while (srcy > 1000)
{
lnb = lnb->next;
srcy -= 1000;
}
lnb_row = srcy;
row = 0;
getmaxyx(win, maxy, maxx);
while (row < maxy )
{
int bytes;
char *ptr;
char *rowstr;
bool is_cursor_row;
is_cursor_row = row == cursor_row;
if (lnb_row == 1000)
{
lnb = lnb->next;
lnb_row = 0;
}
if (lnb != NULL && lnb_row < lnb->nrows)
rowstr = lnb->rows[lnb_row++];
else
rowstr = NULL;
active_attr = is_cursor_row ? cursor_line_attr : line_attr;
wattron(win, active_attr);
wmove(win, row++, 0);
if (rowstr != NULL)
{
int i;
int effective_row = row + srcy_bak - 1; /* row was incremented before, should be reduced */
bool fix_line_attr_style;
bool is_expand_head;
int ei_min, ei_max;
if (desc->is_expanded_mode)
{
fix_line_attr_style = effective_row >= desc->border_bottom_row;
is_expand_head = is_expanded_header(rowstr, &ei_min, &ei_max);
}
else
{
fix_line_attr_style = effective_row == desc->border_top_row ||
effective_row == desc->border_head_row ||
effective_row >= desc->border_bottom_row;
is_expand_head = false;
}
/* skip first srcx chars */
for (i = 0; i < srcx; i++)
{
if (*rowstr != '\0')
rowstr += utf8charlen(*rowstr);
else
break;
}
ptr = rowstr;
bytes = 0;
/* find length of maxx characters */
if (*ptr != '\0')
{
for (i = 0; i < maxx; i++)
{
if (is_expand_head)
{
int pos = srcx + i;
int new_attr;
if (is_cursor_row)
new_attr = pos >= ei_min && pos <= ei_max ? cursor_expi_attr : cursor_line_attr;
else
new_attr = pos >= ei_min && pos <= ei_max ? expi_attr : line_attr;
if (new_attr != active_attr)
{
if (bytes > 0)
{
waddnstr(win, rowstr, bytes);
rowstr += bytes;
bytes = 0;
}
/* disable current style */
wattroff(win, active_attr);
/* active new style */
active_attr = new_attr;
wattron(win, active_attr);
}
}
else if (!fix_line_attr_style && desc->headline_transl != NULL)
{
int htrpos = srcx + i;
if (htrpos < desc->headline_char_size)
{
int new_attr;
if (is_cursor_row)
new_attr = desc->headline_transl[htrpos] == 'd' ? cursor_data_attr : cursor_line_attr;
else
new_attr = desc->headline_transl[htrpos] == 'd' ? data_attr : line_attr;
if (new_attr != active_attr)
{
if (bytes > 0)
{
waddnstr(win, rowstr, bytes);
rowstr += bytes;
bytes = 0;
}
/* disable current style */
wattroff(win, active_attr);
/* active new style */
active_attr = new_attr;
wattron(win, active_attr);
}
}
}
if (*ptr != '\0')
{
int len = utf8charlen(*ptr);
ptr += len;
bytes += len;
}
else
break;
}
}
else if (is_cursor_row)
/* in this case i is not valid, but it is necessary for cursor line printing */
i = 1;
if (bytes > 0)
waddnstr(win, rowstr, bytes);
/* clean other chars on line */
if (i < maxx)
wclrtoeol(win);
/* draw cursor line to screan end of line */
if (is_cursor_row && i < maxx)
mvwchgat(win, row - 1, i - 1, -1, 0, PAIR_NUMBER(cursor_data_attr), 0);
}
else
{
wclrtobot(win);
break;
}
wattroff(win, active_attr);
}
}
/*
* Prepare dimensions of windows layout