Subject: Re: Screen 'stuff'...
From: [email-masked] (Greg Rose)
Date: Mon, 7 Jun 1993 23:46:15 GMT
Newsgroups: comp.lang.perl

In article <[email-masked]> [email-masked] (Jason Verch) writes:
>...  You will have
>a program called curseperl, this is a version of perl that has curses 
>functions built in, you can call them just like perl subroutines, i.e.
>	&initscr;
>It seems to work fairly well so far, I havent played much yet... Does anyone
>have nifty examples of using curses calls in perl to share?

The following script is one of my favourites. When you have a program
which outputs something, and most of the information is constant, but
you want to watch what changes, you use this utility (which I call
'watch'). For example, suppose you are creating a large file, and you
want to monitor the progress of the growth, you could say:

	watch ls -l bigfile

and see it happen. This is not a particularly good example, since in
this case there is only one line to watch, and you could just
repeatedly execute the ls command itself, but when the information
becomes multi-line, or the interesting part might move around, it
becomes a big win -- human eyes being attracted to movement as they
are.

Manual page? You can either give it a command to execute, or it will
read STDIN. In either case, a form feed character on input (preferably
on a line by itself, since the line is discarded) will update the
screen, and further input is considered to be more (slightly
different) of the same; this is for some older programs which ran
continuously and produced paginated output. More normally, the program
is run every $SLEEPTIME seconds, and the modifications apply to
successive runs. As a simple example, try 'watch date'. Program
terminates on terminate or interrupt signals.

Share and enjoy!

Greg Rose                 Australian Computing and Communications Institute
[email-masked]                                              +61 18 174 842
`Use of the standard phrase "HIJACKED" may be inadvisable' -- CAA

----------- cut cut cut -----------

#!/usr/local/bin/curseperl

# Watch the world go by
# Copyright C 1992 Australian Computing and Communications Institute Inc.
# Written by Greg Rose.
# Use it as you will, no warranties expressed or implied, leave
# this copyright statement intact.

$SLEEPTIME = 3; # seconds between executions

$SIG{TERM} = 'catchit';
$SIG{INT} = 'catchit';

sub catchit {
	$caught = 1;
}

if (@ARGV) {
    open(INFILE, "@ARGV|") || die "can't run @ARGV: $!\n";
} else {
    open(INFILE, "<&STDIN") || die "can't dup stdin: $!\n";;
}

&initscr;
&refresh;

eval <<'END';
while (!$caught) {
    &move(0,0);
    $closeit = 1;
    while (($_ = <INFILE>) && !$caught) {
	if (//) {
	    $closeit = 0;
	    last;
	}
	&addstr($_);
    }
    last if $caught;
    &clrtobot;
    &refresh;
    if ($closeit) {
	close(INFILE);
	last unless @ARGV;
	sleep($SLEEPTIME);
	open(INFILE, "@ARGV|") || die "can't run @ARGV: $!\n";
    }
}
END

&move($LINES - 1,0);
&clrtoeol;
&refresh;
&endwin;

warn $@ if $@;
--
Greg Rose                 Australian Computing and Communications Institute
[email-masked]                                              +61 18 174 842
`Use of the standard phrase "HIJACKED" may be inadvisable' -- CAA