Commit 5585e774 authored by Chris's avatar Chris
Browse files

menu code is looking better

parent bcd2c549
Loading
Loading
Loading
Loading
+81 −61
Original line number Diff line number Diff line
// menu.c

#include <stdio.h>
#include <string.h>
#include "menu.h"

enum MENU_DIRECTION get_direction();

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
@@ -25,20 +28,45 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) {
            }
            printf("%s\n", items[i].title);
        }

        enum MENU_DIRECTION direction = get_direction();

        switch (direction) {
            case DOWN:
                if (hovered_selection_index < num_menu_items - 1) {
                    hovered_selection_index++;
                }
                break;
            case UP:
                if (hovered_selection_index > 0) {
                    hovered_selection_index--;
                }
                break;
            case SELECT:
                selected_action = items[hovered_selection_index].command; // choose the hovered + selected item
                break;
            default:
                break;
        }
    } while (strcasecmp(selected_action, "") == 0);

    return selected_action;
}

enum MENU_DIRECTION get_direction() {
    // todo test (+ fix) all of this character grabbing stuff in windows - use conio.h as included above
    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

    // handle arrow key press: we have to do it this way because the arrow keys send more than one character
    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;
                return DOWN;
            case 'A':
				key_pressed = 'k'; // todo messy! make this better
				break;
                return UP;
            default:
                break;
        }
@@ -49,25 +77,17 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) {
        case 'J':
        case 's':
        case 'S':
			if (hovered_selection_index < num_menu_items - 1) {
				hovered_selection_index++;
			}
			break;
            return DOWN;
        case 'k':
        case 'K':
        case 'w':
        case 'W':
			if (hovered_selection_index > 0) {
				hovered_selection_index--;
			}
			break;
            return UP;
        case '\n':
			selected_action = items[hovered_selection_index].command; // choose the hovered + selected item
			break;
            return SELECT;
        default:
			break;
            return NONE;
    }
	} while (selected_action == "");

	return selected_action;

}
+2 −0
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@
enum MENU_DIRECTION {
    DOWN,
    UP,
    SELECT,
    NONE,
};

typedef struct {