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