-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtable-pager
executable file
·460 lines (344 loc) · 11.4 KB
/
table-pager
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
#!/usr/bin/perl
use utf8;
use strict;
use Data::Dumper;
use POSIX;
use Term::ANSIColor;
use Time::HiRes qw (sleep);
use Term::ReadKey;
binmode STDOUT, ":utf8";
# Colors see http://misc.flogisoft.com/bash/tip_colors_and_formatting
my @lines = <STDIN>;
my @header = split('\|', shift @lines);
shift @lines; # this is the ---+--+-- line
pop @lines; # 1 blank line at end
pop @lines; # 2 blank line at end
# print Dumper(@header);
# exit;
# print @lines;
# print "EOFEOF";
# exit;
my $state;
$state->{'x'} = 0;
$state->{'y'} = 0;
$state->{'head'} = \@header;
$state->{'lines'} = @lines;
$state->{'draw'} = 0;
$state->{'lockcol'} = 1;
# print Dumper($state);
# exit;
sub draw {
my ($st) = @_;
my $f = '┌─┬╥┐ '.
'│ │║│ '.
'├─┼╫┤ '.
'└─┴╨┘ ';
my $colc = "\e[34m"; # blue fg - color column
my $cold = "\e[39m"; # default fg - color header
my $colh = "\e[32m"; # green fg - color data
my $coln = "\e[90m"; # yellow fg - color row num
# Calc the viewport size
my @size = Term::ReadKey::GetTerminalSize STDOUT;
$st->{maxx} = $size[0];
$st->{maxy} = $size[1];
my $height = $st->{maxy} - 1;
$st->{h} = $height;
my $y1 = $st->{y};
my $y2 = $y1 + $height - 3; # 4 lines of chrome to take off
my $width = $st->{maxx};
$st->{w} = $width;
my $x1 = $st->{x};
my $residue = 0;
if ($#lines <= $height - 3) {
$y1 = 0;
$y2 = $#lines + 1;
$residue = $height - $y2 -3;
}
# Reset the window, don't do it the first time
if ($st->{draw}++ > 0){
print "\e[H"; # reset
# print "\e[J"; # clear
}
my @head = @{ $st->{head} };
# Draw a sticky file header
my $remainder = $width; # How many visible chars left on the screen?
# print "Rem $remainder orig\n";
# How many rows do we have? So how wide is that number in chars?
my $nw = length(($#lines+1) . '');
# Line 1,2,3 which make up the header
my $l1 = $colc
. substr($f,1,1) x $nw
. substr($f,2,1)
. $colc;
my $l2 = $coln
. ' ' x ($nw-1) . '#'
. $colc
. substr($f,12,1);
my $l3 = substr($f,1,1) x $nw
. substr($f,22,1);
$remainder -= ($nw + 1); # width of number col
# print "Rem $remainder num\n";
my $col = 0;
my $w;
my $head;
# Draw the locked header cols
for(; $col<$st->{lockcol}; $col++){
$head = $head[$col];
chomp $head;
$w = length($head);
$l1 .= substr($f, 1,1) x $w;
$l1 .= substr($f, 3,1);
$l2 .= $colh;
$l2 .= sprintf("%-".$w."s", $head);
$l2 .= $colc;
$l2 .= substr($f,13,1);
$l3 .= substr($f,21,1) x $w;
$l3 .= substr($f,23,1);
$remainder -= ($w + 1); # width of number col
}
# print "Rem $remainder lock\n";
$remainder++;
my $colsleft = $#head - $col + 1;
$remainder -= $colsleft;
# print "Rem $remainder removed $colsleft\n";
my $processed = 0; # how many chars have we processed?
# Draw the scrolling header cols
for(; $col<=$#head; $col++){
$head = $head[$col];
chomp $head;
$w = length($head) + 1; # add 1 for the border
$head =~ s/^\s+/ /g;
my $offset = 0; # how much of this header will we show?
# There are 5 scenarios:
# 1) cropped from the left completetly
# printf "%-2d %-3d %-2d %-2d %s ", $col, $processed, $x1, $w, $remainder, $head;
if ($x1 >= $processed + $w){
$processed += $w;
$remainder++;
$w = 0;
# 2) cropped from the left partially
} elsif ($x1 > $processed ) {
$offset = $x1 - $processed;
$processed += $w;
$w -= $offset;
$remainder -= $w - 1;
# print "left $remainder\n";
# what if the screen is too small for a single field?
# crop on right too
# 3) squished
} elsif ($remainder <= 0) {
$processed += $w;
$w = 1;
$remainder = 0;
# print "drop $remainder\n";
# 4) cropping the right side
} elsif ($w > $remainder) {
$processed += $w;
$w = $remainder;
$remainder -= $w - 1;
# print "right $remainder\n";
# 4) showing the lot
} else {
$processed += $w;
$remainder -= $w - 1;
# print "middle $remainder\n";
}
if ($w > 0){
$l1 .= substr($f,1,1) x ($w-1);
$l2 .= $colh;
$l2 .= sprintf("%-".($w-1)."s", substr($head,$offset, $w-1) );
$l2 .= $colc;
$l3 .= substr($f,21,1) x ($w-1);
# right border
if ($col<$#head) {
$l1 .= substr($f,2,1);
$l2 .= substr($f,12,1);
$l3 .= substr($f,22,1);
} else {
$l1 .= substr($f, 4,1);
$l2 .= substr($f,14,1);
$l3 .= substr($f,24,1);
}
}
}
# Blank space after table
$l1 .= ' ' x ($remainder-1);
$l2 .= ' ' x ($remainder-1);
$l3 .= ' ' x ($remainder-1);
print "$l1\n$l2\n$l3\n";
my $file = '';
my $lnum = -1;
my $text = '';
my $mode = '';
for (my $y = $y1; $y < $y2; $y++){
my $line = $lines[$y];
chomp $line;
$col = 0;
my @parts = split('\|', $line);
my $remainder = $width; # How many visible chars left on the screen?
# print row number
print $coln;
printf "%".$nw."s", $y+1;
print $colc;
print substr($f,12,1);
$remainder -= $nw+1;
# Draw the locked header cols
for(; $col<$st->{lockcol}; $col++){
$head = $head[$col];
chomp $head;
$w = length($head);
print $cold;
print sprintf("%-".$w."s", $parts[$col]);
print $colc;
print substr($f,13,1);
$remainder -= ($w+1);
}
# print " $remainder ";
my $processed = 0; # how many chars have we processed?
# Draw the scrolling header cols
for(; $col<=$#head; $col++){
$head = $head[$col];
chomp $head;
$w = length($head) + 1; # add 1 for the border
$head =~ s/^\s+/ /g;
my $offset = 0; # how much of this header will we show?
my $l;
# There are 5 scenarios:
# 1) cropped from the left completetly
# printf "%-2d %-3d %-2d %-2d %s ", $col, $processed, $x1, $w, $remainder, $head;
if ($x1 >= $processed + $w){
$processed += $w;
$w = 0;
# 2) cropped from the left partially
} elsif ($x1 > $processed ) {
$offset = $x1 - $processed;
$processed += $w;
$w -= $offset;
$remainder -= $w;
# print "left $remainder\n";
# what if the screen is too small for a single field?
# crop on right too
# 3) squished
} elsif ($remainder <= 0) {
$processed += $w;
$w = 1;
$remainder = 0;
last;
# print "drop $remainder\n";
# 4) cropping the right side
} elsif ($w > $remainder) {
$processed += $w;
$w = $remainder;
$remainder -= $w;
# print "right $remainder\n";
# 4) showing the lot
} else {
$processed += $w;
$remainder -= $w;
# print "middle $remainder\n";
}
if ($w > 0){
$l .= $cold;
$l .= sprintf("%-".($w-1)."s", substr($parts[$col],$offset, $w-1) );
$l .= $colc;
# right border
if ($col<$#head) {
$l .= substr($f,12,1);
} else {
$l .= substr($f,14,1);
}
}
print $l;
}
print ' ' x ($remainder);
print "\n";
}
# Print lots of white space to fill out the screen
# TODO but only is it's a redraw
my $clear = ' ' x $width . "\n";
print $clear x $residue;
# If the number of rows is more than the screen height then
# Draw a position and status bar
# print Dumper($st);
print "\e[39m"; # default fg
print "\e[7m"; # invert
print " Rows ".($y1+1)."-$y2 / ". ($#lines+1). ' ';
if ($#lines > 0) {
printf "%d% ", 100 * ($y2 / ($#lines+1)); # per %
}
print " $x1 ";
print "\e[27m"; # reset invert
print "\e[?25l "; # no blinking cursor
}
draw($state);
open my $TTY, '<', '/dev/tty';
ReadMode('cbreak', $TTY);
$SIG{INT} = sub {
ReadMode('normal', $TTY);
print "\n";
print "\e[?25h "; # reset blinking cursor
exit;
};
my $buffer = '';
while (1) {
my $char = ReadKey(-1, $TTY);
if (!defined $char){
if ($buffer){
my $per;
my $lh = $#lines - $state->{h} + 4; # lines of chrome
if ( $buffer == '27' # esc
|| $buffer == '113') { # q
last;
} elsif($buffer == '10') { # up
$state->{selected} = 1;
last;
} elsif($buffer == '279165') { # up
$state->{'y'}--;
# $state->{'cursor'}--;
} elsif($buffer == '279166') { # down
$state->{'y'}++;
# $state->{'cursor'}++;
} elsif($buffer == '279149595068') { # shift left 1
$state->{'x'}-= 1;
} elsif($buffer == '279149595067') { # shift right 1
$state->{'x'}+= 1;
} elsif($buffer == '279168') { # left
$state->{'x'}-= floor($state->{maxx} * .1);
} elsif($buffer == '279167') { # right
$state->{'x'}+= floor($state->{maxx} * .1);
} elsif($buffer == '279153126') { # page up
$state->{'y'}-= $state->{h};
} elsif($buffer == '279154126' # page down
|| $buffer == '32') { # space
$state->{'y'}+= $state->{h};
} elsif ($buffer >= '48' and $buffer <= '57'){ # 1 = 10%, 9 = 90%
$per = $buffer*1 - 48;
if ($per == 0){ $per = 10; }
$per--;
$state->{'y'} = floor($per * $lh / 9);
}
# Don't go out of bounds
if ($state->{x} < 0 ){ $state->{x} = 0; }
if ($state->{y} < 0 ){ $state->{y} = 0; }
if ($state->{y} > $lh){ $state->{y} = $lh; }
draw($state);
print "buffer: '$buffer' $per";
}
$buffer = '';
sleep (0.005);
next;
}
$buffer .= ord($char);
}
print "\n";
ReadMode('normal', $TTY);
print "\e[?25h"; # reset blinking cursor
print "\e[21m"; # not bold
print "\e[39m"; # default fg
print "\e[49m"; # default bg
close $TTY;
# TODO
# If less lines than terminal height don't enter interactive mode
# If more lines than say 1000 then stop reading sdtin, flag that there is more, only keep reading if we scroll down that far
# show clean error when no results
# If new lines in data then do something sane