-
Notifications
You must be signed in to change notification settings - Fork 87
/
pspg.c
7047 lines (5731 loc) · 165 KB
/
pspg.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
/*-------------------------------------------------------------------------
*
* pspg.c
* a terminal pager designed for usage from psql
*
* Portions Copyright (c) 2017-2024 Pavel Stehule
*
* IDENTIFICATION
* src/pspg.c
*
*-------------------------------------------------------------------------
*/
#define PDC_NCMOUSE
#if defined HAVE_NCURSESW_CURSES_H
#include <ncursesw/curses.h>
#elif defined HAVE_NCURSESW_H
#include <ncursesw.h>
#elif defined HAVE_NCURSES_CURSES_H
#include <ncurses/curses.h>
#elif defined HAVE_NCURSES_H
#include <ncurses.h>
#elif defined HAVE_CURSES_H
#include <curses.h>
#else
/* fallback */
#include <ncurses/ncurses.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <langinfo.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifndef GWINSZ_IN_SYS_IOCTL
#include <termios.h>
#endif
#ifdef PDCURSES
#include <term.h>
#endif
#include <time.h>
#include <unistd.h>
#include "commands.h"
#include "config.h"
#include "inputs.h"
#include "pspg.h"
#include "themes.h"
#include "unicode.h"
#ifdef COMPILE_MENU
#include "st_menu.h"
#endif
#ifdef DEBUG_PIPE
/* used for mallinfo */
#include <malloc.h>
#endif
#ifdef PDCURSES
#define BUTTON_CTRL BUTTON_MODIFIER_CONTROL
#endif
#ifndef NCURSES_CONST
#define NCURSES_CONST const
#endif
static char last_row_search[256];
static char last_col_search[256];
static char last_line[256];
static char last_path[1025];
static char last_rows_number[256];
static char last_table_name[256];
static char last_nullstr[256];
static char cmdline[1024];
static const char *cmdline_ptr = 0;
int clipboard_application_id = 0;
#define USE_EXTENDED_NAMES
#ifdef DEBUG_PIPE
FILE *debug_pipe = NULL;
int debug_eventno = 0;
static void print_memory_stats(bool enable_memory_debug);
#endif
static long last_watch_ms = 0;
static time_t last_watch_sec = 0; /* time when we did last refresh */
static bool paused = false; /* true, when watch mode is paused */
static bool active_ncurses = false;
static bool xterm_mouse_mode_was_initialized = false;
static int number_width(int num);
static void set_scrollbar(ScrDesc *scrdesc, DataDesc *desc, int first_row);
static bool check_visible_vertical_cursor(DataDesc *desc, Options *opts, int vertical_cursor_column);
static void print_status(Options *opts, ScrDesc *scrdesc, DataDesc *desc);
StateData *current_state = NULL;
static struct sigaction old_sigsegv_handler;
/*
* Global error buffer - used for building and storing a error messages
*/
char pspg_errstr_buffer[PSPG_ERRSTR_BUFFER_SIZE];
/*
* Global setting
*/
bool use_utf8;
bool quiet_mode = false;
/*
* Global state variables
*/
bool handle_sigint = false;
bool handle_sigwinch = false;
WINDOW *prompt_window = NULL;
attr_t prompt_window_input_attr = 0;
attr_t prompt_window_error_attr = 0;
attr_t prompt_window_info_attr = 0;
typedef enum
{
MARK_MODE_NONE,
MARK_MODE_ROWS, /* activated by F3 */
MARK_MODE_BLOCK, /* activated by F15 ~ Shift F3 */
MARK_MODE_CURSOR, /* activated by SHIFT + CURSOR */
MARK_MODE_MOUSE, /* activated by CTRL + MOUSE */
MARK_MODE_MOUSE_COLUMNS,/* activated by CTRL + MOUSE on column headers */
MARK_MODE_MOUSE_BLOCK, /* activated by ALT + MOUSE */
} MarkModeType;
/*
* Static variables
*/
static int vertical_cursor_column = -1; /* table columns are counted from one */
static int cursor_col = 0;
static int cursor_row = 0;
static int footer_cursor_col = 0;
static int first_row = 0;
static int first_data_row;
static int mouse_row = -1;
static int mouse_col = -1;
static int default_freezed_cols = 1;
static int fixedRows = -1; /* detect automatically (not yet implemented option) */
static int fix_rows_offset = 0;
static int fix_hide_header_line = 0;
static MarkModeType mark_mode = MARK_MODE_NONE;
static int mark_mode_start_row = 0;
static int mark_mode_start_col = 0;
static bool recheck_vertical_cursor_visibility = false;
#ifdef COMPILE_MENU
static bool menu_is_active = false;
static struct ST_MENU *menu = NULL;
static struct ST_CMDBAR *cmdbar = NULL;
#endif
#ifdef DEBUG_PIPE
static time_t start_app_sec;
static long start_app_ms;
#endif
typedef void (*__sigt)(int);
/*
* Own signal handlers
*/
static void
SigintHandler(int sig_num)
{
UNUSED(sig_num);
signal(SIGINT, SigintHandler);
handle_sigint = true;
}
#ifndef PDCURSES
static void
SigwinchHandler(int sig_num)
{
UNUSED(sig_num);
signal(SIGWINCH, SigwinchHandler);
handle_sigwinch = true;
}
#endif
static void
SigtermHandler(int sig_num)
{
UNUSED(sig_num);
signal(SIGTERM, SigtermHandler);
/* force own exit_handler routine */
exit(EXIT_FAILURE);
}
/* Custom SIGSEGV handler. */
void
SigsegvHandler (int sig)
{
exit_handler();
log_row("pspg crashed by Sig %d\n", sig);
fprintf(stderr, "pspg crashed by signal %d\n", sig);
if (logfile)
{
fclose(logfile);
logfile = NULL;
}
/* Call old sigsegv handler; may be default exit or third party one (e.g. ASAN) */
sigaction (SIGSEGV, &old_sigsegv_handler, NULL);
}
/* Set up sigsegv handler. */
static void
setup_sigsegv_handler(void)
{
struct sigaction act;
sigemptyset (&act.sa_mask);
act.sa_flags = (int) SA_RESETHAND;
act.sa_handler = SigsegvHandler;
sigaction(SIGSEGV, &act, &old_sigsegv_handler);
}
inline int
min_int(int a, int b)
{
return a < b ? a : b;
}
inline int
max_int(int a, int b)
{
return a > b ? a : b;
}
/*
* The argument "b" should be >= "a" or it should be ignored
* and "a" is used instead "b".
*/
int
trim_to_range(int v, int a, int b)
{
/*
* if second parameter is below a, result should be a,
* because the interval is [a, a].
*/
if (b <= a)
return a;
if (v <= a)
return a;
if (v > b)
return b;
return v;
}
void
current_time(time_t *sec, long *ms)
{
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
*ms = roundl(spec.tv_nsec / 1.0e6);
*sec = spec.tv_sec;
}
inline void
enable_xterm_mouse_mode(bool enable_mode)
{
if (enable_mode)
{
fprintf(stdout, "\033[?1002h");
fflush(stdout);
xterm_mouse_mode_was_initialized = true;
log_row("xterm mouse mode 1002 activated");
}
else
{
xterm_mouse_mode_was_initialized = false;
log_row("xterm mouse mode 1002 is not activated");
}
}
inline bool
disable_xterm_mouse_mode(void)
{
if (xterm_mouse_mode_was_initialized)
{
fprintf(stdout, "\033[?1002l");
fflush(stdout);
xterm_mouse_mode_was_initialized = false;
log_row("xterm mouse mode 1002 is deactivated");
return true;
}
else
{
log_row("xterm mouse mode 1002 is not active");
return false;
}
}
#ifdef DEBUG_PIPE
static void
print_duration(time_t start_sec, long start_ms, const char *label)
{
time_t end_sec;
long end_ms;
current_time(&end_sec, &end_ms);
fprintf(debug_pipe, "duration of \"%s\" is %ld ms\n",
label,
time_diff(end_sec, end_ms, start_sec, start_ms));
}
#endif
/*
* Multiple used block - searching in string based on configuration
*/
const char *
pspg_search(Options *opts, ScrDesc *scrdesc, const char *str)
{
bool ignore_case = opts->ignore_case;
bool ignore_lower_case = opts->ignore_lower_case;
bool has_upperchr = scrdesc->has_upperchr;
const char *searchterm = scrdesc->searchterm;
const char *result;
if (ignore_case || (ignore_lower_case && !has_upperchr))
{
result = use_utf8 ? utf8_nstrstr(str, searchterm) : nstrstr(str, searchterm);
}
else if (ignore_lower_case && has_upperchr)
{
result = use_utf8 ?
utf8_nstrstr_ignore_lower_case(str, searchterm) :
nstrstr_ignore_lower_case(str, searchterm);
}
else
result = strstr(str, searchterm);
return result;
}
/*
* Trim footer rows - We should to trim footer rows and calculate footer_char_size
*/
static void
trim_footer_rows(DataDesc *desc)
{
if (desc->headline_transl != NULL && desc->footer_row != -1)
{
LineBufferIter lbi;
char *line;
desc->footer_char_size = 0;
init_lbi_ddesc(&lbi, desc, desc->footer_row);
while(lbi_get_line_next(&lbi, &line, NULL, NULL))
{
char *ptr = line;
char *last_nspc = NULL;
int len;
/* search last non space char */
while (*ptr)
{
if (*ptr != ' ')
last_nspc = ptr;
ptr += 1;
}
if (last_nspc)
*(last_nspc + 1) = '\0';
else
*line = '\0';
len = use_utf8 ? utf8len(line) : (int) strlen(line);
if (len > desc->footer_char_size)
desc->footer_char_size = len;
}
}
else
desc->footer_char_size = desc->maxx;
}
static void
set_scrollbar_dimensions(Options *opts, DataDesc *desc, ScrDesc *scrdesc)
{
if (opts->show_scrollbar)
{
int saved_slider_min_y = 0;
/*
* The relayout can be invoked in scrollbar mode too (when we try
* to change visibility of footer window (data)). Unfortunatelly,
* inside layout initialization we init slider position to. In
* this case we want to preserve an slider position.
*/
if (scrdesc->scrollbar_mode)
saved_slider_min_y = scrdesc->slider_min_y;
/* show scrollbar only when display is less than data */
if (scrdesc->main_maxy < desc->last_row + 1)
{
Theme *t;
int num_width;
/*
* This calculation can be processed repeatedly, so we need to
* calculate main_maxx absolutely.
*/
num_width = opts->show_rownum ? number_width(desc->maxy) + 2 : 0;
scrdesc->main_maxx = scrdesc->maxx - num_width - 1;
t = &scrdesc->themes[WINDOW_VSCROLLBAR];
scrdesc->scrollbar_x = scrdesc->maxx - 1;
scrdesc->scrollbar_start_y = scrdesc->main_start_y;
scrdesc->scrollbar_maxy = scrdesc->main_maxy;
if (t->scrollbar_slider_symbol)
scrdesc->slider_size = 1;
else if (scrdesc->scrollbar_maxy > 4)
{
scrdesc->slider_size
= round(((double) scrdesc->main_maxy - scrdesc->fix_rows_rows) *
((double) scrdesc->main_maxy - 2) /
((double) desc->last_row - desc->fixed_rows + 1));
if (scrdesc->slider_size < 2)
scrdesc->slider_size = 2;
}
else
scrdesc->slider_size = 1;
scrdesc->slider_min_y = 1;
}
else
scrdesc->scrollbar_x = -1;
/* restore saved slider position */
if (scrdesc->scrollbar_mode)
scrdesc->slider_min_y = saved_slider_min_y;
}
else
scrdesc->scrollbar_x = -1;
}
/*
* Prepare dimensions of windows layout
*/
static void
create_layout_dimensions(Options *opts, ScrDesc *scrdesc, DataDesc *desc,
int fixCols, int fixRows,
int maxy, int maxx)
{
scrdesc->maxy = maxy;
scrdesc->maxx = maxx;
if (opts->show_rownum)
{
int startx = number_width(desc->maxy) + 2;
scrdesc->main_start_x = startx;
scrdesc->main_maxx -= startx;
}
fix_hide_header_line = 0;
scrdesc->fix_cols_cols = 0;
/* search end of fixCol'th column */
if (desc->headline_transl != NULL && fixCols > 0)
{
char *c = desc->headline_transl;
while (*c != 0)
{
if (*c == 'I' && --fixCols == 0)
{
scrdesc->fix_cols_cols = c - desc->headline_transl + 1;
break;
}
c += 1;
}
}
scrdesc->fix_rows_rows = 0;
scrdesc->footer_rows = 0;
if (fixRows != -1)
{
scrdesc->fix_rows_rows = fixRows;
}
else if (!desc->is_expanded_mode && desc->border_head_row != -1 &&
desc->headline_transl != NULL)
{
scrdesc->fix_rows_rows = desc->border_head_row + 1 - desc->title_rows;
if (opts->hide_header_line && scrdesc->fix_rows_rows > 1)
{
scrdesc->fix_rows_rows -= 1;
fix_hide_header_line = 1;
}
}
/* disable fixed parts when is not possible draw in screen */
if (scrdesc->fix_cols_cols > scrdesc->main_maxx)
scrdesc->fix_cols_cols = 0;
if (scrdesc->fix_rows_rows > scrdesc->main_maxy)
scrdesc->fix_rows_rows = 0;
if (scrdesc->fix_rows_rows == 0 && !desc->is_expanded_mode)
{
desc->title_rows = 0;
desc->title[0] = '\0';
}
desc->fixed_rows = scrdesc->fix_rows_rows;
set_scrollbar_dimensions(opts, desc, scrdesc);
}
static void
create_layout(Options *opts,
ScrDesc *scrdesc,
DataDesc *desc,
int _first_data_row)
{
int i;
for (i = 0; i < PSPG_WINDOW_COUNT; i++)
{
if (i != WINDOW_TOP_BAR &&
i != WINDOW_BOTTOM_BAR)
{
if (scrdesc->wins[i])
{
delwin(scrdesc->wins[i]);
scrdesc->wins[i] = NULL;
}
}
}
if (desc->headline_transl != NULL && desc->footer_row > 0)
{
int rows_rows = desc->footer_row - first_row - _first_data_row;
int data_rows;
/* desc->footer_row == desc->first_data_row when result is empty */
if (rows_rows > 0 || desc->footer_row == desc->first_data_row)
{
data_rows = scrdesc->main_maxy - desc->fixed_rows;
scrdesc->rows_rows = min_int(rows_rows, data_rows);
}
else
{
int new_fix_rows_rows = scrdesc->fix_rows_rows + rows_rows - 1;
scrdesc->fix_rows_rows = new_fix_rows_rows > 0 ? new_fix_rows_rows : 0;
scrdesc->rows_rows = new_fix_rows_rows >= 0 ? 1 : 0;
data_rows = scrdesc->main_maxy - scrdesc->fix_rows_rows;
}
/*
* Footer window fill all remaining space.
*/
scrdesc->footer_rows = data_rows - scrdesc->rows_rows;
if (scrdesc->footer_rows > 0)
{
w_footer(scrdesc) = subwin(stdscr,
scrdesc->footer_rows,
scrdesc->main_maxx,
scrdesc->main_start_y + scrdesc->fix_rows_rows + scrdesc->rows_rows,
scrdesc->main_start_x);
}
}
else if (desc->headline_transl != NULL)
{
scrdesc->rows_rows = scrdesc->main_maxy - scrdesc->fix_rows_rows;
}
else
{
scrdesc->rows_rows = 0;
scrdesc->fix_rows_rows = 0;
scrdesc->footer_rows = min_int(scrdesc->main_maxy, desc->last_row + 1);
w_footer(scrdesc) = subwin(stdscr,
scrdesc->footer_rows,
scrdesc->main_maxx,
scrdesc->main_start_y,
scrdesc->main_start_x);
}
if (scrdesc->fix_rows_rows > 0)
{
w_fix_rows(scrdesc) = subwin(stdscr,
scrdesc->fix_rows_rows,
scrdesc->main_maxx - scrdesc->fix_cols_cols,
scrdesc->main_start_y,
scrdesc->fix_cols_cols + scrdesc->main_start_x);
}
if (scrdesc->fix_cols_cols > 0 && scrdesc->rows_rows > 0)
{
w_fix_cols(scrdesc) = subwin(stdscr,
scrdesc->rows_rows,
scrdesc->fix_cols_cols,
scrdesc->fix_rows_rows + scrdesc->main_start_y,
scrdesc->main_start_x);
}
if (scrdesc->fix_rows_rows > 0 && scrdesc->fix_cols_cols > 0)
{
w_luc(scrdesc) = subwin(stdscr,
scrdesc->fix_rows_rows,
scrdesc->fix_cols_cols,
scrdesc->main_start_y,
scrdesc->main_start_x);
}
if (scrdesc->rows_rows > 0)
{
w_rows(scrdesc) = subwin(stdscr, scrdesc->rows_rows,
scrdesc->main_maxx - scrdesc->fix_cols_cols,
scrdesc->fix_rows_rows + scrdesc->main_start_y,
scrdesc->fix_cols_cols + scrdesc->main_start_x);
}
if (scrdesc->fix_rows_rows > 0 && opts->show_rownum)
{
Theme *theme = &scrdesc->themes[WINDOW_ROWNUM_LUC];
w_rownum_luc(scrdesc) = subwin(stdscr,
scrdesc->fix_rows_rows,
scrdesc->main_start_x,
scrdesc->main_start_y,
0);
wbkgd(w_rownum_luc(scrdesc), theme->data_attr);
}
if (scrdesc->rows_rows + scrdesc->footer_rows > 0 && opts->show_rownum)
{
w_rownum(scrdesc) = subwin(stdscr,
scrdesc->rows_rows + scrdesc->footer_rows,
scrdesc->main_start_x,
scrdesc->fix_rows_rows + scrdesc->main_start_y,
0);
}
if (opts->show_scrollbar)
{
w_vscrollbar(scrdesc) = subwin(stdscr,
scrdesc->scrollbar_maxy,
1,
scrdesc->scrollbar_start_y,
scrdesc->scrollbar_x);
}
}
/*
* Refresh aux windows like top bar or bottom bar.
*/
static void
refresh_aux_windows(Options *opts, ScrDesc *scrdesc)
{
int maxy, maxx;
WINDOW *top_bar = w_top_bar(scrdesc);
WINDOW *bottom_bar = w_bottom_bar(scrdesc);
getmaxyx(stdscr, maxy, maxx);
if (top_bar != NULL)
{
delwin(top_bar);
top_bar = NULL;
w_top_bar(scrdesc) = NULL;
}
if (opts->menu_always)
scrdesc->top_bar_rows = 1;
else if (opts->no_topbar)
scrdesc->top_bar_rows = 0;
else
{
scrdesc->top_bar_rows = 1;
#ifdef PDCURSES
/*
* bug in pdcurses, width should be exactly specified
*/
top_bar = subwin(stdscr, 1, maxx, 0, 0);
#else
top_bar = subwin(stdscr, 1, 0, 0, 0);
#endif
wbkgd(top_bar, ncurses_theme_attr(PspgTheme_status_bar));
wnoutrefresh(top_bar);
w_top_bar(scrdesc) = top_bar;
}
if (bottom_bar != NULL)
{
delwin(bottom_bar);
bottom_bar = NULL;
w_bottom_bar(scrdesc) = NULL;
}
/*
* bottom_bar should be created every time - it can be used
* for an alerts, but sometimes, it should not be visible.
*/
bottom_bar = subwin(stdscr, 1, 0, maxy - 1, 0);
w_bottom_bar(scrdesc) = bottom_bar;
/* When it is visible, clean content and set background */
if (opts->less_status_bar
#ifdef COMPILE_MENU
|| !opts->no_commandbar
#endif
)
{
werase(bottom_bar);
/* data colours are better than default */
wbkgd(bottom_bar, ncurses_theme_attr(PspgTheme_data));
wnoutrefresh(bottom_bar);
}
scrdesc->main_maxy = maxy;
scrdesc->main_maxx = maxx;
scrdesc->main_start_y = 0;
scrdesc->main_start_x = 0;
if (scrdesc->top_bar_rows > 0)
{
scrdesc->main_maxy -= scrdesc->top_bar_rows;
scrdesc->main_start_y = scrdesc->top_bar_rows;
}
if (opts->less_status_bar
#ifdef COMPILE_MENU
|| !opts->no_commandbar
#endif
)
scrdesc->main_maxy -= 1;
/*
* Store ref to bottom bar to global variable
*/
prompt_window = bottom_bar;
/*
* Readline requires disabled keypad
*/
keypad(prompt_window, FALSE);
/*
* This looks like obscure code, but it is necessary. The key processing
* is terminal feature, and ncurses switches the terminal mode by commands
* smkx and rmkx. keypad function sends these codes. The wgetch function
* sendes these codes too in dependency on window configuration. But because
* pspg uses own instance of poll function, then wgetch function is called
* after of moment when terminal generates input sequence related to keyboard
* event. So because last sent command to terminal was rmkx (related to
* keypad(win, FALSE), its is need to activate keypad on terminal again, so
* first keypad event will be processed with activated keypad.
*/
keypad(stdscr, TRUE);
/*
* Used for detection of terminal resize in edit time. The terminal
* resize interrupts editing. I have not possibility how to force
* refresh layout from get_string routine. Canceling editing is most
* simply and good enough solution.
*/
wtimeout(prompt_window, 10000);
}
/*
* Refresh ncurses internal metric when terminal was resized
*/
void
refresh_terminal_size(void)
{
int maxy, maxx;
#ifndef PDCURSES
struct winsize size;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *) &size) >= 0)
{
resize_term(size.ws_row, size.ws_col);
log_row("new terminal size %d %d", size.ws_row, size.ws_col);
}
#endif
getmaxyx(stdscr, maxy, maxx);
log_row("info: stdscr - maxy: %d, maxx: %d", maxy, maxx);
current_state->scrdesc->maxy = maxy;
current_state->scrdesc->maxx = maxx;
}
void
refresh_layout_after_terminal_resize(void)
{
ScrDesc *scrdesc;
DataDesc *desc;
Options *opts;
int maxy, maxx;
scrdesc = current_state->scrdesc;
desc = current_state->desc;
opts = current_state->opts;
getmaxyx(stdscr, maxy, maxx);
refresh_aux_windows(opts, scrdesc);
create_layout_dimensions(opts, scrdesc, desc, opts->freezed_cols != -1 ? opts->freezed_cols : default_freezed_cols, fixedRows, maxy, maxx);
create_layout(opts, scrdesc, desc, first_data_row);
/* recheck visibility of vertical cursor. now we have fresh fix_cols_cols data */
if (recheck_vertical_cursor_visibility && vertical_cursor_column > 0)
{
int vminx = desc->cranges[vertical_cursor_column - 1].xmin;
int left_border = scrdesc->fix_cols_cols + cursor_col - 1;
if (vminx < left_border)
cursor_col = vminx - scrdesc->fix_cols_cols + 1;
}
#ifdef COMPILE_MENU
if (cmdbar)
cmdbar = init_cmdbar(cmdbar, opts);
#endif
set_scrollbar(scrdesc, desc, first_row);
}
/*
* This rotine is separated from event loop, because it should be
* called from edit string routine (when terminal is resized). It
* uses lot of global variables. These variables are state variables,
* and it is more simply use as global, then passing to state var or
* somwhere else.
*/
void
redraw_screen(void)
{
ScrDesc *scrdesc;
DataDesc *desc;
Options *opts;
int vcursor_xmin_fix = -1;
int vcursor_xmax_fix = -1;
int vcursor_xmin_data = -1;
int vcursor_xmax_data = -1;
int selected_xmin = INT_MIN;
int selected_xmax = INT_MIN;
int i;
time_t current_sec;
long current_ms;
static time_t last_doupdate_sec = -1;
static long last_doupdate_ms = -1;
#ifdef DEBUG_PIPE
time_t start_doupdate_sec;
long start_doupdate_ms;
time_t start_draw_sec;
long start_draw_ms;
static bool first_doupdate = true;
#endif
scrdesc = current_state->scrdesc;
desc = current_state->desc;
opts = current_state->opts;
if (opts->vertical_cursor && desc->columns > 0 && vertical_cursor_column > 0)
{
int vcursor_xmin = desc->cranges[vertical_cursor_column - 1].xmin;
int vcursor_xmax = desc->cranges[vertical_cursor_column - 1].xmax;
if (vcursor_xmin < scrdesc->fix_cols_cols)
{
vcursor_xmin_fix = vcursor_xmin;
vcursor_xmin_data = vcursor_xmin - scrdesc->fix_cols_cols;
}
else
{
vcursor_xmin_fix = vcursor_xmin - cursor_col;
vcursor_xmin_data = vcursor_xmin - scrdesc->fix_cols_cols - cursor_col;
}
if (vcursor_xmax < scrdesc->fix_cols_cols)
{
vcursor_xmax_fix = vcursor_xmax;
vcursor_xmax_data = vcursor_xmax - scrdesc->fix_cols_cols;
}
else
{
vcursor_xmax_fix = vcursor_xmax - cursor_col;
vcursor_xmax_data = vcursor_xmax - scrdesc->fix_cols_cols - cursor_col;
}
/*
* When vertical cursor is not in freezed columns, then it cannot to
* overwrite fixed col cols. Only last char position can be shared.
*/
if (vertical_cursor_column > (opts->freezed_cols > -1 ? opts->freezed_cols : default_freezed_cols))