Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

peek - output a one-line preview of stdin

by halley (Prior)
on Apr 27, 2003 at 17:36 UTC ( #253503=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Ed Halley <> This program is free software; you can redistribute it and/or modify it under the same terms as Perl
Description: 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.

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.

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.

Update: printf and --keep suggestions implemented


# Copyright (C) 2001-2003 Ed Halley

=head1 NAME

peek - eat lines of standard input; output a one-line preview instead


    tar jcvf home.tar.bz2 /home | peek


The peek process reads line-oriented text from the standard input, and
outputs each line atop each other on the standard output.  This is use
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.



use Getopt::Long;

use warnings;
use strict;

my $wide = 79;
my $keep = 0;

my %options =
    ( 'wide|width=i' => \$wide,
      'keep!' => \$keep,

GetOptions(%options) or die;

$wide = 0 if $wide < 1;


$| = 1;
while (<>)
    tr{\n\t}{\r };
    printf "%-${wide}.${wide}s\r", $_;
print ' 'x$wide, "\r" if not $keep;
print "\n" if $keep;



=head1 OPTIONS

=over 4

=item B<--width>=[integer]

Truncates the output lines to the given width, so as to avoid any line
wrapping issues.  Default is 79 characters wide.

=item B<--keep>

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

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 convent
and holds true for many POSIX-styled terminals, but may not work for a
output devices.

=head1 LICENSE

Copyright (C) 2001-2003  Ed Halley  <>

This program is free software; you can redistribute it and/or modify i
under the same terms as Perl itself.  For details on the Perl Artistic
License, read the F<>

Replies are listed 'Best First'.
•Re: peek - output a one-line preview of stdin
by merlyn (Sage) on Apr 27, 2003 at 20:40 UTC
      Great tip-- I often forget to use printf, as if moving from C to Perl meant having to leave all of C's methods behind. I have never liked the %#.#s notation of printf... and now I imagine perl6's interpolated hashes may make for some righteous DWIM hacks.

      [ e d @ h a l l e y . c c ]

Re: peek - output a one-line preview of stdin
by Improv (Pilgrim) on Apr 28, 2003 at 15:08 UTC
    Let's add the ability to preserve the last line with --keep
    my $wide = 79; my $keep=0; my %options = ( 'wide|width=i' => \$wide, 'keep' => \$keep ); GetOptions(%options) or die;
    while (<>) { chomp; tr{\n\t}{\r }; $_ = substr($_ . $pad, 0, $wide); print "$_\r"; } if(!$keep) {print "$pad\r";} else {print "\n";} exit(0);

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://253503]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2023-03-20 09:56 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.