4 use List::Util qw( max sum );
8 use Getopt::Long '2.33';
11 ) or exit 64; # EX_USAGE
12 $opt{width} ||= $ENV{COLUMNS} || 80;
14 my @lines = readline or exit;
16 my @values = map { s/^\h*([0-9]*)// and $1 } @lines;
17 my @order = sort { $b <=> $a } @values;
18 my $lenval = 1 + int log($order[0]) / log 10; # max string length
19 my $len = 1 + max map { length } @lines; # left padding
20 my $size = ($opt{width} - $lenval - $len) / $order[0]; # bar multiplication
22 sub orderpos { ($order[$_[0]] + $order[$_[0] + .5]) / 2 * $size }
24 $barmark[ sum(@values) / @values * $size ] = '='; # average
25 $barmark[ orderpos($#order / 2) ] = '+'; # mean
26 defined and $_ = "\e[36m$_\e[0m" for @barmark;
28 for my $nr (0 .. $#lines) {
29 my $val = $values[$nr];
31 $val == $order[0] ? 32 : # max
32 $val == $order[-1] ? 31 : # min
34 printf "\e[%sm", $color if $color;
35 printf "%*s", $lenval, $val;
36 print "\e[0m" if $color;
37 printf '%-*s', $len, $lines[$nr];
38 print $barmark[$_] // '-' for 1 .. $val * $size;
46 graph - append bar chart to input numbers
50 B<graph> [<options>] [<input>]
52 cat ... | uniq -c | graph
56 Each line starting with a number is given a bar to visualise relative sizes.
62 =item -w, --width=<columns>
64 Override the maximum number of columns to use.
65 Appended graphics will extend to fill up the entire screen.
71 Mischa POSLAWSKY <perl@shiar.org>