Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
Node Status?
node history
Node Type: sourcecode [id://253503]
[Corion]: The "and I understand it" part is optional.
[Corion]: ambrus: Yes but I also need to implement the file / IO watcher, because Prima has that (in Prima::File), and I need to supply the appropriate thing to make push_write etc. work with Prima
[ambrus]: Corion: yes, you need to implement the io watcher, which should be simple because Prima::File is basically that, and the timer watcher form Prima::Timer
[Corion]: ... or so I think. As I said, I'm somewhat vague on how to make AnyEvent cooperate with a callback-driven IO event loop that gives me callbacks when data is available or can be written
[ambrus]: what push_write thing? I don't think you need that. that's implemented generically by AnyEvent::Handle
[Corion]: ambrus: Yeah, that's what I think as well. But you give me an idea, maybe I should start with implementing the timer, as that should be far simpler and with fewer edge-cases/nasty interaction than the file watcher
[ambrus]: You only provide the watcher part that tells when the handle is readable or writable, not the actual writing and reading.
[Corion]: ambrus: Hmmm. It makes sense that AnyEvent would implement the push_write itself, but I think I don't have a good idea of where the boundary between AnyEvent and the underlying event system lies... Implementing the timer should give me a better idea
[ambrus]: Corion: push_write is in the higher level abstraction of AnyEvent::Handle, not in the watcher
[Corion]: ambrus: Hmm - rereading Prima::File, that merrily coincides with what Prima does - it tells you "you can read", and you're supposed to read from the fh yourself. I thought it called you with the data already read, which would've been harder to integrate

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2016-12-08 12:18 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (141 votes). Check out past polls.