Skip to content

Instantly share code, notes, and snippets.

@monsieurp
Last active July 13, 2020 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsieurp/218c7651ff68c5a35c5ab29629339f58 to your computer and use it in GitHub Desktop.
Save monsieurp/218c7651ff68c5a35c5ab29629339f58 to your computer and use it in GitHub Desktop.
Create a centered window with a label using Perl Curses::UI module
#!/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 $sigint_dialog = sub {
my $return = $cui->dialog(
-message =>
"You've hit CTRL+C and/or ESC!\n\n"
. "What do you want to do?",
-tfg => 'red',
-fg => 'red',
-buttons => [
{
-label => '[ Continue ]',
-value => 0
},
{
-label => '[ Restart ]',
-value => 1
},
{
-label => '[ Abort ]',
-value => 2
}
]
);
if ($return eq 1 || $return eq 2) {
exit 0;
}
};
my $main = $cui->add('main', 'Window');
my $header = $main->add(
'header', 'Label',
-text => 'Window centered demo',
-textalignment => 'middle',
-bold => 1,
-fg => 'white',
-bg => 'blue',
-y => 0,
-width => -1,
-paddingspaces => 1,
);
my $main_window = $cui->add(
'main_window', 'Window',
-border => 1,
-padtop => 1,
-padbottom => 1,
-width => 20,
-height => 7,
-centered => 1
);
my $container = $main_window->add(
'main_cont', 'Container',
);
my $explanation = $container->add(
'explanation', 'Label',
-text => "How are you?",
-padtop => 1,
-padleft => 1,
-fg => 'blue',
);
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;
$main_window->draw;
$main_window->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