ncurses-examples/source/window/menu.d

76 lines
1.7 KiB
D
Raw Permalink Normal View History

2024-10-12 22:08:40 +00:00
module window.menu;
import singlog;
import std.conv;
import std.string;
import std.stdio;
public import deimos.ncurses;
public import deimos.form;
public import core.stdc.locale;
enum {
COLOR_WINDOW = 1,
COLOR_BACKGROUND = 2
}
void menu() {
setlocale(LC_ALL,"");
initscr();
raw();
noecho();
curs_set(0);
start_color();
init_pair(COLOR_WINDOW, COLOR_WHITE, COLOR_RED);
init_pair(COLOR_BACKGROUND, COLOR_WHITE, COLOR_BLUE);
int width, height;
getmaxyx(stdscr, height, width);
bkgd(COLOR_PAIR(COLOR_BACKGROUND));
refresh();
WINDOW *win = newwin(7, 16, height/2 - 4, width/2 - 8);
keypad(win, TRUE);
box(win, 0, 0);
wbkgd(win, COLOR_PAIR(COLOR_WINDOW));
wrefresh(win);
string[] items = ["Пункт 1", "Пункт 2", "Пункт 3", "Пункт 4", "Пункт 5"];
int current_item = 0;
int count_items = items.length.to!int;
int ch;
do {
switch (ch) {
case KEY_UP:
current_item = (current_item - 1 + count_items) % count_items;
break;
case KEY_DOWN:
current_item = (current_item + 1) % count_items;
break;
case 10:
case KEY_ENTER:
log.i(items[current_item]);
break;
default:
break;
}
foreach (i, item; items) {
if (i == current_item) {
wattron(win, A_REVERSE);
}
mvwprintw(win, i.to!int + 1, 4, item.toStringz);
wattroff(win, A_REVERSE);
}
wrefresh(win);
} while ((ch = wgetch(win)) != KEY_F(2));
endwin();
}