| 1 |
#!/usr/local/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use IO::Pager::Perl; |
| 5 |
use Term::ReadKey; |
| 6 |
use Getopt::Long; |
| 7 |
|
| 8 |
use vars '$VERSION'; |
| 9 |
$VERSION = '2.10'; #Untouched since 2.10 |
| 10 |
|
| 11 |
my %Opts = (fold=>1); |
| 12 |
(my $LESS = $ENV{LESS} || '') =~ s/P.+(?:\$|$)//; |
| 13 |
$Opts{eof} = 1 if $LESS =~ /e/; |
| 14 |
$Opts{statusCol} = 1 if $LESS =~ /J/; |
| 15 |
$Opts{lineNo} = 1 if $LESS =~ /N/; |
| 16 |
$Opts{raw} = 1 if $LESS =~ /r/; |
| 17 |
$Opts{squeeze} = 1 if $LESS =~ /s/; |
| 18 |
$Opts{fold} = 0 if $LESS =~ /S/; |
| 19 |
$Opts{pause} = "\cL" if defined($ENV{MORE}) && $ENV{MORE} =~ /l/; |
| 20 |
|
| 21 |
@ARGV = (map('-'.$_, split(//, $ENV{TPOPT})), @ARGV) if defined($ENV{TPOPT}); |
| 22 |
|
| 23 |
my %Long; |
| 24 |
#Custom argument processing |
| 25 |
{ |
| 26 |
no warnings 'uninitialized'; |
| 27 |
($Long{jump} = (grep { /^\+\d+$/ } @ARGV)[-1]) =~ s/^\+//; |
| 28 |
($Long{search} = (grep { /^\+\// } @ARGV)[-1]) =~ s%\+/=%%; |
| 29 |
} |
| 30 |
@ARGV = grep { $_ !~ /^[-+]\d+$|^\+\// } @ARGV; |
| 31 |
Getopt::Long::Configure("no_ignore_case"); |
| 32 |
GetOptions(\%Long, |
| 33 |
(map { "$_!" } split//, 'JSenrs'), # bare |
| 34 |
(map { "$_=s" } qw'j p cols'), # args ##rows |
| 35 |
'f:s', |
| 36 |
qw/tail|$ scrollbar|[/ |
| 37 |
); |
| 38 |
$Long{f} = "\cL" if defined($Long{f}) && $Long{f} eq ''; |
| 39 |
$Long{tail} = $Long{tail} && scalar(@ARGV) == 1 ? 1 : 0; |
| 40 |
|
| 41 |
$Opts{eof} = $Long{e} if defined($Long{e}); |
| 42 |
$Opts{statusCol} = $Long{J} if defined($Long{J}); |
| 43 |
$Opts{pause} = $Long{f} if defined($Long{f}); |
| 44 |
$Opts{lineNo} = $Long{n} if defined($Long{n}); |
| 45 |
$Opts{raw} = $Long{r} if defined($Long{r}); |
| 46 |
$Opts{squeeze} = $Long{s} if defined($Long{s}); |
| 47 |
#$Opts{rows} = $Long{rows} if defined($Long{rows}); |
| 48 |
$Opts{cols} = $Long{cols} if defined($Long{cols}); |
| 49 |
$Opts{fold} = not $Long{S} if defined($Long{S}); |
| 50 |
$Opts{jump} = ($Long{j}||$Long{jump}) |
| 51 |
if defined($Long{j})||defined($Long{jump}); |
| 52 |
$Opts{search} = $Long{p}||$Long{search} |
| 53 |
if defined($Long{p})||defined($Long{search}); |
| 54 |
|
| 55 |
$Opts{scrollBar} = $Long{scrollbar} if defined($Long{scrollbar}); |
| 56 |
|
| 57 |
#use Data::Dumper; print Dumper \%Opts; exit 0; |
| 58 |
|
| 59 |
my $t = IO::Pager::Perl->new(%Opts); |
| 60 |
|
| 61 |
my($PIPE, $FILE, @F, $prevsize); |
| 62 |
if( -t STDIN ){ |
| 63 |
if( scalar(@ARGV) == 1){ |
| 64 |
#Tail comes first because <ARGV> clobbers @ARGV |
| 65 |
if( $Long{tail} ){ |
| 66 |
open($FILE, '<', $ARGV[0]) or die $!; |
| 67 |
seek($FILE, 0, 2); |
| 68 |
$prevsize = tell($FILE) } |
| 69 |
|
| 70 |
@F = <ARGV> } |
| 71 |
else{ |
| 72 |
#Current multi-file implementation gives us continuous numbering |
| 73 |
|
| 74 |
#Dead-simple option slurs everything together |
| 75 |
# $t->add_text( <ARGV> ); |
| 76 |
|
| 77 |
my $i=1; |
| 78 |
foreach my $file ( @ARGV ){ |
| 79 |
my $err=''; |
| 80 |
open(FILE, '<', $file) or $err=$!; |
| 81 |
push @F, sprintf("======== \cF\c]\cL\cE [%i/%i] %s ========%s\n", |
| 82 |
$i++, $#ARGV+1, $file, $err), <FILE>; |
| 83 |
$F[-1] .= $/ unless $F[-1] =~ /\n$/; |
| 84 |
close(FILE); |
| 85 |
$t->set_fileN($i); |
| 86 |
} |
| 87 |
} |
| 88 |
$t->add_text(@F); |
| 89 |
} |
| 90 |
else{ |
| 91 |
#Separate piped input from keyboard input |
| 92 |
open($PIPE, '<&=STDIN' ) or die $!; |
| 93 |
close(STDIN); |
| 94 |
open(STDIN, '<', '/dev/tty') or die $!; |
| 95 |
} |
| 96 |
|
| 97 |
eval{ |
| 98 |
$t->jump($Opts{jump}) if $Opts{jump}; |
| 99 |
while( $t->more(RT=>.05) ){ |
| 100 |
my $X; |
| 101 |
|
| 102 |
if( defined($PIPE) ){ |
| 103 |
$t->add_text($X) if sysread($PIPE, $X, 1024); |
| 104 |
} |
| 105 |
elsif( $Long{tail} ){ |
| 106 |
my $cursize = -s $FILE; |
| 107 |
if( $cursize > $prevsize ){ |
| 108 |
$t->add_text($X) if sysread($FILE, $X, $cursize-$prevsize); |
| 109 |
$prevsize = $cursize; |
| 110 |
$t->scrollbar() if $t->{scrollBar}; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
__END__ |
| 117 |
=pod |
| 118 |
|
| 119 |
=head1 NAME |
| 120 |
|
| 121 |
tp - a pure perl pager |
| 122 |
|
| 123 |
=head1 SYNOPSIS |
| 124 |
|
| 125 |
tp -[JSenrs] [-cols] [-f STR] [-j|+ #] [-p|+/ STR] [FILE]... |
| 126 |
|
| 127 |
=head1 OPTIONS |
| 128 |
|
| 129 |
=over 4 |
| 130 |
|
| 131 |
=item B<-e> |
| 132 |
|
| 133 |
Exit at end of file. |
| 134 |
|
| 135 |
=item B<-f STR> |
| 136 |
|
| 137 |
If defined, the pager will pause when the character sequence specified |
| 138 |
by STR is encountered in the input text. |
| 139 |
|
| 140 |
The default value when enabled is formfeed i.e; ^L; in order to mimic |
| 141 |
traditional behavior of L<more/1>, but due to the pecularities of |
| 142 |
L<Getopt::Long>> you need to use the -- argument separator in order to |
| 143 |
to trigger this e.g; |
| 144 |
|
| 145 |
tp -f -- foo #pauses on lines in foo with "^L" in them |
| 146 |
|
| 147 |
You might also supply a regular expression as STR e.g; |
| 148 |
|
| 149 |
tp -f '[ie]t' bar #pauses on lines in bar with "it" or "et" in them |
| 150 |
|
| 151 |
=item B<-J> |
| 152 |
|
| 153 |
Add a column with markers indicating which lines match a search expression. |
| 154 |
|
| 155 |
=item B<-n> |
| 156 |
|
| 157 |
Display line numbering. Toggleable at run time with I<#>. |
| 158 |
|
| 159 |
=item B<-r> |
| 160 |
|
| 161 |
Send raw control characters from input unadulterated to the terminal. |
| 162 |
By default, chracters other than tab and newline will be converted to |
| 163 |
caret notation e.g; ^@ for null or ^L for form feed. |
| 164 |
|
| 165 |
=item B<-s> |
| 166 |
|
| 167 |
Squeeze multiple blank lines into one. |
| 168 |
|
| 169 |
=item B<-S> |
| 170 |
|
| 171 |
Do not fold long lines. |
| 172 |
|
| 173 |
=item B<-[> or B<--scrollbar> |
| 174 |
|
| 175 |
Display an interactive scrollbar in the right-most column. |
| 176 |
|
| 177 |
=item B<-$> or B<--tail> |
| 178 |
|
| 179 |
Keep checking the displayed file for new content. |
| 180 |
Only available when paging a single file. |
| 181 |
|
| 182 |
=cut |
| 183 |
|
| 184 |
#=item B<-rows> |
| 185 |
|
| 186 |
Set the number of rows for the pager. |
| 187 |
|
| 188 |
If absent, the terminal is queried directly with L<Term::ReadKey> if |
| 189 |
loaded or C<stty> or C<tput>, and if these fail it defaults to 25. |
| 190 |
|
| 191 |
=pod |
| 192 |
|
| 193 |
=item B<--cols> |
| 194 |
|
| 195 |
Set the number of columns for the pager. |
| 196 |
|
| 197 |
If absent, the terminal is queried directly with L<Term::ReadKey> if |
| 198 |
loaded or C<stty> or C<tput>, and if these fail it defaults to 80. |
| 199 |
|
| 200 |
=back |
| 201 |
|
| 202 |
=head1 User Interface |
| 203 |
|
| 204 |
C<C-> is Control, C<M-> is Meta/Alt, C<S-> is Shift, and C<\d+> is a sequence of digits |
| 205 |
|
| 206 |
=head2 General |
| 207 |
|
| 208 |
=over |
| 209 |
|
| 210 |
=item help - C<h> or C<H> |
| 211 |
|
| 212 |
=item close - C<q> or C<Q> or C<:q> or C<:Q> |
| 213 |
|
| 214 |
=item refresh - C<r> or C<C-l> or C<C-R> |
| 215 |
|
| 216 |
=item flush buffer - C<R> |
| 217 |
|
| 218 |
=item save buffer - C<:w> |
| 219 |
|
| 220 |
=item open file - C<:e> |
| 221 |
|
| 222 |
=back |
| 223 |
|
| 224 |
=head2 Navigation |
| 225 |
|
| 226 |
=over |
| 227 |
|
| 228 |
=item down one line - C<ENTER> or C<e> or C<j> or C<J> or C<C-e> or C<C-n> or C<down arrow> |
| 229 |
|
| 230 |
=item down half page - C<d> or C<C-d> |
| 231 |
|
| 232 |
=item down one page - C<SPACE> C<f> or C<z> or C<C-f> or C<C-v> or C<M-space> or C<PgDn> |
| 233 |
|
| 234 |
=item up one page - C<b> or C<w> or C<C-b> or C<M-v> or C<PgUp> |
| 235 |
|
| 236 |
=item up half page - C<u> or C<C-u> |
| 237 |
|
| 238 |
=item up one line - C<k> or C<y> or C<K> or C<Y> or C<C-K> or C<C-P> or C<C-Y> or C<up arrow> |
| 239 |
|
| 240 |
=item to bottom - C<G> or C<$> or C<E<gt>> or C<M-E<gt>> or C<End> |
| 241 |
|
| 242 |
=item to top - C<g> or C<E<lt>> or C<M-E<lt>> |
| 243 |
|
| 244 |
=item left one tab - C<left arrow> |
| 245 |
|
| 246 |
=item left half screen - C<S-left arrow> |
| 247 |
|
| 248 |
=item right one tab - C<right arrow> |
| 249 |
|
| 250 |
=item right half screen - C<S-right arrow> |
| 251 |
|
| 252 |
=item jump to line number - C<\d+> |
| 253 |
|
| 254 |
=item next file - C<:n> or C<S-M-right arrow> |
| 255 |
|
| 256 |
=item previous file - C<:p> or C<S-M-left arrow> |
| 257 |
|
| 258 |
=back |
| 259 |
|
| 260 |
=head3 Bookmarks |
| 261 |
|
| 262 |
=over |
| 263 |
|
| 264 |
=item Save mark - C<m> or C<Ins> |
| 265 |
|
| 266 |
=item Goto mark - C<'> |
| 267 |
|
| 268 |
=item Special mark: Beginning of file - C<^> |
| 269 |
|
| 270 |
=item Special mark: End of file - C<$> |
| 271 |
|
| 272 |
=item Special mark: Previous location - C<'> |
| 273 |
|
| 274 |
=item Special mark: List user-created marks - C<"> |
| 275 |
|
| 276 |
=item Special mark: C<\d> - Top of file \d when viewing multiple files |
| 277 |
|
| 278 |
=back |
| 279 |
|
| 280 |
=head2 Search |
| 281 |
|
| 282 |
=over |
| 283 |
|
| 284 |
=item forward - / |
| 285 |
|
| 286 |
=item backward - ? |
| 287 |
|
| 288 |
=item next match - n or P |
| 289 |
|
| 290 |
=item previous match - p or N |
| 291 |
|
| 292 |
=item grep (show only matching lines) - & |
| 293 |
|
| 294 |
=back |
| 295 |
|
| 296 |
=head1 Options |
| 297 |
|
| 298 |
=over |
| 299 |
|
| 300 |
=item toggle line-numbering - # |
| 301 |
|
| 302 |
=item toggle folding - S |
| 303 |
|
| 304 |
=item toggle raw/cooked output - C |
| 305 |
|
| 306 |
=back |
| 307 |
|
| 308 |
=head1 ENVIRONMENT |
| 309 |
|
| 310 |
tp checks the I<LESS>, I<MORE>, I<TPOPT>, I<TERM> and I<TERMCAP> variables. |
| 311 |
|
| 312 |
The I<TPOPT> variable is used to set options explicitly for tp, by |
| 313 |
concatenating undecorated options together e.g; Sr for squished raw output. |
| 314 |
|
| 315 |
I<LESS> and I<MORE> are checked for options that tp supports, and if detected |
| 316 |
they are enabled. |
| 317 |
|
| 318 |
=head1 SEE ALSO |
| 319 |
|
| 320 |
L<IO::Pager::Perl>, L<less(1)> |
| 321 |
|
| 322 |
=head1 AUTHORS |
| 323 |
|
| 324 |
Jerrad Pierce [email protected] |
| 325 |
|
| 326 |
=head1 LICENSE |
| 327 |
|
| 328 |
=over |
| 329 |
|
| 330 |
=item * Thou shalt not claim ownership of unmodified materials. |
| 331 |
|
| 332 |
=item * Thou shalt not claim whole ownership of modified materials. |
| 333 |
|
| 334 |
=item * Thou shalt grant the indemnity of the provider of materials. |
| 335 |
|
| 336 |
=item * Thou shalt use and dispense freely without other restrictions. |
| 337 |
|
| 338 |
=back |
| 339 |
|
| 340 |
Or, if you prefer: |
| 341 |
|
| 342 |
This library is free software; you can redistribute it and/or modify |
| 343 |
it under the same terms as Perl itself, either Perl version 5.0 or, |
| 344 |
at your option, any later version of Perl 5 you may have available. |
| 345 |
|
| 346 |
=cut |