mirror of
https://codeberg.org/h3xx/simplify_static_dir
synced 2024-08-14 23:57:24 +00:00
50 lines
1.1 KiB
Perl
50 lines
1.1 KiB
Perl
package Directory::Simplify::Utils;
|
|
# vi: et sts=4 sw=4 ts=4
|
|
use strict;
|
|
use warnings;
|
|
|
|
sub addcommas {
|
|
my @numbers = @_;
|
|
my @added;
|
|
foreach my $num (@numbers) {
|
|
# don't split anything after the decimal
|
|
my @parts = split /\./, $num;
|
|
while ($parts[0] =~ s/(\d)(\d{3}(?:\D|$))/$1,$2/) {
|
|
}
|
|
push @added, (join '.', @parts);
|
|
}
|
|
wantarray ? @added : $added[0]
|
|
}
|
|
|
|
sub hr_size {
|
|
my $sz = shift;
|
|
my @sizes = qw/ B KB MB GB TB PB EB ZB YB /;
|
|
my $fact = 1024;
|
|
my $thresh = 0.1;
|
|
my @ret;
|
|
foreach my $exp (reverse 0 .. $#sizes) {
|
|
if ($sz > (1 - $thresh) * $fact ** $exp) {
|
|
@ret = ($sz / $fact ** $exp, $sizes[$exp]);
|
|
last;
|
|
}
|
|
}
|
|
|
|
# default to ($sz, 'bytes')
|
|
@ret = ($sz, $sizes[0]) unless @ret;
|
|
|
|
wantarray ? @ret : "@ret"
|
|
}
|
|
|
|
sub shell_quote {
|
|
# shell-escape argument for inclusion in non-interpolated single quotes
|
|
my @words = @_;
|
|
my @transformed = map {
|
|
(my $out = $_)
|
|
=~ s/'/'\\''/g;
|
|
"'$out'";
|
|
} @words;
|
|
wantarray ? @transformed : $transformed[0];
|
|
}
|
|
|
|
1;
|