Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

A long time ago, when Kurt Starsinic and I worked in the same office, he wrote a little script to capture the output of du(1) and display only the largest sizes on the screen, and to do it as the results came in, not after du(1) finished. You only have to see the output that matters.

The trick is to use Curses and only remember the largest sizes. It was pretty cool then and it's still pretty cool.

I keep losing the script, though, because I never bothered to copy it to any new machines I got. I think this is the third time that I've rewritten the script, and only because I'm up late at night and I don't want to work on anything useful.

Maybe if I have too much free time on some other night I'll generalize this a bit more. Most of the heavy lifting doesn't care anything about du(1): it just knows it has a number and a string, and it sorts by the number. With a little anonymous sub magic, that number can be anything but formatted differently for display, say, a unix epoch time and a human readable date for sorting a bunch of files by modification time.

Enjoy.

#!/usr/bin/perl open my( $pipe ), "du -a |"; my $files = Local::lines->new; while( <$pipe> ) { chomp; my( $size, $file ) = split /\s+/, $_, 2; next if -d $file; next if $file eq "."; $files->add( $size, "$file" ); } package Local::lines; use Curses; use vars qw($win %rindex); use constant MAX => 24; use constant SIZE => 0; use constant NAME => 1; use Data::Dumper qw(Dumper); sub new { my $self = bless [], __PACKAGE__; $self->init(); return $self; } sub init { my $self = shift; initscr; $win = Curses->new; for( my $i = MAX; $i >= 0; $i-- ) { $self->size( $i, undef ); $self->name( $i, '' ); } } sub DESTROY { endwin; } sub add { my $self = shift; my( $size, $name ) = @_; # add new entries at the end if( $size > $self->size( MAX ) ) { $self->last( $size, $name ); $self->sort; } $self->draw(); } sub sort { my $self = shift; no warnings; $self->elements( sort { $b->[SIZE] <=> $a->[SIZE] } $self->elements ); %rindex = map { $self->name( $_ ), $_ } 0 .. MAX - 1; } sub elements { my $self = shift; if( @_ ) { @$self = @_ } @$self; } sub size { my $self = shift; my $index = shift || -1; if( @_ ) { $self->[$index][SIZE] = shift } $self->[$index][SIZE] || 0; } sub name { my $self = shift; my $index = shift || -1; if( @_ ) { $self->[$index][NAME] = shift } $self->[$index][NAME] || ''; } sub last { my $self = shift; if( @_ ) { $self->size( -1, shift ); $self->name( -1, shift || '' ); } ( $self->size( -1 ), $self->name( -1 ) ); } sub draw { my $self = shift; for( my $i = 0; $i < MAX; $i++ ) { next if $self->size( $i ) == 0 or $self->name( $i ) eq ''; $win->addstr( $i, 1, " " x $Curses::COLS ); $win->addstr( $i, 1, sprintf( "%8d", $self->[$i][SIZE] || '' +) ); $win->addstr( $i, 10, $self->name( $i ) ); $win->refresh; } }
--
brian d foy <bdfoy@cpan.org>

In reply to A du(1) real-time sorter using Curses by brian_d_foy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (6)
    As of 2019-06-26 07:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Is there a future for codeless software?



      Results (109 votes). Check out past polls.

      Notices?