这是对 commafy.pl 的一个改进,它允许您在使用 ls 时选择是否列出文件大小。将 ls
别名为 commafy.pl
以便使用它。
#!/usr/bin/perl
# Does ls and adds commas to numbers if ls -l is used.
$largest_number_of_commas = 0;
$result = `ls -C @ARGV`;
# First line adds five spaces before file size
$result =~ s/(^[-lrwxds]{10,}s*[^s]+s*[^s]+s*[^s]+)/$1 /gm;
$result =~ s/(.{5} )(d{4,}) /truncatePre($1,$2).commafy($2). /eg;
$remove_extra_spaces = 5 - $largest_number_of_commas;
$result =~ s/(^[-lrwxds]{10,}s*[^s]+s*[^s]+s*[^s]+) {$remove_extra_spaces}/$1/gm;
print $result;
# adds commas to an integer as appropriate
sub commafy
{
my($num) = @_;
my $len = length($num);
if ($len <= 3) { return $num; }
return commafy(substr($num, 0, $len - 3)) . , . substr($num, -3);
}
# removes as many chars from the end of str as there are commas to be added
# to num
sub truncatePre
{
my($str, $num) = @_;
$numCommas = int((length($num)-1) / 3);
if ($numCommas > $largest_number_of_commas) {$largest_number_of_commas = $numCommas}
return substr($str, 0, length($str) - $numCommas);
}