miscutils/p
Adolfo Perez Alvarez 9465227fe7 Plan 9's p pager
Useful for dumb terminals (e.g. `shell` under Emacs).
2024-06-17 12:06:09 +02:00

48 lines
713 B
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
my $pagesize = $ENV{'LINES'} || 22;
open(my $tty, '<', '/dev/tty')
or die "$0: Cannot open /dev/tty for reading: $!";
sub evalcmd {
{
chomp($_ = <$tty>);
if (/^q$/) {
exit(0);
} elsif (/^!(.*)/) {
system($1);
redo;
}
}
}
sub printfile {
my ($fh) = @_;
my $lines = 0;
while (<$fh>) {
print;
&evalcmd unless ++$lines % $pagesize;
}
&evalcmd;
}
unshift(@ARGV, '-') unless @ARGV;
for (@ARGV) {
if (/^-$/) {
&printfile(\*STDIN);
} elsif (/^-(\d+)$/) {
$pagesize = $1;
} else {
open(my $fh, '<', $_) or die "$0: Cannot open $_ for reading: $!";
&printfile($fh);
close($fh);
}
}
close($tty);