代码格式化
2018-07-27 本文已影响128人
焉知非鱼
use Math::Matrix ;
my $key-width = 0;
my $value-width = 0;
my $comment-width = 0;
my $padding = 5;
my @sep = ('=', '//') ;
my @lengths;
# sub MAIN() {
# }
# 找到每一列的最大长度
for $=finish.lines -> $line {
my @items = $line.split(/ @sep /);
my @l;
for @items -> $item {
@l.append($item.trim.chars);
}
@lengths.push(@l);
}
my $m = Math::Matrix.new( [@lengths] );
my @maximal-width;
my @b = $m.transposed.list-rows;
@maximal-width.append(.max) for @b;
@maximal-width »+=» $padding;
my @f = @maximal-width».&sformat;
my $cformat = j(@f, @sep);
for $=finish.lines -> $l {
my @items = $l.split(/@sep/);
say sprintf("{$cformat}", @items);
}
sub sformat($l) {
return '%-' ~ $l ~ 's'
}
# 如果不使用 is copy , 会修改 @sep 的值, 影响结果, 坑!
sub j(@columns, @separators is copy) {
return (@columns »~» (@separators.append(""))).join("");
}
=finish
val GAS = "gas" //天然气
val HYDROGEN = "hydrogen" //氢气
val GASOLINE = "gasoline" //汽油
val LOWEST_VOLTAGE_CELL_MONOMER = "lowest_voltage_cell_monomer" //电池单体电压最低值