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