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