X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=taskasaur.c;h=87326787fce1dee1195c9cce1ac0b45cec79d0e9;hb=72509aeb4746b7ea2bd9cc84cfd45dfb87042e07;hp=50b72d4ab43c7769f834c1895463dd456fede760;hpb=8fb19bb7969617821e9c2c1c64d113ff2c26abaf;p=taskasaur.git diff --git a/taskasaur.c b/taskasaur.c index 50b72d4..8732678 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -1,39 +1,285 @@ + #include "headers/parser.h" +#include "headers/render.h" +#include "headers/menu.h" +#include "headers/utils.h" +#include "config.h" + +// this is temp +#define MENU_WIDTH 40 + +typedef struct BoardMenu { + Menu** menu_list; + int menu_count; + int selected; +} BoardMenu; + +BoardMenu* create_board_menu(Board* board); +int set_selected_menu(BoardMenu* boardmenu, int index); + +MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length); +Menu** make_menus(Board* board, int todolist_length); +int swap_menu(BoardMenu* boardmenu, int src_index, int dest_index); int main(int argc, char** argv) { + /* read from todo file */ Board* board; - board = begin_parse("test_board.md"); + /* log_todo(board); */ + + /* init curses */ + init_tscurses(); + + BoardMenu* boardmenu; + boardmenu = create_board_menu(board); + + // this is temp + for (int i = 0; i < boardmenu->menu_count; i++) { + render_menu(boardmenu->menu_list[i]); + } + + char ch; + while ((ch = getch()) != BINDING_QUIT) { + + Menu* active_menu; + active_menu = boardmenu->menu_list[boardmenu->selected]; + + switch (ch) { + + case BINDING_SCROLL_UP: + menu_driver(active_menu, MENU_UP); + break; + case BINDING_SCROLL_DOWN: + menu_driver(active_menu, MENU_DOWN); + break; + case BINDING_SCROLL_LEFT: + if (boardmenu->selected-1 < 0) break; + set_selected_menu(boardmenu, boardmenu->selected-1); + break; + case BINDING_SCROLL_RIGHT: + if (boardmenu->selected+1 > boardmenu->menu_count-1) break; + set_selected_menu(boardmenu, boardmenu->selected+1); + break; + case BINDING_JUMP_TOP: + menu_driver(active_menu, MENU_TOP); + break; + case BINDING_JUMP_BOTTOM: + menu_driver(active_menu, MENU_BOTTOM); + break; + case BINDING_MOVE_ITEM_UP: + menu_driver(active_menu, MENU_MOVE_UP); + break; + case BINDING_MOVE_ITEM_DOWN: + menu_driver(active_menu, MENU_MOVE_DOWN); + break; + case BINDING_MOVE_ITEM_LEFT: + if (boardmenu->selected-1 < 0) break; + { + Menu* from_menu; + Menu* to_menu; + + from_menu = boardmenu->menu_list[boardmenu->selected], + to_menu = boardmenu->menu_list[boardmenu->selected-1], + + insert_item( + to_menu, + get_menu_item( + from_menu, + get_selected_item(from_menu) + ), + min( + get_selected_item(from_menu), + get_menu_length(to_menu) + ) + ); + delete_item( + from_menu, + get_selected_item(from_menu) + ); + set_selected_menu(boardmenu, boardmenu->selected-1); + } + + break; + case BINDING_MOVE_ITEM_RIGHT: + if (boardmenu->selected >= boardmenu->menu_count-1) break; + // this is legit cpy paste please fix this + { + Menu* from_menu; + Menu* to_menu; - for (int i = 0; i < board->todolist_count; i++) { - TodoList* todolist; - printf("List =-=-=-=-=-==-=-=-=-=-=-\n"); - todolist = board->todolist_list[i]; - printf("List name: %s\n", todolist->list_name); - printf("Num of items: %d\n", todolist->item_count); - - for (int j = 0; j < todolist->item_count; j++) { - TodoItem* todoitem; - printf("Item =-=-=-=-=-\n"); - todoitem = todolist->item_list[j]; - printf("Item name: %s\n", todoitem->item_name); - printf("Description: %s\n", todoitem->description); - printf("Num of subtasks: %d\n", todoitem->subtask_count); - - for (int k = 0; k < todoitem->subtask_count; k++) { - SubTask* subtask; - int done; - - subtask = todoitem->subtask_list[k]; - printf("Subtask: %s, %d\n", subtask->subtask_name, subtask->done); - } + from_menu = boardmenu->menu_list[boardmenu->selected], + to_menu = boardmenu->menu_list[boardmenu->selected+1], + + insert_item( + to_menu, + get_menu_item( + from_menu, + get_selected_item(from_menu) + ), + min( + get_selected_item(from_menu), + get_menu_length(to_menu) + ) + ); + delete_item( + from_menu, + get_selected_item(from_menu) + ); + set_selected_menu(boardmenu, boardmenu->selected+1); + } + + break; + case BINDING_DELETE_ITEM: + menu_driver(active_menu, MENU_DELETE); + break; + case BINDING_APPEND_ITEM: + menu_driver(active_menu, MENU_APPEND); + break; + case BINDING_INSERT_ABOVE: + menu_driver(active_menu, MENU_INSERT_ABOVE); + break; + case BINDING_INSERT_BELOW: + menu_driver(active_menu, MENU_INSERT_BELOW); + break; + /* case BINDING_MOVE_MENU_LEFT: */ + /* if (boardmenu->selected-1 < 0) break; */ + + /* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected-1); */ + /* boardmenu->selected -= 1; */ + /* set_selected_menu(boardmenu, boardmenu->selected); */ + +/* break; */ +/* case BINDING_MOVE_MENU_RIGHT: */ +/* if (boardmenu->selected >= boardmenu->menu_count-1) break; */ +/* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected+1); */ + /* boardmenu->selected += 1; */ + /* set_selected_menu(boardmenu, boardmenu->selected); */ + + break; + case BINDING_EDIT_ITEM: + menu_driver(active_menu, MENU_EDIT); + break; + } + + for (int i = 0; i < boardmenu->menu_count; i++) { + render_menu(boardmenu->menu_list[i]); } + } - /* printf("%d\n", board->todolist_count); */ - /* printf("%s\n", board->todolist_list[1]->list_name); */ + exit_tscurses(); return 0; } + +BoardMenu* +create_board_menu(Board* board) +{ + BoardMenu* new_boardmenu; + + new_boardmenu = malloc(sizeof(BoardMenu)); + + new_boardmenu->menu_list = make_menus(board, board->todolist_count); + new_boardmenu->menu_count = board->todolist_count; + new_boardmenu->selected = 0; + + return new_boardmenu; +} + +int +set_selected_menu(BoardMenu* boardmenu, int index) +{ + Menu* old_menu; + Menu* new_menu; + int new_pos; + + old_menu = boardmenu->menu_list[boardmenu->selected]; + new_menu = boardmenu->menu_list[index]; + + set_menu_focus(old_menu, false); + set_menu_focus(new_menu, true); + + /* also try to jump to a similar position if possible */ + /* rn theres a bug if old menu is empty */ + new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1); + set_selected_item(new_menu, new_pos); + + boardmenu->selected = index; + + return 0; +} + +MenuItem** +todolist_to_menuitem(TodoItem** item_list, int list_length) +{ + MenuItem** items; + + items = malloc((list_length+1)*sizeof(MenuItem*)); + for (int i = 0; i < list_length; i++) { + items[i] = create_menuitem(item_list[i]->item_name); + } + + items[list_length] = 0; //null terminate + return items; +} + +Menu** +make_menus(Board* board, int todolist_length) +{ + + Menu** menu_list; + + menu_list = malloc(todolist_length*sizeof(Menu*)); + + for (int i = 0; i < todolist_length; i++) { + + /* read from parsed */ + TodoList* todo_list = board->todolist_list[i]; + MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count); + + Menu* new_menu = create_menu(todo_list->list_name, item_list); + + /* make window */ + WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i); + box(win, 0, 0); + + /* some menu settings */ + set_menu_win(new_menu, win); + set_menu_focus(new_menu, i == 0); // make first win focused + + /* refresh */ + refresh(); + wrefresh(win); + + menu_list[i] = new_menu; + } + + return menu_list; +} + +int +swap_menu(BoardMenu* boardmenu, int src_index, int dest_index) +{ + /* reposition menus */ + mvwin(get_menu_win(boardmenu->menu_list[src_index]), + 1, 1+MENU_WIDTH*dest_index + ); + mvwin(get_menu_win(boardmenu->menu_list[dest_index]), + 1, 1+MENU_WIDTH*src_index + ); + wrefresh(get_menu_win(boardmenu->menu_list[src_index])); + wrefresh(get_menu_win(boardmenu->menu_list[dest_index])); + /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */ + /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */ + /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */ + /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */ + clear(); + + /* swap in array */ + ar_swap_item(boardmenu->menu_list, src_index, dest_index); + + return 0; +} +