From 76a8fb4c4af64548aee80d56779cb960f5aa0855 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Mon, 10 Aug 2026 22:12:17 +0200 Subject: [PATCH] Add description support to the generic select_from_list thingy --- scripts/Modules/TUI.pm | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/scripts/Modules/TUI.pm b/scripts/Modules/TUI.pm index 3686506..39be47b 100644 --- a/scripts/Modules/TUI.pm +++ b/scripts/Modules/TUI.pm @@ -24,13 +24,31 @@ sub init_cui { sub select_from_list { 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; + # 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 %labels = $multiselect - ? ( '__ALL__' => '[ALL]', map { $_ => $_ } @items, ) - : map { $_ => $_ } @items; + ? ( '__ALL__' => '[ALL]', map { $_ => $format->($_) } @items, ) + : map { $_ => $format->($_) } @items; my @selection; my @selection_order; # multiselect only: items in toggle order