Skip to content

Instantly share code, notes, and snippets.

@alguerocode
Created July 5, 2022 08:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alguerocode/372cfbc64717be51421b3d04372b4126 to your computer and use it in GitHub Desktop.
Save alguerocode/372cfbc64717be51421b3d04372b4126 to your computer and use it in GitHub Desktop.
C++ menu system using ncurses
#include <ncurses.h>
int main()
{
initscr();
refresh();
noecho();
WINDOW *menuwin = newwin(10, 40, 2, 4);
box(menuwin, 0, 0);
char choices[3][10] = {"Play", "Dashboard", "Setting"};
int choice;
int highlight = 0;
keypad(menuwin, TRUE);
int chociesLength = (sizeof(choices) / sizeof(*choices));
while (1)
{
for (int i = 0; i < chociesLength; i++)
{
if (i == highlight)
{
wattron(menuwin, A_REVERSE);
mvwprintw(menuwin, i + 1, 2, choices[i]);
wattroff(menuwin, A_REVERSE);
}
else
{
mvwprintw(menuwin, i + 1, 2, choices[i]);
}
}
wrefresh(menuwin);
choice = wgetch(menuwin);
switch (choice)
{
case KEY_UP:
if (highlight > 0)
{
highlight--;
}
break;
case KEY_DOWN:
if (highlight < chociesLength - 1)
{
highlight++;
}
break;
default:
break;
}
if (choice == 10)
{
wclear(menuwin);
box(menuwin, 0, 0);
mvwprintw(menuwin, 1, 2, "Your Choice: %s", choices[highlight]);
mvwprintw(menuwin, 3, 2, "back to menu -> press cancel");
wrefresh(menuwin);
while (1)
{
choice = wgetch(menuwin);
if (choice == 263)
break;
}
wclear(menuwin);
box(menuwin, 0, 0);
}
}
endwin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment