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