-
Notifications
You must be signed in to change notification settings - Fork 2
/
mypager.pl
executable file
·244 lines (187 loc) · 6.31 KB
/
mypager.pl
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
#!/usr/bin/env perl
package main;
use strict;
use warnings;
require 5.008_000;
use Module::Load;
use POSIX ":sys_wait_h";
use Term::ANSIColor qw/:constants/;
my $reset = RESET;
# Different styles for different types
my $style_int = GREEN;
my $style_null = CYAN;
my $style_date = YELLOW;
# Column header in the \G style
# TODO column headers too
my $style_header = UNDERLINE;
# Row headers in the \G style
my $style_row = MAGENTA;
# Styles can be combined too
# my $style_date = YELLOW . ON_BLUE;
# Try to determine the screen size from module or stty
my ($term_cols, $term_lines) = (0, 0);
eval {
load Term::ReadKey;
($term_cols, $term_lines) = Term::ReadKey::GetTerminalSize();
} or eval {
($term_lines, $term_cols) = split /\s+/, `stty -F /dev/stderr size`;
};
# Config stuff
my %CONF;
{
no warnings qw/prototype/;
%CONF = %{ MyPager::Config::get_config() || {} };
}
$ENV{LESS} ||= "";
$CONF{"less-options"} ||= "";
if ( $CONF{"less-options-overrides-env"} ) {
$ENV{LESS} = $ENV{LESS} . $CONF{"less-options"};
} else {
$ENV{LESS} = $CONF{"less-options"} . $ENV{LESS};
}
# Global print "buffer" scalar and filehandle
# Used to store data before sending it to `less` or stdout
my $outhandle;
my $outstring = "";
open($outhandle, "+>", \$outstring)
or die("Can't create temporary buffer");
select($outhandle);
END {
# If less was used, then outstring will be empty
print STDOUT $outstring if $outstring;
}
my $input_format = ""; # unknown by default;
# First line with +---+-----+ or ******
my $header = <>;
if ( $header =~ /^\+(?:-+\+)+$/ ) {
$input_format = "std";
print $header;
} elsif ( $header =~ /^\*+/ ) {
$input_format = "vertical";
print $style_row, $header, $reset;
} else {
# Unknown format, will proceed without coloring
print $header;
}
my $date = '\d{4}-\d{2}-\d{2}';
my $time = '\d{2}:\d{2}:\d{2}(?:\.\d+)?';
# Quick max function :p
sub max(@) { (sort @_)[-1] }
# If output to a non-terminal, don't bother sending data to less
# TODO should not buffer in $outstring then
my $lesspid;
my $useless = !(-t STDOUT) || undef;
my $cur_cols = length($header);
my $cur_lines = scalar(grep /\n/, $outstring);
my $count = 0;
while (my $line = <>) {
if ( ! $useless ) {
$cur_lines++;
$cur_cols = max($cur_cols, length($line));
# adding lines may lead to full terminal height
# will lead to using less, which will wrap long lines
if ( not $CONF{"long-lines-to-less"} ) {
$cur_lines += int(length($line) / $term_cols);
}
if ( $cur_lines > $term_lines ||
($CONF{"long-lines-to-less"} && $cur_cols - 1 > $term_cols) ) {
# Switch to less, and write current buffer
$lesspid = open($useless, '| less -R')
or die("Can't open less");
select($useless);
print $useless $outstring;
close($outhandle);
$outstring = "";
}
} elsif ( $lesspid && $count++ == 300 ) {
# every 300 rows, check that less didn't exit
# (don't hang CPU on large resultsets)
$count = 0;
if ( -1 == waitpid($lesspid, WNOHANG)) {
last;
}
}
if ( $input_format eq "std" ) {
$line =~ s/(\| +)(NULL +)(?=\|)/$1$style_null$2$reset/g;
$line =~ s/(\| +)(-?\d+\.?\d*(?:e\+\d+)? )(?=\|)/$1$style_int$2$reset/g;
$line =~ s/\| ((?:$date(?: $time)?|(?:$date )?$time) +)(?=\|)/| $style_date$1$reset/g;
} elsif ( $input_format eq "vertical" ) {
$line =~ s/^((\*{27}) \d+\..*? \*{27})/$style_row$1$reset/;
$line =~ s/^( *)(\S+)(?=: )/$1$style_header$2$reset/;
$line =~ s/: (NULL)$/: $style_null$1$reset/ ||
$line =~ s/: (-?\d+\.?\d*)$/: $style_int$1$reset/ ||
$line =~ s/: ((?:$date(?: $time)?|(?:$date )?$time))$/: $style_date$1$reset/;
}
print $line;
}
# this should be placed in another file, but I'd really
# like to keep this utility in one script
package MyPager::Config;
use strict;
use warnings;
use Fcntl qw/SEEK_SET/;
use constant CONFPATH => "~/.mypager.conf";
sub get_config() {
my %return = ();
my $config_file = CONFPATH;
$config_file = glob($config_file);
my $strconf = undef;
# Try to read config file, otherwise revert to internal defaults
if ( -f $config_file && -r _ ) {
open CONF, $config_file;
$strconf = join "", <CONF>;
close CONF;
} else {
$strconf = strdata();
$return{'-defaults'} = 1;
}
# Remove inline comments
$strconf =~ s/(?<!\\)\s+#.*//gm;
# and unescape the non-comments #
$strconf =~ s/\\#/#/gm;
# Simple scalars, allow empty values with "varname = "
while ( $strconf =~ /^[\040\t]*([^#\@\s]+?)[\040\t]*=[\040\t]*(.*?)[\040\t]*$/gm ) {
next if defined($return{$1}); # really ?
$return{$1} = $2;
}
# Arrays
while ( $strconf =~ /\@(\S+?)\s*=\s*\((.*?)(?<!\\)\)/gs ) {
next if defined($return{$1});
my @values =
# 3. then unescape spaces and parenthesis
map { s/\\([ )])/$1/g; $_ }
# 2. remove empty matches
grep { length }
# 1. split using non-escaped whitespaces
split /(?<!\\)\s+/s, $2;
$return{$1} = \@values
}
# and no dict yet
return \%return;
}
sub strdata() {
# Rewind data handle after reading, in case we'll need to read it again
my $origin = tell(DATA);
my $strconf = join "", <DATA>;
seek(DATA, $origin, SEEK_SET);
return $strconf;
}
1;
# Bellow is the default config, you can copy its contents to ~/.mypager.conf
# if you wish to configure it.
# Or simply change the values bellow :)
__DATA__
# This is the default configuration file
# 1: mypager will switch to less if it encounters any line longer than screen
# width (even if they fit within the height of the screen)
# 0: it will only take the height as variable to switch to less.
long-lines-to-less = 1
# Options passed on to less (as environment variable)
# default: -S to chop long lines
# you can add -I for case insensitive searches for example
# `man less` for all options
less-options = -S
# If the $LESS environment variable is already set, the default is to set our
# config options ("less-options") with a lower priority (in case of conflicts)
# Set to 1 to "override" the environment variable options
less-options-overrides-env = 0