From cbf018815e12a627e8331a85088671e2c7eca0c5 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 5 Sep 2019 17:05:39 +0200 Subject: [PATCH] parse values while reading lines Manipulate input as it comes in, spreading the load and having values ready for upcoming interrupts without a separate phase to repeat. --- graph | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/graph b/graph index 501ac3c..cb8a406 100755 --- a/graph +++ b/graph @@ -20,16 +20,24 @@ GetOptions(\my %opt, $opt{width} ||= $ENV{COLUMNS} || 80; $opt{color} //= 1; -my @lines = readline or exit; -chomp for @lines; -my @values = map { s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x and $1 } @lines; -my @order = sort { $b <=> $a } grep { length } @values; -if (defined $opt{trim}) { - my $trimpos = abs $opt{trim}; - $trimpos <= 1 ? ($_ = substr($_, 0, 1)) : - (length > $trimpos and substr($_, $trimpos - 1) = '…') for @lines; +my (@lines, @values); +while (readline) { + chomp; + push @values, s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x && $1; + if (defined $opt{trim}) { + my $trimpos = abs $opt{trim}; + if ($trimpos <= 1) { + $_ = substr $_, 0, 1; + } + elsif (length > $trimpos) { + substr($_, $trimpos - 1) = '…'; + } + } + push @lines, $_; } +@lines or exit; +my @order = sort { $b <=> $a } grep { length } @values; my $maxval = $order[0]; my $minval = min $order[-1], 0; my $lenval = max map { length } @order; -- 2.30.0