X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=taskasaur.c;h=03c3ab443ea06a580f3fed6266834080004591ce;hb=7dd95a0ec56529992866e603dcfaf395614d5bf2;hp=966907bb1a4aeb0676e542c388aff96eef9137b1;hpb=95a6b4ad62f8ef4ddee2f6416a330735d12d5dae;p=taskasaur.git diff --git a/taskasaur.c b/taskasaur.c index 966907b..03c3ab4 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -1,221 +1,169 @@ -#include -#include -#include -#include -#include -#include -#include -void winch_handler(int sig); +#include "headers/parser.h" +#include "headers/render.h" +#include "headers/menu.h" +#include "headers/utils.h" +#include "config.h" -char** read_todo(FILE* file, int* length); +typedef struct BoardMenu { + Menu** menu_list; + int menu_count; + int selected; +} BoardMenu; -WINDOW* create_win(int height, int width, int y, int x); -MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length); +BoardMenu* create_board_menu(Board* board); +int set_selected_menu(BoardMenu* boardmenu, int index); -void on_select(char *item); +MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length); +Menu** make_menus(Board* board, int todolist_length); -void free_todo(char** todo_list, int todo_length); -#include "config.h" +int +main(int argc, char** argv) +{ + /* read from todo file */ + Board* board; + board = begin_parse("test_board.md"); + /* log_todo(board); */ -#define SELECTED_COLOR 1 -#define NON_SELECTED_COLOR 2 + /* init curses */ + init_tscurses(); -int -main(int argc, char** argv) -{ - int flag; - FILE* board_file; - char** todos; - int todo_length; - - int height, width; - int ch; - - WINDOW* todo_win; - MENU* todo_menu; - - signal(SIGWINCH, winch_handler); - - // read command line args - flag = getopt(argc, argv, "o:n:"); - switch (flag) { - case 'o': - - // read from task file (might need to check for read and write permissions) - board_file = fopen(optarg, "r"); - if (!board_file) { - printf("%s does not exist\n", optarg); - return 1; - } - - todos = read_todo(board_file, &todo_length); - fclose(board_file); - - break; - - case 'n': - - // make sure file does not exist - // however, it maybe be possible that an different error has occured (besides the file not existing) - if (access(optarg, F_OK) == 0) { - printf("%s already exists\n", optarg); - return 1; - } - // create a file here - board_file = fopen(optarg, "w"); - // write init stuff here - fclose(board_file); - printf("Successfully created %s\n", optarg); - - todos = malloc(0); - todo_length = 0; - - break; - - case -1: - case '?': - printf("Help string\n"); - return 2; + 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]); } - // start ncurses - initscr(); - cbreak(); - noecho(); - curs_set(0); - keypad(stdscr, TRUE); - start_color(); - - /* colors */ - init_pair(SELECTED_COLOR, selected_color, COLOR_BLACK); - init_pair(NON_SELECTED_COLOR, non_selected_color, COLOR_BLACK); - - getmaxyx(stdscr, height, width); - - todo_win = create_win(20, 40, 5, 5); - todo_menu = create_todo_menu(todo_win, todos, todo_length); - post_menu(todo_menu); - refresh(); - wrefresh(todo_win); - + 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(todo_menu, REQ_UP_ITEM); + menu_driver(active_menu, MENU_UP); break; case BINDING_SCROLL_DOWN: - menu_driver(todo_menu, REQ_DOWN_ITEM); + 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(todo_menu, REQ_FIRST_ITEM); + menu_driver(active_menu, MENU_TOP); break; case BINDING_JUMP_BOTTOM: - menu_driver(todo_menu, REQ_LAST_ITEM); + 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_SELECT: + case BINDING_DELETE_ITEM: + menu_driver(active_menu, MENU_DELETE); break; - } - wrefresh(todo_win); - } + } - endwin(); + for (int i = 0; i < boardmenu->menu_count; i++) { + render_menu(boardmenu->menu_list[i]); + } - /* Free mem */ - unpost_menu(todo_menu); - free_todo(todos, todo_length); + } - return 0; + exit_tscurses(); + return 0; } -void -winch_handler(int sig) +BoardMenu* +create_board_menu(Board* board) { - endwin(); - refresh(); -} + BoardMenu* new_boardmenu; -char** -read_todo(FILE* file, int* length) -{ // apparently getline isn't rly that portable, so consider other options - char** out_arr; - int out_len; - char* lineptr; - size_t len; - ssize_t nread; + new_boardmenu = malloc(sizeof(BoardMenu)); - out_arr = NULL; - out_len = 0; - lineptr = NULL; - len = 0; + new_boardmenu->menu_list = make_menus(board, board->todolist_count); + new_boardmenu->menu_count = board->todolist_count; + new_boardmenu->selected = 0; - while ((nread = getline(&lineptr, &len, file)) != -1) { - out_len++; - out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? - - lineptr[strcspn(lineptr, "\n")] = 0; // remove newline - out_arr[out_len-1] = lineptr; + return new_boardmenu; +} - lineptr = NULL; - } +int +set_selected_menu(BoardMenu* boardmenu, int index) +{ + Menu* old_menu; + Menu* new_menu; - *length = out_len; - return out_arr; + 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); + + boardmenu->selected = index; + + return 0; } -WINDOW* -create_win(int height, int width, int y, int x) +MenuItem** +todolist_to_menuitem(TodoItem** item_list, int list_length) { - WINDOW* new_win = newwin(height, width, y, x); - wrefresh(new_win); - return new_win; + 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* -create_todo_menu(WINDOW* win, char** todo_list, int todo_length) +Menu** +make_menus(Board* board, int todolist_length) { - MENU* todo_menu; - ITEM** item_list; - ITEM* cur_item; - int wheight, wwidth; - - item_list = malloc((todo_length+1)*sizeof(ITEM*)); - for (int i = 0; i < todo_length; i++) { - item_list[i] = new_item(todo_list[i], ""); - set_item_userptr(item_list[i], on_select); - } - item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason? + // this is temp + #define MENU_WIDTH 40 - todo_menu = new_menu(item_list); + Menu** menu_list; - getmaxyx(win, wheight, wwidth); - set_menu_win(todo_menu, win); - set_menu_sub(todo_menu, derwin(win, wheight-2, wwidth-2, 1, 2)); - set_menu_mark(todo_menu, ""); - set_menu_spacing(todo_menu, 1, 2, 1); - set_menu_fore(todo_menu, COLOR_PAIR(SELECTED_COLOR)); - set_menu_back(todo_menu, COLOR_PAIR(NON_SELECTED_COLOR)); + menu_list = malloc(todolist_length*sizeof(Menu*)); - box(win, 0, 0); //temp + for (int i = 0; i < todolist_length; i++) { - return todo_menu; -} + /* read from parsed */ + TodoList* todo_list = board->todolist_list[i]; + MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count); -void -on_select(char *item) -{ - printf("lol"); -} + Menu* new_menu = create_menu(todo_list->list_name, item_list); -void -free_todo(char** todo_list, int todo_length) -{ - // probably check if list is too short or too long - for (int i = 0; i < todo_length; i++) { - free(todo_list[i]); + /* 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; } - free(todo_list); + + return menu_list; }