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