/
16-menus-colors.pl6
executable file
·90 lines (76 loc) · 2.1 KB
/
16-menus-colors.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env perl6
#
# Example translated and modified from C source
# http://www.tldp.org/HOWTO/html_single/NCURSES-Programming-HOWTO/
#
use v6;
use NativeCall;
use NCurses;
my @choices = [
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
];
# Initialize curses
my $win = initscr() or die "Could not initialize curses";
start_color;
cbreak;
noecho;
keypad($win, TRUE);
# Initialize colors
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
# Initialize items
# Create items
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
item_opts_off($my-items[3], O_SELECTABLE);
item_opts_off($my-items[6], O_SELECTABLE);
# Create menu
my $my-menu = new_menu($my-items);
# Set fore ground and back ground of the menu
set_menu_fore($my-menu, COLOR_PAIR[0] +| A_REVERSE);
set_menu_back($my-menu, COLOR_PAIR[1]);
set_menu_grey($my-menu, COLOR_PAIR[2]);
# Post the menu
mvprintw(LINES() - 3, 0, "Press <ENTER> to see the option selected");
mvprintw(LINES() - 2, 0, "Up and Down arrow keys to naviage (ESC to Exit)");
post_menu($my-menu);
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); }
when 10 {
# Enter
move(20, 0);
clrtoeol;
mvprintw(20, 0, sprintf("Item selected is : %s", item_name(current_item($my-menu))));
pos_menu_cursor($my-menu);
}
}
}
LEAVE {
# Unpost and free all the memory taken up
if $my-menu {
unpost_menu($my-menu);
free_menu($my-menu);
}
for 0..$n-choices - 1 -> $i {
free_item($my-items[$i]) if $my-items && $my-items[$i];
}
endwin;
}