From a5efc91e9cfa40607a2064b8662047788a246ab1 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 30 Jan 2021 23:10:32 -0500 Subject: [PATCH] vim movement --- headers/menu.h | 9 +++++++++ menu.c | 24 ++++++++++++++++++++++++ taskasaur.c | 36 +++++++++++++++++++++++++----------- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/headers/menu.h b/headers/menu.h index ce0883e..0d17779 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -5,6 +5,13 @@ /* ncurses doesn't support multiline menu items, * so i wrote my own menu lib */ +typedef enum { + MENU_UP = 0, + MENU_DOWN, + MENU_TOP, + MENU_BOTTOM +} MenuAction; + typedef struct Menu Menu; typedef struct MenuItem MenuItem; @@ -15,6 +22,8 @@ extern int set_menu_items(Menu* menu, MenuItem** item_list); extern int set_menu_win(Menu* menu, WINDOW* win); extern MenuItem* get_menu_items(Menu* menu); +extern int menu_driver(Menu* menu, MenuAction action); + extern int render_menu(Menu* menu); extern int free_menu(Menu* menu); diff --git a/menu.c b/menu.c index f077fd2..777e334 100644 --- a/menu.c +++ b/menu.c @@ -70,6 +70,30 @@ get_menu_items(Menu* menu) return NULL; } +int +menu_driver(Menu* menu, MenuAction action) +{ + + switch (action) { + case MENU_UP: + menu->selected_item = menu->selected_item-1 >= 0 ? menu->selected_item-1 : 0; + break; + case MENU_DOWN: + menu->selected_item = menu->selected_item+1 <= menu->menu_length-1 ? menu->selected_item+1 : menu->menu_length-1; + break; + case MENU_TOP: + menu->selected_item = 0; + break; + case MENU_BOTTOM: + menu->selected_item = menu->menu_length-1; + break; + default: + fprintf(stderr, "Invalid menu action"); + } + + return 0; +} + int render_menu(Menu* menu) { diff --git a/taskasaur.c b/taskasaur.c index 647c848..c8fd8e6 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -3,10 +3,12 @@ #include "headers/render.h" #include "headers/menu.h" #include "headers/utils.h" +#include "config.h" int main(int argc, char** argv) { + char ch; /* Board* board; */ /* board = begin_parse("test_board.md"); */ /* log_todo(board); */ @@ -14,26 +16,38 @@ main(int argc, char** argv) /* init curses */ init_tscurses(); - - MenuItem** item_list = malloc(4*sizeof(MenuItem*)); - for (int i = 0; i < 3; i++) { + MenuItem** item_list = malloc(6*sizeof(MenuItem*)); + for (int i = 0; i < 5; i++) { item_list[i] = create_menuitem("Many of you are probably feeling a little sad."); } + item_list[5] = 0; Menu* menu = create_menu(item_list); - render_menu(menu); - getch(); + while ((ch = getch()) != BINDING_QUIT) { + + switch (ch) { + + case BINDING_SCROLL_UP: + menu_driver(menu, MENU_UP); + break; + case BINDING_SCROLL_DOWN: + menu_driver(menu, MENU_DOWN); + break; + case BINDING_JUMP_TOP: + menu_driver(menu, MENU_TOP); + break; + case BINDING_JUMP_BOTTOM: + menu_driver(menu, MENU_BOTTOM); + break; + } + render_menu(menu); - exit_tscurses(); + } - /* int out; */ - /* printf("%s", wrap_text("ayylmaooooxdxdxdxdxd", 3, &out)); */ - /* printf("%d\n", out); */ - /* wrap_text("ayylmaooooxdxdxdxdxd", 3, &out); */ - + exit_tscurses(); return 0; } -- 2.20.1