sid_com
sid_com

Reputation: 25117

Is there a module that allows me, to choose on the terminal with the up- and down-keys?

#!/usr/bin/env perl
use warnings; 
use 5.12.0;
use Term::UI;
use Term::ReadLine;

my $term = Term::ReadLine->new( 'brand' );
my @choices = ( qw( blue red green black white ) );
my $reply = $term->get_reply(
        prompt => 'What is your favorite color?',
        choices => \@choices,
        default => 'blue',
);
say $reply;

Is there a module that allows me, to choose with the up- and down-keys:
I don't have to write my choices like here but I can go with the up/down-key to the line with my favorite color and press "enter".

Upvotes: 1

Views: 302

Answers (2)

sid_com
sid_com

Reputation: 25117

If I replace the layout-function from Clui.pm ( Term::Clui ) with this layout-function

my $no_col = 1;
sub layout { 
    my @list = @_;
    $this_cell = 0; 
    my $irow = 1; 
    my $icol = 0;  
    for my $i ( 0 .. $#list ) {
        if ( not $no_col ) {
            $l[$i] = length( $list[$i] ) + 2;
            if ( $l[$i] > $maxcols - 1 ) {
                $l[$i] = $maxcols - 1; 
            }
            if ( ( $icol + $l[$i] ) >= $maxcols ) {
                $irow++; 
                $icol = 0;
            }
            $irow[$i] = $irow;
        }
        elsif ( $no_col ) {
            $irow[$i] = $irow++;
        }
        return $irow if $irow > $maxrows;
        $icol[$i] = $icol;
        $this_cell = $i if $list[$i] eq $choice;
        if ( not $no_col ) {
            $icol += $l[$i];
        }
    }
    return $irow if not $no_col;
    return --$irow if $no_col;
} 

it does what I want ( without thorough testing and without reading the whole sourcecode )

Upvotes: 0

kixx
kixx

Reputation: 3295

If you want to display a menu in a terminal window, try Term::Clui. It displays a list of choices and lets the user select one or more with mouse or arrow keys.

Upvotes: 2

Related Questions