简体   繁体   中英

Simple Menu System Perl

I'm trying to have 10-20 commands I run (centos) on a simple menu, Instead of writing from scratch, I was thinking of using ncurses or something similar to this image, very basic

样本

Then that would run whatever command, maybe pause at the end when completed and thats it. I looked for a while for a basic example menu to grab and go and no luck. Found a ton of references with 90% simply dead links, outdated etc. If someone has either an example using curses or a better simple way for me to just have a bunch of console commands run from the menu, that would be great.

Thanks!

Here is an example using Curses::UI and a Curses::UI::Listbox to display the menu:

use strict;
use warnings;
use Curses::UI;

my $cui = Curses::UI->new( -color_support => 1 );
my $win1 = $cui->add('win1', 'Window');

my $listbox = $win1->add(
    'lb',
    'Listbox',
    -vscrollbar => 'left',
    -title      => "Select Playlist",
    -border     => 1,
    -wraparound => 1,
    -values     => [ "Radio",
                 "Recently added",
                 "SD-Alternative",
                 "SD-Country",
                 "SD-Pop",
                 "SD-Rock",
                 "TV Shows",
                 "Voice Memos",
                 "iTunes U",
             ],
    -onchange   => \&selected_item,
);
$cui->draw;
$cui->set_binding( \&exit_dialog , "\cQ");
$cui->mainloop();

sub selected_item {
    my $item = $listbox->get_active_value();
    my $return = $cui->dialog(
        -message   => "You selected item: $item. Do you want to quit?",
        -title     => "Item selected",
        -buttons   => ['yes', 'no'],
    );
    exit 0 if $return;
}

sub exit_dialog {
    my $return = $cui->dialog(
        -message   => "Do you really want to quit?",
        -title     => "Are you sure?",
        -buttons   => ['yes', 'no'],
    );
    exit 0 if $return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM