From: Mischa POSLAWSKY Date: Wed, 4 Sep 2019 13:16:23 +0000 (+0200) Subject: write out script, enable warnings X-Git-Tag: v1.00~10 X-Git-Url: http://git.shiar.nl/barcat.git/commitdiff_plain/8e5ab06d01ae253baa5be620c3dcb0996985587c?ds=sidebyside write out script, enable warnings Mostly the same code but more readable and maintainable. Integers are captured and bars enlarged to fill screen width. --- diff --git a/graph b/graph index fbc1248..d20fcd3 100755 --- a/graph +++ b/graph @@ -1,6 +1,18 @@ -#!/usr/bin/perl -l -use List::Util 'max'; -chomp for my @r = <>; -my $len = 1 + max map length, @r; -my $size = (80 - $len) / max @r; -print $_, ' 'x($len-length), '#'x($_*$size) for @r; +#!/usr/bin/env perl +use 5.014; +use warnings; +use List::Util qw( max ); + +my $width = $ENV{COLUMNS} || 80; + +my @lines = readline; +chomp for @lines; +my @values = map { /^\h*([0-9]*)/ } @lines; +my $maxval = max @values; +my $len = 1 + max map { length } @lines; # left padding +my $size = ($width - $len) / $maxval; # bar multiplication + +for (@lines) { + my $val = shift @values; + say $_, ' ' x ($len - length), '#' x ($val * $size); +}