Add description support to the generic select_from_list thingy

This commit is contained in:
2026-08-10 22:12:17 +02:00
parent 7f5380dff0
commit 76a8fb4c4a
+20 -2
View File
@@ -24,13 +24,31 @@ sub init_cui {
sub select_from_list { sub select_from_list {
my ( $title, $multiselect, @items ) = @_; my ( $title, $multiselect, @items ) = @_;
# Optional item descriptions: { item => "description" }
my $descriptions =
( @items && ref( $items[-1] ) eq 'HASH' ) ? pop @items : {};
die "No items to choose from" unless @items; die "No items to choose from" unless @items;
# Width of the longest item name to align descriptions
my $width = 0;
for my $item (@items) {
$width = length $item if length $item > $width;
}
my $format = sub {
my ($item) = @_;
my $desc = $descriptions->{$item};
return $item unless defined $desc && length $desc;
return sprintf( "%-*s %s", $width, $item, $desc );
};
my @values = $multiselect ? ( '__ALL__', @items ) : @items; my @values = $multiselect ? ( '__ALL__', @items ) : @items;
my %labels = my %labels =
$multiselect $multiselect
? ( '__ALL__' => '[ALL]', map { $_ => $_ } @items, ) ? ( '__ALL__' => '[ALL]', map { $_ => $format->($_) } @items, )
: map { $_ => $_ } @items; : map { $_ => $format->($_) } @items;
my @selection; my @selection;
my @selection_order; # multiselect only: items in toggle order my @selection_order; # multiselect only: items in toggle order