text
stringlengths
0
834k
else {
return ""ppm> "";
}
}
{
# Weights for particular fields: these are stored in percentage of the
# screen width, based on the number of columns they use on an 80 column
# terminal. They also have a minimum and maximum.
use constant MIN => 0;
use constant MAX => 1;
my %weight = (
name => [12, 20],
title => [12, 20],
abstract => [12, 20],
author => [12, 20],
repository => [12, 20],
version => [ 4, 9],
);
my %meth = (
name => 'name',
title => 'title',
version => 'version',
abstract => 'abstract',
author => 'author',
repository => sub {
my $o = shift;
my $rep = $o->repository or return ""Installed"";
my $name = $rep->name;
my $id = $o->id || $name;
my $loc = $rep->location;
""$name [$loc]""
},
);
# These are Text::Autoformat justification marks. They're actually used to
# build a printf() format string, since it's so much more efficient for a
# non-line-wrapping case.
my %just = (
name => '<',
title => '<',
abstract => '<',
author => '<',
repository => '<',
version => '>',
);
my %plus = (
name => '0',
title => '0',
abstract => '0',
author => '0',
repository => '0',
version => '2',
);
my %filt = (
version => q{""[$_]""},
);
sub picture_optimized {
my $o = shift;
my @items = @{shift(@_)};
unless ($o->conf('fields')) {
my $m = $o->setmode('SILENT');
$o->conf('fields', '', 1);
$o->setmode($m);
}
my @fields = split ' ', $o->conf('fields');
$_ = lc $_ for @fields;
my (%max_width, %width);
my $cols = $o->termsize->{cols};
for my $f (@fields) {
my $meth = $meth{$f};
$max_width{$f} = max { length($_->$meth) } @items;
$max_width{$f} += $plus{$f};
$width{$f} = $max_width{$f} / 80 * $cols;
my $max_f = $weight{$f}[MAX] / 80 * $cols;
my $min_f = $weight{$f}[MIN];
my $gw = $width{$f};
$width{$f} = (
$width{$f} > $max_width{$f} ? $max_width{$f} :
$width{$f} > $max_f ? $max_f :
$width{$f} < $min_f ? $min_f : $width{$f}
);
}
my $right = $fields[-1];
my $index_sz = length( scalar(@items) ) + 3; # index spaces
my $space_sz = @fields + 1; # separator spaces
my $room = $cols - $index_sz - $space_sz;
$width{$right} = $room - sum { $width{$_} } @fields[0 .. $#fields-1];
while ($width{$right} > $max_width{$right}) {
my $smallest;
my $n;
for my $k (@fields[0 .. $#fields-1]) {
my $max = $max_width{$k};
my $sz = $width{$k};
$smallest = $k, $n = $max - $sz if $max - $sz > $n;
}
$width{$right}--;
$width{$smallest}++;
}
while ($width{$right} < $weight{$right}[MIN]) {
my $biggest;