X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=taskasaur.c;h=22e2f2a6200be7d0f5091e8d02d0071f596fcebf;hb=99cc6e19069115197e8101c1ed585fe0ae5de405;hp=119411e5e790a3e4c715f5f6da7c12970a583a2b;hpb=f270512184ab1c41eee46c76a149b7cbddb1b131;p=taskasaur.git diff --git a/taskasaur.c b/taskasaur.c index 119411e..22e2f2a 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -1,201 +1,86 @@ -#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); +MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length); -WINDOW* create_win(int height, int width, int y, int x); -MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length); +int +main(int argc, char** argv) +{ -void free_todo(char** todo_list, int todo_length); + /* read from todo file */ + Board* board; + board = begin_parse("test_board.md"); + log_todo(board); + + /* init curses */ + init_tscurses(); + + TodoList* todo_list = board->todolist_list[0]; + MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count); + Menu* menu = create_menu(todo_list->list_name, item_list); + WINDOW* win = newwin(20, 40, 5, 5); + set_menu_win(menu, win); + set_menu_focus(menu, true); + box(win, 0, 0); + refresh(); + wrefresh(win); -#include "config.h" + render_menu(menu); -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; - } + char ch; + while ((ch = getch()) != BINDING_QUIT) { - /* for (int i = 0; i < todo_length; i++) { */ - /* printf("%p\n", todos[i]); */ - /* printf(todos[i]); */ - /* } */ - /* return 0; */ - - // start ncurses - initscr(); - cbreak(); - noecho(); - curs_set(0); - start_color(); - - 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); - wrefresh(todo_win); - - while ((ch = getch()) != 'q') { - switch (ch) { - case 'j': - menu_driver(todo_menu, REQ_DOWN_ITEM); + + case BINDING_SCROLL_UP: + menu_driver(menu, MENU_UP); + break; + case BINDING_SCROLL_DOWN: + menu_driver(menu, MENU_DOWN); break; - case 'k': - menu_driver(todo_menu, REQ_UP_ITEM); + case BINDING_JUMP_TOP: + menu_driver(menu, MENU_TOP); break; - case 'G': // try to implement gg too - menu_driver(todo_menu, REQ_LAST_ITEM); + case BINDING_JUMP_BOTTOM: + menu_driver(menu, MENU_BOTTOM); + break; + case BINDING_MOVE_ITEM_UP: + menu_driver(menu, MENU_MOVE_UP); + break; + case BINDING_MOVE_ITEM_DOWN: + menu_driver(menu, MENU_MOVE_DOWN); + break; + case BINDING_DELETE_ITEM: + menu_driver(menu, MENU_DELETE); break; - } - wrefresh(todo_win); - } + } + render_menu(menu); - endwin(); + } - /* Free mem */ - unpost(todo_menu); - free_todo(todos, todo_length); - return 0; + exit_tscurses(); + return 0; } -void -winch_handler(int sig) +MenuItem** +todolist_to_menuitem(TodoItem** item_list, int list_length) { - endwin(); - refresh(); -} + MenuItem** items; -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; - - out_arr = NULL; - out_len = 0; - lineptr = NULL; - len = 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; - - lineptr = NULL; + items = malloc((list_length+1)*sizeof(MenuItem*)); + for (int i = 0; i < list_length; i++) { + items[i] = create_menuitem(item_list[i]->item_name); } - - *length = out_len; - return out_arr; -} -WINDOW* -create_win(int height, int width, int y, int x) -{ - WINDOW* new_win = newwin(height, width, y, x); - wrefresh(new_win); - return new_win; + items[list_length] = 0; //null terminate + return items; } -MENU* -create_todo_menu(WINDOW* win, char** todo_list, int todo_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], ""); - } - item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason? - todo_menu = new_menu(item_list); - getmaxyx(stdscr, wheight, wwidth); - set_menu_win(todo_menu, win); - set_menu_sub(todo_menu, derwin(win, wheight, wwidth, 0, 0)); - - box(win, 0, 0); //temp - - return todo_menu; -} - -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]); - } - free(todo_list); -}