Created
July 13, 2020 17:52
-
-
Save monsieurp/f8a37606f373d071ed98257270a8cccb to your computer and use it in GitHub Desktop.
Centered window and listbox demo using Perl Curses::UI module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
use strict; | |
use Curses; | |
use Curses::UI; | |
my $cui = Curses::UI->new( | |
-color_support => 1, | |
-clear_on_exit => 1 | |
); | |
my $exit_dialog = sub { | |
my $return = $cui->dialog( | |
-message => 'Do you really want to quit?', | |
-tfg => 'red', | |
-fg => 'red', | |
-buttons => [ { -label => '[ Yes ]' }, { -label => '[ No ]' } ] | |
); | |
$return eq 0 and exit(0); | |
}; | |
my $main = $cui->add( 'main', 'Window' ); | |
my $header = $main->add( | |
'header', 'Label', | |
-text => 'Listbox demo', | |
-textalignment => 'middle', | |
-bold => 1, | |
-fg => 'white', | |
-bg => 'blue', | |
-y => 0, | |
-width => -1, | |
-paddingspaces => 1, | |
); | |
my $screen = $cui->add( | |
'screen', 'Window', | |
-border => 1, | |
-width => 60, | |
-height => 7, | |
-centered => 1, | |
); | |
my $find_args = [ 'find', '/dev', '-type', 'c' ]; | |
my $listbox_values = []; | |
open( FIND_PIPE, "-|", @$find_args ) | |
or die "can't run @$find_args: $!"; | |
while (<FIND_PIPE>) { | |
chomp; | |
push @$listbox_values, $_; | |
} | |
close FIND_PIPE; | |
@$listbox_values = sort @$listbox_values; | |
my $explanation = $screen->add( | |
'explanation', 'Label', | |
-text => "Please pick a value from the list:", | |
-padtop => 1, | |
-padleft => 1, | |
-fg => 'blue', | |
); | |
my $listbox = $screen->add( | |
'list', 'Popupmenu', | |
-x => ( length $explanation->text ) + 2, | |
-border => 1, | |
-fg => 'blue', | |
-bg => 'black', | |
-vscrollbar => 1, | |
-wraparound => 1, | |
-values => $listbox_values | |
); | |
$screen->add( | |
'buttons', | |
'Buttonbox', | |
-y => -1, | |
-buttonalignment => 'middle', | |
-fg => 'blue', | |
-buttons => [ | |
{ | |
-label => "[ Get ]", | |
-onpress => sub { | |
my $selected = $listbox->get; | |
my $msg = ''; | |
if ( !defined $selected ) { | |
$msg = "Please select a value!"; | |
} | |
else { | |
chomp $selected; | |
$msg = "You have selected: $selected"; | |
} | |
shift()->root->dialog( | |
-message => $msg, | |
-vscrollbar => 0, | |
); | |
} | |
} | |
] | |
); | |
my $footer = $main->add( | |
'footer', 'Label', | |
-text => "[ctrl+q] Quit.", | |
-textalignment => 'left', | |
-bold => 1, | |
-bg => 'black', | |
-fg => 'white', | |
-y => -1, | |
-width => -1, | |
-paddingspaces => 1, | |
); | |
$header->draw; | |
$footer->draw; | |
$screen->draw; | |
$screen->focus; | |
$cui->set_binding( $exit_dialog, "\cQ" ); | |
$cui->mainloop; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment