Plan 9's p pager

Useful for dumb terminals (e.g. `shell` under Emacs).
This commit is contained in:
Adolfo Perez Alvarez 2024-06-17 12:06:09 +02:00
parent 3e32f1b04e
commit 9465227fe7
2 changed files with 95 additions and 0 deletions

47
p Executable file
View File

@ -0,0 +1,47 @@
#!/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);

48
p.1 Normal file
View File

@ -0,0 +1,48 @@
.Dd May 17, 2024
.Dt TS 1
.Os
.Sh NAME
.Nm p
.Nd Paginate
.Sh SYNOPSIS
.Nm
.Op Fl number
.Ar
.Sh DESCRIPTION
.Nm
copies its standard input, or the named files if given,
to its standard output,
stopping at the end of every page and at the end of each file.
The special file name
.Pa -
can be used to refer explicitly to the standard input.
.Pp
Page size is defined by the
.Ev LINES
environment variable.
If not defined, a default page size of 22 will be used.
The page size can be overriden with the numeric option.
.Pp
While waiting for a newline,
.Nm
interprets the commands:
.Bl -tag
.It !
Run the rest of the line as a shell command.
.It q
Quit.
.El
Any other input will show the next input page.
.Sh ENVIRONMENT
.Bl -tag -width LINES
.It Ev LINES
Number of lines of the terminal.
.El
.Sh EXIT STATUS
.Ex -std
.Sh HISTORY
This is a reimplementation of Plan 9's
.Nm
pager.
.Sh AUTHORS
.An Adolfo Perez Alvarez Aq Mt adolfopa@sdf.org