negative values less than positive
[barcat.git] / graph
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use List::Util qw( max sum );
5 use open qw( :std :utf8 );
6
7 our $VERSION = '1.00';
8
9 use Getopt::Long '2.33';
10 sub podexit { require Pod::Usage; Pod::Usage::pod2usage(-exitval => 0, @_) }
11 GetOptions(\my %opt,
12         'color|c!',
13         'width|w=i',
14         'usage|h' => sub { podexit() },
15         'help'    => sub { podexit(-verbose => 2) },
16 ) or exit 64;  # EX_USAGE
17 $opt{width} ||= $ENV{COLUMNS} || 80;
18 $opt{color} //= 1;
19
20 my @lines = readline or exit;
21 chomp for @lines;
22 my @values = map { s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x and $1 } @lines;
23 my @order  = sort { $b <=> $a } grep { length } @values;
24 my $maxval = $order[0];
25 my $minval = min $order[-1], 0;
26
27 my $lenval = max map { length } @order;
28 my $len    = 1 + max map { length } @lines;  # left padding
29 my $size   = ($maxval - $minval) &&
30         ($opt{width} - $lenval - $len) / ($maxval - $minval);  # bar multiplication
31
32 sub orderpos { (($order[$_[0]] + $order[$_[0] + .5]) / 2 - $minval) * $size }
33 my @barmark;
34 $barmark[ (sum(@order) / @order - $minval) * $size ] = '=';  # average
35 $barmark[ orderpos($#order / 2) ] = '+';  # mean
36 $barmark[ -$minval * $size ] = '|' if $minval < 0;  # zero
37 defined and $opt{color} and $_ = "\e[36m$_\e[0m" for @barmark;
38
39 for my $nr (0 .. $#lines) {
40         my $val = $values[$nr];
41         if (length $val) {
42                 my $color = !$opt{color} ? 0 :
43                         $val == $order[0] ? 32 : # max
44                         $val == $order[-1] ? 31 : # min
45                         90;
46                 printf "\e[%sm", $color if $color;
47                 printf "%*s", $lenval, $val;
48                 print "\e[0m" if $color;
49         }
50         printf '%-*s', $len, $lines[$nr];
51         print $barmark[$_] // '-' for 1 .. (($val || 0) - $minval) * $size;
52         say '';
53 }
54
55 __END__
56
57 =head1 NAME
58
59 graph - append bar chart to input numbers
60
61 =head1 SYNOPSIS
62
63 B<graph> [<options>] [<input>]
64
65 =head1 DESCRIPTION
66
67 Each line starting with a number is given a bar to visualise relative sizes.
68
69 =head1 OPTIONS
70
71 =over
72
73 =item --no-color
74
75 Disable colored output of values and bar markers.
76
77 =item -w, --width=<columns>
78
79 Override the maximum number of columns to use.
80 Appended graphics will extend to fill up the entire screen.
81
82 =back
83
84 =head1 EXAMPLES
85
86 Commonly used after counting, such as users on the current server:
87
88     users | sed 's/ /\n/g' | sort | uniq -c | graph
89
90 Letter frequencies in text files:
91
92     cat /usr/share/games/fortunes/*.u8 |
93     perl -CO -nE 'say for grep length, split /\PL*/, uc' |
94     sort | uniq -c | graph
95
96 Memory usage of user processes:
97
98     ps xo %mem,pid,cmd | graph -l40
99
100 Sizes (in megabytes) of all root files and directories:
101
102     du -d0 -m * | graph
103
104 Number of HTTP requests per day:
105
106     cat log/access.log | cut -d\  -f4 | cut -d: -f1 | uniq -c | graph
107
108 Any kind of database query with leading counts:
109
110     echo 'SELECT count(*),schemaname FROM pg_tables GROUP BY 2' |
111     psql -t | graph
112
113 Git statistics, such commit count by year:
114
115     git log --pretty=%ci | cut -b-4 | uniq -c | graph
116
117 Or the most frequent authors:
118
119     git shortlog -sn | graph | head -3
120
121 =head1 AUTHOR
122
123 Mischa POSLAWSKY <perl@shiar.org>
124
125 =head1 LICENSE
126
127 GPL3+.