/[scripts]/trunk/sum.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/sum.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (show annotations)
Tue Apr 8 20:46:20 2008 UTC (14 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 1323 byte(s)
qute little number summer with graph from Feb 10, 2005

1 #!/usr/bin/perl -w
2
3 use strict;
4 use Term::ReadKey qw(GetTerminalSize);
5
6 my @lines;
7
8 my $max_size = 0;
9 my $max_sum = 0;
10 my $rest_len = 0;
11 my $sum = 0;
12
13 while(<>) {
14 chomp;
15 if (/\s*([\d\.]+)\s+(.+)/) {
16 my ($size,$rest) = ($1,$2);
17 $sum += $size;
18 $max_size = $size if ($size > $max_size);
19 $max_sum = $sum if ($sum > $max_sum);
20 my $rl = length($rest);
21 $rest_len = $rl if ($rl > $rest_len);
22 push @lines, [ $size, $rest, $sum ];
23 } else {
24 print STDERR "# $_\n";
25 }
26 }
27
28 my $cols = eval { (Term::ReadKey::GetTerminalSize)[0] } || $ENV{COLS} || $ENV{COLUMNS} || 80;
29
30 # convert to chars
31 my $size_len = length($max_size);
32 my $sum_len = length($max_sum);
33
34 my $bar_size = $cols - $rest_len - $sum_len - $size_len - 4;
35
36 sub bar_size {
37 my ($v,$m) = @_;
38 return int($v * $bar_size / $m)
39 }
40
41 sub bar {
42
43 my ($v1,$m1,$v2,$m2) = @_;
44 my $b1 = bar_size($v1, $m1);
45 my $b2 = bar_size($v2, $m2);
46
47 my $bar = '';
48
49 if ($b1 < $b2) {
50 $bar .= "O" x $b1;
51 $bar .= "-" x ($b2 - $b1);
52 } elsif ($b1 > $b2) {
53 $bar .= "*" x $b2;
54 $bar .= "-" x ($b1 - $b2);
55 } else {
56 $bar .= "O" x $b1;
57 }
58 }
59
60 foreach my $l (@lines) {
61 my ($size,$r,$tmpsum) = @{$l};
62 # printf "%-50s\t[ + %-8d= %-8d ]\n",$r,$size,$tmpsum;
63 printf "%-${rest_len}s %${size_len}d %-${bar_size}s %${sum_len}d\n",
64 $r, $size, bar($size, $sum, $tmpsum, $sum), $tmpsum;
65 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26