Recently I needed to plot the output of another program, which was recording minute-by-minute values in a CSV file. What I wanted to do was to 'tail' this file and display a plot of the data. Having recently "mastered" Term::Screen for another application involving a continuously updated display, I figured this would be a piece of cake. The code is very simple, and not entirely complete (it still burps some sort of error message as it starts and as it exits, but I haven't had an inspiration as to what's causing the error). It plots data that scrolls down the page with new values appearing at the top (not the natural way to do things, but Term::Screen only has a method to insert a line which moves the rest of the screen down).
#!/usr/bin/perl -w # # Quick 'n' dirty plotting of the free.csv report. use strict; use English; BEGIN { # Argument processing. if (@ARGV == 2 and $ARGV[1] =~ /\D/) { # free_plot.pl 123 file.csv # Yuck, because the 'missing' value could be either top limit. die "Arguments '@ARGV' too hard to interpret: use 'NNN NNN file.c +sv'\n" } elsif (@ARGV == 1 and $ARGV[0] =~ /\D/) { # free_plot.pl file.csv # Stick in a couple of empty strings to get the TOP_* constants to + work. unshift @ARGV, '',''; } } my $screen; # screen control panel use constant TERM_SIZE => 75; # how big is the screen? use constant TIME_FLD => 6; # how wide is the time display? use constant TOP_LOAD => shift || 20; # scale factor for load a +verage use constant TOP_BUFF => shift || 20000; # scale factor for buffer +size INIT { { no warnings; require Term::Screen; } # Set up the screen. $screen = Term::Screen->new() or die "Something's wrong with Term::S +creen"; $screen->clrscr(); $screen->at(0,TIME_FLD)->puts(join('', '|----' x (TERM_SIZE/5), '*/L' . TOP_LOAD, ' o/F+/B' . TOP_BUFF, ) ); } END { # Cleanup. $screen->at($screen->{ROWS}-1, 0); } $|++; # no buffered output # Ctrl-C handler. $SIG{INT} = sub { exit }; # Do we have a filename to read? if (my $file = shift) { open(INPUT, $file) or die $!; } else { open(INPUT, "<&STDIN") or die $!; } while (<INPUT>) { # process all the input # CSV file has 'time,loadavg,junk,buff,...' my($time,$load,$buff,$free) = (split /,/)[0,1,3,10]; next if ($load . $buff . $free) =~ /[^\d\.]/; $screen->at(1,0)->il; # new line after the axis $time =~ s/\D//g; # reduce time to HHMMSS $time =~ s/.*(\d{6})$/$1/; $screen->at(1,0)->puts($time); $_ ||= 0E0 # for old format CSV with missing fields foreach ($load,$buff,$free); # create scaling my $load_sp = min(TERM_SIZE, TERM_SIZE * ($load/TOP_LOAD)); my $buff_sp = min(TERM_SIZE, TERM_SIZE * ($buff/TOP_BUFF)); my $free_sp = min(TERM_SIZE, TERM_SIZE * ($free/TOP_BUFF)); if ($load_sp == $buff_sp) { # The overlap case. $screen->at(1,TIME_FLD+$load_sp)->puts('X'); } else { # The normal case. $screen->at(1,TIME_FLD+$load_sp)->puts('*'); if ($buff_sp == $free_sp) { $screen->at(1,TIME_FLD+$buff_sp)->puts('x'); } else { $screen->at(1,TIME_FLD+$buff_sp)->puts('+'); $screen->at(1,TIME_FLD+$free_sp)->puts('o'); } } } sub min { my @list = grep(defined,@_); return $list[0] unless @list > 1; return (sort {$a <=> $b} @list)[0]; }

In reply to Quick 'n' dirty plotting by Mur

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, details, 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, summary, 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.