/
11-menus.pl6
executable file
·62 lines (52 loc) · 1.26 KB
/
11-menus.pl6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env perl6
#
# Example translated and modified from C source
# http://www.tldp.org/HOWTO/html_single/NCURSES-Programming-HOWTO/
#
use v6;
use lib 'lib';
use NativeCall;
use NCurses;
my @choices = [
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
];
my $win = initscr() or die "Could not Initialize curses";
cbreak;
noecho;
keypad($win, TRUE);
my $n-choices = @choices.elems;
my $my-items = CArray[ITEM].new;
# Workaround to keep them in-memory
my @str-choices;
for 0..$n-choices - 1 -> $i {
@str-choices.push( CArray[uint8].new(@choices[$i].encode.list) );
$my-items[$i] = new_item(@str-choices[$i], @str-choices[$i]);
}
$my-items[$n-choices] = ITEM.new; # NULL
my $my-menu = new_menu($my-items);
post_menu($my-menu);
mvprintw(LINES() - 2, 0, "Use UP, DOWN arrow keys to switch between menu items. Press ESC to Exit");
nc_refresh;
while (my $c = wgetch($win)) != 27 {
given $c {
when KEY_DOWN {
menu_driver($my-menu, REQ_DOWN_ITEM);
}
when KEY_UP {
menu_driver($my-menu, REQ_UP_ITEM);
}
}
nc_refresh;
}
LEAVE {
# Cleanup
for 0..$n-choices - 1 -> $i {
free_item($my-items[$i]) if $my-items[$i];
}
free_menu($my-menu) if $my-menu;
endwin;
}