mirror of
https://codeberg.org/h3xx/simplify_static_dir
synced 2024-08-14 23:57:24 +00:00
62f2503cb0
That's what .editorconfig is for.
50 lines
1.1 KiB
Perl
50 lines
1.1 KiB
Perl
package Directory::Simplify::Utils;
|
|
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);
|
|
}
|
|
return 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')
|
|
unless (@ret) {
|
|
@ret = ($sz, $sizes[0]);
|
|
}
|
|
|
|
return 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;
|
|
return wantarray ? @transformed : $transformed[0];
|
|
}
|
|
|
|
1;
|