Commit 458b60ef authored by Chris's avatar Chris
Browse files

optimized redraw of menu items

parent 5585e774
Loading
Loading
Loading
Loading
+19 −10
Original line number Diff line number Diff line
@@ -11,12 +11,13 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) {
        printf("\n"); // make downward room for the menu
    }
    char *selected_action = "";
    int hovered_item_changed = 1;
    unsigned int hovered_selection_index = 0;
    // menu logic! TODO make this cross platform (play nice with Windows...)
    do {
        // return cursor to menu top position
        if (hovered_item_changed) {
            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
            }
@@ -28,6 +29,7 @@ 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();

@@ -35,17 +37,24 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) {
            case DOWN:
                if (hovered_selection_index < num_menu_items - 1) {
                    hovered_selection_index++;
                    hovered_item_changed = 1;
                } else {
                    hovered_item_changed = 0;
                }
                break;
            case UP:
                if (hovered_selection_index > 0) {
                    hovered_selection_index--;
                    hovered_item_changed = 1;
                } else {
                    hovered_item_changed = 0;
                }
                break;
            case SELECT:
                selected_action = items[hovered_selection_index].command; // choose the hovered + selected item
                break;
            default:
                hovered_item_changed = 0;
                break;
        }
    } while (strcasecmp(selected_action, "") == 0);