Skip to content
Snippets Groups Projects
Commit 96fbff0a authored by Síle Ekaterin Liszka's avatar Síle Ekaterin Liszka
Browse files

Chessa::URI::Plugin::Default: replace format_size with humanize

parent 1294d8ce
No related branches found
No related tags found
No related merge requests found
......@@ -111,7 +111,7 @@ sub head {
return if (!defined($size) || (($size < SZ_MEGABYTE) && ($type =~ m!^image/!)));
$on_info->(
$self->{msg}('Type', $type) . ' ' .
$self->{msg}('Size', $self->format_size($size)) .
$self->{msg}('Size', $self->humanize($size)) .
($redirect ? ' ' . $self->{msg}('Redirect', $redirect) : '')
);
}
......@@ -122,23 +122,17 @@ my @scale = (
[SZ_MEGABYTE, 'MB'],
[SZ_KILOBYTE, 'kB'],
);
my @units = split //, 'kMGTPEZY';
sub format_size {
sub humanize {
my $self = shift;
my ($size) = @_;
my $fmt = '%d B';
return 'N/A' unless defined($size);
for my $i (@scale) {
my ($scale, $text) = ($i->[0], $i->[1]);
if ($size > $scale) {
$size /= $scale;
$fmt = '%.02f ' . $text;
last;
}
}
my $radix = log($size) / log(1024);
$radix = 9 if $radix > 9;
my $val = $size / (1024 ** $radix);
return sprintf $fmt, $size;
return sprintf("%.02f%s", $val, $units[$radix - 1] . 'iB');
}
sub redirect {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment