strip trailing carriage returns
[barcat.git] / graph
diff --git a/graph b/graph
index d317df5e7f848922e07986cd2fb558ca8973a49c..e76ec645492b138a49ddfb11a52f1adf2e4b2088 100755 (executable)
--- a/graph
+++ b/graph
@@ -8,12 +8,16 @@ use open qw( :std :utf8 );
 our $VERSION = '1.02';
 
 use Getopt::Long '2.33', qw( :config gnu_getopt );
-sub podexit { require Pod::Usage; Pod::Usage::pod2usage(-exitval => 0, @_) }
+sub podexit {
+       require Pod::Usage;
+       Pod::Usage::pod2usage(-exitval => 0, -perldocopt => '-oman', @_);
+}
 GetOptions(\my %opt,
        'color|c!',
        'follow|f:i',
        'trim|length|l=i',
        'markers|m=s',
+       'unmodified|u!',
        'width|w=i',
        'usage|h' => sub { podexit() },
        'help'    => sub { podexit(-verbose => 2) },
@@ -35,8 +39,9 @@ $SIG{INT} = 'IGNORE';  # continue after assumed eof
 
 my (@lines, @values);
 while (readline) {
-       chomp;
-       push @values, s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x && $1;
+       s/\r?\n\z//;
+       s/^\h*// unless $opt{unmodified};
+       push @values, s/^ ( \h* -? [0-9]* \.? [0-9]+ |)//x && $1;
        if (defined $opt{trim}) {
                my $trimpos = abs $opt{trim};
                if ($trimpos <= 1) {
@@ -54,6 +59,8 @@ $SIG{INT} = 'DEFAULT';
 
 sub show_lines {
 
+state $nr = 0;
+
 my @order  = sort { $b <=> $a } grep { length } @values;
 my $maxval = $order[0];
 my $minval = min $order[-1], 0;
@@ -72,9 +79,21 @@ if ($opt{markers} // 1 and $size > 0) {
        $barmark[ orderpos($#order / 2) ] = '+';  # mean
        $barmark[ -$minval * $size ] = '|' if $minval < 0;  # zero
        defined and $opt{color} and $_ = "\e[36m$_\e[0m" for @barmark;
+
+       state $lastmax = $maxval;
+       if ($maxval > $lastmax) {
+               print ' ' x ($lenval + $len);
+               printf "\e[90m" if $opt{color};
+               printf '%-*s',
+                       ($lastmax - $minval) * $size + .5,
+                       '-' x (($values[$nr - 1] - $minval) * $size);
+               print "\e[92m" if $opt{color};
+               say '+' x (($maxval - $lastmax - $minval) * $size + .5);
+               print "\e[0m" if $opt{color};
+               $lastmax = $maxval;
+       }
 }
 
-state $nr = 0;
 while ($nr <= $#lines) {
        my $val = $values[$nr];
        if (length $val) {
@@ -166,6 +185,11 @@ or 68% of all entries.
 
 =back
 
+=item -u, --unmodified
+
+Do not strip leading whitespace.
+Keep original value alignment, which may be significant in some programs.
+
 =item -w, --width=<columns>
 
 Override the maximum number of columns to use.
@@ -200,7 +224,7 @@ Number of HTTP requests per day:
 Any kind of database query with leading counts:
 
     echo 'SELECT count(*),schemaname FROM pg_tables GROUP BY 2' |
-    psql -t | graph
+    psql -t | graph -u
 
 Git statistics, such commit count by year: