diff options
author | Joey Hess <joey@kodama.kitenet.net> | 2008-07-01 18:40:42 -0400 |
---|---|---|
committer | Joey Hess <joey@kodama.kitenet.net> | 2008-07-01 18:40:42 -0400 |
commit | e8a13c32ba090e4428fcc1d25e4f93cd14380326 (patch) | |
tree | c47754e0aa9288b55a1e2cf952d3fe9cf13a1d4f /IkiWiki | |
parent | 8441d3153506bc704d97def187c7a77c10fa7e3f (diff) | |
download | ikiwiki-e8a13c32ba090e4428fcc1d25e4f93cd14380326.tar ikiwiki-e8a13c32ba090e4428fcc1d25e4f93cd14380326.tar.gz |
display human sizes
Diffstat (limited to 'IkiWiki')
-rw-r--r-- | IkiWiki/Plugin/attachment.pm | 70 |
1 files changed, 54 insertions, 16 deletions
diff --git a/IkiWiki/Plugin/attachment.pm b/IkiWiki/Plugin/attachment.pm index 690b61cbc..a8e50f833 100644 --- a/IkiWiki/Plugin/attachment.pm +++ b/IkiWiki/Plugin/attachment.pm @@ -38,7 +38,7 @@ sub attachment_list ($) { push @ret, { "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'">', link => htmllink($page, $page, $f, noimageinline => 1), - size => (stat(_))[7], + size => humansize((stat(_))[7]), mtime => displaytime($IkiWiki::pagemtime{$f}), }; } @@ -174,32 +174,70 @@ sub formbuilder (@) { #{{{ } } # }}} -package IkiWiki::PageSpec; +my %units=( # size in bytes + B => 1, + byte => 1, + KB => 2 ** 10, + kilobyte => 2 ** 10, + K => 2 ** 10, + KB => 2 ** 10, + kilobyte => 2 ** 10, + M => 2 ** 20, + MB => 2 ** 20, + megabyte => 2 ** 20, + G => 2 ** 30, + GB => 2 ** 30, + gigabyte => 2 ** 30, + T => 2 ** 40, + TB => 2 ** 40, + terabyte => 2 ** 40, + P => 2 ** 50, + PB => 2 ** 50, + petabyte => 2 ** 50, + E => 2 ** 60, + EB => 2 ** 60, + exabyte => 2 ** 60, + Z => 2 ** 70, + ZB => 2 ** 70, + zettabyte => 2 ** 70, + Y => 2 ** 80, + YB => 2 ** 80, + yottabyte => 2 ** 80, + # ikiwiki, if you find you need larger data quantities, either modify + # yourself to add them, or travel back in time to 2008 and kill me. + # -- Joey +); sub parsesize ($) { #{{{ my $size=shift; + no warnings; my $base=$size+0; # force to number use warnings; - my $multiple=1; - if ($size=~/kb?$/i) { - $multiple=2**10; - } - elsif ($size=~/mb?$/i) { - $multiple=2**20; - } - elsif ($size=~/gb?$/i) { - $multiple=2**30; + foreach my $unit (sort keys %units) { + if ($size=~/\Q$unit\E/i) { + return $base * $units{$unit}; + } } - elsif ($size=~/tb?$/i) { - $multiple=2**40; + return $base; +} #}}} + +sub humansize ($) { #{{{ + my $size=shift; + + foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) { + if ($size / $units{$unit} > 0.25) { + return (int($size / $units{$unit} * 100)/100)."$unit"; + } } - return $base * $multiple; + return $size; # near zero, or negative } #}}} +package IkiWiki::PageSpec; + sub match_maxsize ($$;@) { #{{{ shift; - my $maxsize=eval{parsesize(shift)}; + my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)}; if ($@) { return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)"); } @@ -219,7 +257,7 @@ sub match_maxsize ($$;@) { #{{{ sub match_minsize ($$;@) { #{{{ shift; - my $minsize=eval{parsesize(shift)}; + my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)}; if ($@) { return IkiWiki::FailReason->new("unable to parse minsize (or number too large)"); } |