/[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 76 - (show annotations)
Tue Apr 8 21:05:42 2008 UTC (13 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 1713 byte(s)
added -h to produce human-readable output

1 #!/usr/bin/perl -w
2
3 use strict;
4 use Term::ReadKey qw(GetTerminalSize);
5 use Getopt::Long;
6
7 my $human = 0;
8 GetOptions(
9 'human!' => \$human,
10 );
11
12 my @lines;
13
14 my $max_size = 0;
15 my $max_sum = 0;
16 my $rest_len = 0;
17 my $sum = 0;
18
19 my @units = qw/b k M G/;
20 sub unit {
21 my $v = shift;
22
23 return $v unless $human;
24
25 my $o = 0;
26
27 while ( ( $v / 10000 ) >= 1 ) {
28 $o++;
29 $v /= 1024;
30 }
31
32 if ( $v >= 1 ) {
33 return sprintf("%d%s", $v, $units[$o]);
34 } elsif ( $v == 0 ) {
35 return 0;
36 } else {
37 return sprintf("%.1f%s", $v, $units[$o]);
38 }
39 }
40
41 while(<>) {
42 chomp;
43 if (/\s*([\d\.]+)\s+(.+)/) {
44 my ($size,$rest) = ($1,$2);
45 $sum += $size;
46 $max_size = $size if ($size > $max_size);
47 $max_sum = $sum if ($sum > $max_sum);
48 my $rl = length($rest);
49 $rest_len = $rl if ($rl > $rest_len);
50 push @lines, [ $size, $rest, $sum ];
51 } else {
52 print STDERR "# $_\n";
53 }
54 }
55
56 my $cols = eval { (Term::ReadKey::GetTerminalSize)[0] } || $ENV{COLS} || $ENV{COLUMNS} || 80;
57
58 # convert to chars
59 my $size_len = length($max_size);
60 my $sum_len = length($max_sum);
61
62 my $bar_size = $cols - $rest_len - $sum_len - $size_len - 4;
63
64 sub bar_size {
65 my ($v,$m) = @_;
66 return int($v * $bar_size / $m)
67 }
68
69 sub bar {
70
71 my ($v1,$m1,$v2,$m2) = @_;
72 my $b1 = bar_size($v1, $m1);
73 my $b2 = bar_size($v2, $m2);
74
75 my $bar = '';
76
77 if ($b1 < $b2) {
78 $bar .= "O" x $b1;
79 $bar .= "-" x ($b2 - $b1);
80 } elsif ($b1 > $b2) {
81 $bar .= "*" x $b2;
82 $bar .= "-" x ($b1 - $b2);
83 } else {
84 $bar .= "O" x $b1;
85 }
86 }
87
88 foreach my $l (@lines) {
89 my ($size,$r,$tmpsum) = @{$l};
90 # printf "%-50s\t[ + %-8d= %-8d ]\n",$r,$size,$tmpsum;
91 printf "%-${rest_len}s %${size_len}s %-${bar_size}s %${sum_len}s\n",
92 $r, unit($size), bar($size, $sum, $tmpsum, $sum), unit($tmpsum);
93 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26