Category:
Author/Contact Info
Description:

I use this as the front end component of a graphical "top" command. Its purpose is to read some numbers in columns from STDIN and make a little scrolling graph out of it. Every line in the output is a line of data - the columns are the scale 0 to whatever the column's maximum is. The program accepts 1 or more pairs of keyword and scale.

I feed the columns Blocks In/Out, User/System/IO/Idle CPU from vmstat into it with the following script:

#!/bin/sh vmstat -n 1 | perl -lane 'INIT{$,=q[ ];$|=1}print @F[8,9,12..15]' | lo +okee in 6000 out 6000 u 100 s 100 i 100 w 100
wutu i
wut i
wut i
wst i
wn out i
wst i
outwin u i
wuts u i
wut s u i
in ws u i
in s w u i
in s out w u i
wut i
wut i
us w out i
sn out w i
wst i
wut i
wut i
wut i
wut i
wut i
wut i
wut u i
out w u in i
out s u w in i
out w s in u i
outwn s u i
wut s u i
wout s u i
#!/usr/bin/perl
use strict;
use warnings;
use constant { MAX => 1, SIGIL => 0 };

$| = 1;
chomp( my $term_lines   = 0+`tput lines` );
chomp( my $term_columns = 0+`tput cols` );

@ARGV % 2 == 0 or die "$0: SIGIL SCALE ...\n";
my @trackers = map [ @ARGV[$_*2, 1+($_*2) ] ], 0 .. $#ARGV / 2;

my @lines;
while (<STDIN>) {
    my @F = /(\d+)/g or next;
    push @lines, \ @F;
    shift @lines if @lines >= $term_lines;

    # Drop my colored dots onto the field.
    my @cols = (' ') x $term_columns;
    for my $ix ( 0 .. $#trackers ) {
        my $tracker = $trackers[$ix];
        my $max   = \ $tracker->[MAX];
        my $sigil = $tracker->[SIGIL];
        my $datum = $F[$ix];

        if ( $$max < $datum ) {
          $$max = $datum;
        }
        my $scale = $$max;
        my $adjusted = $scale == 0 ? 0 : $datum / $scale;

        my $start_col = $#cols * $adjusted;
        for my $char ( 0 .. length($sigil)-1 ) {
          $cols[ $start_col + $char ] = "\e[" . (31 + $ix) . 'm' . sub
+str( $sigil, $char, 1 ) . "\e[0m";
        }
        # print "ix:$ix,col:$$col,max:$$max,sigil:$sigil,datum:$datum,
+scale:$scale,adjusted:$adjusted,col_idx:$col_idx\n";
    }

    splice @cols, $term_columns - 1 if @cols > $term_columns;
    print join( '', @cols ) . "\n";
}