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