simplify_static_dir/lib/Directory/Simplify/Utils.pm

50 lines
1.1 KiB
Perl
Raw Normal View History

2022-11-16 17:33:08 +00:00
package Directory::Simplify::Utils;
use strict;
use warnings;
sub addcommas {
2023-07-20 17:37:07 +00:00
my @numbers = @_;
2022-11-16 17:33:08 +00:00
my @added;
2023-07-20 17:37:07 +00:00
foreach my $num (@numbers) {
2022-11-16 17:33:08 +00:00
# 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);
}
2023-07-20 17:37:27 +00:00
return wantarray ? @added : $added[0];
2022-11-16 17:33:08 +00:00
}
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')
2023-07-20 18:05:12 +00:00
unless (@ret) {
@ret = ($sz, $sizes[0]);
}
2022-11-16 17:33:08 +00:00
2023-07-20 17:37:27 +00:00
return wantarray ? @ret : "@ret";
2022-11-16 17:33:08 +00:00
}
sub shell_quote {
# shell-escape argument for inclusion in non-interpolated single quotes
2023-07-20 17:37:07 +00:00
my @words = @_;
2023-07-20 20:00:46 +00:00
foreach my $word (@words) {
$word =~ s/'/'\\''/g;
$word = "'$word'";
}
return wantarray ? @words : $words[0];
2022-11-16 17:33:08 +00:00
}
1;