Loading menu.c 0 → 100644 +73 −0 Original line number Diff line number Diff line // menu.c #include <stdio.h> #include "menu.h" char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { for (int i = 0; i < num_menu_items; i++) { printf("\n"); // make downward room for the menu } char *selected_action = ""; unsigned int hovered_selection_index = 0; // menu logic! TODO make this cross platform (play nice with Windows...) do { // return cursor to menu position for (int i = 0; i < num_menu_items; i++) { // todo to optimize, only do the erasure part if the hover selection changes printf("\033[A"); // move cursor up one line printf("\033[2K"); // clear entire line } // print menu items, showing selection for (int i = 0; i < num_menu_items; i++) { if (i == hovered_selection_index) { printf("> "); } printf("%s\n", items[i].title); } char key_pressed = (char)getchar(); // with terminal set to raw mode, this will no longer wait for Enter key; note that Enter key is handled with \n case if (key_pressed == '\033') { getchar(); // skip over the '[' from the arrow key press key_pressed = (char)getchar(); switch (key_pressed) { case 'B': key_pressed = 'j'; // todo messy! make this better break; case 'A': key_pressed = 'k'; // todo messy! make this better break; default: break; } } switch (key_pressed) { case 'j': case 'J': case 's': case 'S': if (hovered_selection_index < num_menu_items - 1) { hovered_selection_index++; } break; case 'k': case 'K': case 'w': case 'W': if (hovered_selection_index > 0) { hovered_selection_index--; } break; case '\n': const unsigned int selected_index = hovered_selection_index; selected_action = items[selected_index].command; break; default: break; } } while (selected_action == ""); return selected_action; } menu.h 0 → 100644 +15 −0 Original line number Diff line number Diff line // menu.h #pragma once #ifndef MENU_H #define MENU_H typedef struct { char *title; char *command; } menu_item; char *menu_prompt(const menu_item *items, unsigned int num_menu_items); #endif //MENU_H yaht.c +0 −2 Original line number Diff line number Diff line Loading @@ -22,8 +22,6 @@ int main(void) { clear_screen(); printf("yaht.c\n"); printf("\n\n"); // new menu would erase game name without this menu_item items[] = { {.title = "Demo", .command = "demo"}, Loading Loading
menu.c 0 → 100644 +73 −0 Original line number Diff line number Diff line // menu.c #include <stdio.h> #include "menu.h" char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { for (int i = 0; i < num_menu_items; i++) { printf("\n"); // make downward room for the menu } char *selected_action = ""; unsigned int hovered_selection_index = 0; // menu logic! TODO make this cross platform (play nice with Windows...) do { // return cursor to menu position for (int i = 0; i < num_menu_items; i++) { // todo to optimize, only do the erasure part if the hover selection changes printf("\033[A"); // move cursor up one line printf("\033[2K"); // clear entire line } // print menu items, showing selection for (int i = 0; i < num_menu_items; i++) { if (i == hovered_selection_index) { printf("> "); } printf("%s\n", items[i].title); } char key_pressed = (char)getchar(); // with terminal set to raw mode, this will no longer wait for Enter key; note that Enter key is handled with \n case if (key_pressed == '\033') { getchar(); // skip over the '[' from the arrow key press key_pressed = (char)getchar(); switch (key_pressed) { case 'B': key_pressed = 'j'; // todo messy! make this better break; case 'A': key_pressed = 'k'; // todo messy! make this better break; default: break; } } switch (key_pressed) { case 'j': case 'J': case 's': case 'S': if (hovered_selection_index < num_menu_items - 1) { hovered_selection_index++; } break; case 'k': case 'K': case 'w': case 'W': if (hovered_selection_index > 0) { hovered_selection_index--; } break; case '\n': const unsigned int selected_index = hovered_selection_index; selected_action = items[selected_index].command; break; default: break; } } while (selected_action == ""); return selected_action; }
menu.h 0 → 100644 +15 −0 Original line number Diff line number Diff line // menu.h #pragma once #ifndef MENU_H #define MENU_H typedef struct { char *title; char *command; } menu_item; char *menu_prompt(const menu_item *items, unsigned int num_menu_items); #endif //MENU_H
yaht.c +0 −2 Original line number Diff line number Diff line Loading @@ -22,8 +22,6 @@ int main(void) { clear_screen(); printf("yaht.c\n"); printf("\n\n"); // new menu would erase game name without this menu_item items[] = { {.title = "Demo", .command = "demo"}, Loading