<?xml version="1.0" encoding="windows-1252"?>
<node id="253503" title="peek - output a one-line preview of stdin" created="2003-04-27 13:36:59" updated="2005-08-12 20:40:08">
<type id="1748">
sourcecode</type>
<author id="249603">
halley</author>
<data>
<field name="doctext">
&lt;code&gt;
#!/usr/bin/perl

#----------------------------------------------------------------------------
# Copyright (C) 2001-2003 Ed Halley
#----------------------------------------------------------------------------

=head1 NAME

peek - eat lines of standard input; output a one-line preview instead

=head1 SYNOPSIS

    tar jcvf home.tar.bz2 /home | peek

=head1 DESCRIPTION

The peek process reads line-oriented text from the standard input, and
outputs each line atop each other on the standard output.  This is useful
for reducing visual clutter in large list-oriented processing while
keeping some visual feedback of the progress.

There are a few options to customize the style, but the defaults are
usually quite sufficient.

=cut

#-----------------------------------------------------------------------------

use Getopt::Long;

use warnings;
use strict;

my $wide = 79;
my $keep = 0;

my %options =
    ( 'wide|width=i' =&gt; \$wide,
      'keep!' =&gt; \$keep,
      );

GetOptions(%options) or die;

$wide = 0 if $wide &lt; 1;

#-----------------------------------------------------------------------------

$| = 1;
while (&lt;&gt;)
{
    chomp;
    tr{\n\t}{\r };
    printf "%-${wide}.${wide}s\r", $_;
}
print ' 'x$wide, "\r" if not $keep;
print "\n" if $keep;
exit(0);

__END__

#-----------------------------------------------------------------------------

=head1 OPTIONS

=over 4

=item B&lt;--width&gt;=[integer]

Truncates the output lines to the given width, so as to avoid any line
wrapping issues.  Default is 79 characters wide.

=item B&lt;--keep&gt;

The final line of output is left visible on the display.

=head1 BUGS

The --width should default to the current terminal width, if it can be
determined.

This animation assumes that a carriage return (as opposed to a newline or
linefeed) will not erase the current line, but will return the output
cursor to the beginning of the current line.  This is a common convention
and holds true for many POSIX-styled terminals, but may not work for all
output devices.

=head1 LICENSE

Copyright (C) 2001-2003  Ed Halley  &lt;ed@halley.cc&gt;

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.  For details on the Perl Artistic
License, read the F&lt;http://www.perl.com/language/misc/Artistic.html&gt;
page.

=cut

&lt;/code&gt;
</field>
<field name="codedescription">
Some commands give visual feedback that is either feast or flood.  They either remain blissfully silent while they work on a long task, or they blather endlessly about every step of their progress.

&lt;p&gt;Many times, it'd be handy to have feedback somewhere in between.  You can still see it's running, but it doesn't scroll your terminal out of sight.

&lt;p&gt;This is essentially a Perl one-liner, written to a script.  It's trivial.  It's not about how tough the task is, but whether you find it useful.

&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: printf and --keep suggestions implemented</field>
<field name="codecategory">
Utility Scripts</field>
<field name="codeauthor">
Ed Halley &lt;ed@halley.cc&gt; This program is free software; you can redistribute it and/or modify it under the same terms as Perl</field>
</data>
</node>
