X-Git-Url: https://git.danieliu.xyz/?p=taskasaur.git;a=blobdiff_plain;f=taskasaur.c;h=dbc3d31229212f74b7ce37df0b30c9903b5b1880;hp=ce93ab746ce4de1c68dee0cbb88d18d06ffdd13d;hb=6eb3a028f4d79ee91e9d94e9c02932f280cd0ac8;hpb=c263062e7183d2c56149c5188a70acd0ff5cb513 diff --git a/taskasaur.c b/taskasaur.c index ce93ab7..dbc3d31 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -1,204 +1,204 @@ -#include -#include -#include -#include -#include -#include -#include - -void winch_handler(int sig); - -char** read_todo(FILE* file, int* length); - -WINDOW* create_win(int height, int width, int y, int x); -MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length); - -void free_todo(char** todo_list, int todo_length); +#include "headers/parser.h" +#include "headers/render.h" +#include "headers/menu.h" +#include "headers/utils.h" #include "config.h" -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); +void render_step(BoardMenu* boardmenu); +void save_to_file(char* filepath, BoardMenu* boardmenu); - 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; +int +main(int argc, char** argv) +{ + char* boardfile_name = "test_board.md"; + printf("%c]0;%s - %s%c", '\033', "taskasaur", boardfile_name, '\007'); // need to reset after program exits - break; + /* read from todo file */ + Board* board; + board = begin_parse(boardfile_name); + /* log_todo(board); */ - case -1: - case '?': - printf("Help string\n"); - return 2; - } + /* init curses */ + init_tscurses(); - /* for (int i = 0; i < todo_length; i++) { */ - /* printf("%p\n", todos[i]); */ - /* printf(todos[i]); */ - /* } */ - /* return 0; */ + BoardMenu* boardmenu; + boardmenu = create_board_menu(board); - // start ncurses - initscr(); - cbreak(); - noecho(); - curs_set(0); - start_color(); + /* need to render before user presses anything */ + render_step(boardmenu); - getmaxyx(stdscr, height, width); + int ch; + while ((ch = getch()) != BINDING_QUIT) { - todo_win = create_win(20, 40, 5, 5); + Menu* active_menu; + active_menu = boardmenu->menu_list[boardmenu->selected]; - todo_menu = create_todo_menu(todo_win, todos, todo_length); - post_menu(todo_menu); - refresh(); - wrefresh(todo_win); - - while ((ch = getch()) != 'q') { - switch (ch) { - case 'j': - menu_driver(todo_menu, REQ_DOWN_ITEM); + + case BINDING_SCROLL_UP: + menu_driver(active_menu, MENU_UP); break; - case 'k': - menu_driver(todo_menu, REQ_UP_ITEM); + case BINDING_SCROLL_DOWN: + menu_driver(active_menu, MENU_DOWN); break; - case 'G': // try to implement gg too - menu_driver(todo_menu, REQ_LAST_ITEM); + case BINDING_SCROLL_LEFT: + if (boardmenu->selected-1 < 0) break; + set_selected_menu(boardmenu, boardmenu->selected-1); break; - } - wrefresh(todo_win); - - /* wrefresh(todo_win); */ - } - - endwin(); - - /* Free mem */ - unpost_menu(todo_menu); - free_todo(todos, todo_length); - - return 0; -} - -void -winch_handler(int sig) -{ - endwin(); - refresh(); -} - -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; + 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); + } - out_arr = NULL; - out_len = 0; - lineptr = NULL; - len = 0; + 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; + + 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); + } - while ((nread = getline(&lineptr, &len, file)) != -1) { - out_len++; - out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? + 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; + case BINDING_SELECT: + break; + case BINDING_WRITE: + save_to_file(boardfile_name, boardmenu); + break; + case KEY_RESIZE: + /* ; */ + /* int y, x; */ + /* char out[10]; */ + /* getmaxyx(stdscr, y, x); */ + /* sprintf(out, "%d,%d", y, x); */ + + /* mvprintw(20, 20, out); */ + /* resize_term(y, x); */ + break; + } - lineptr[strcspn(lineptr, "\n")] = 0; // remove newline - out_arr[out_len-1] = lineptr; + render_step(boardmenu); - lineptr = NULL; } - *length = out_len; - return out_arr; -} + /* save on exit - this causes weird stuff to happen, maybe it's not given enough time to write before program exits? */ + /* save_to_file(boardfile_name, boardmenu); */ -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; + exit_tscurses(); + return 0; } -MENU* -create_todo_menu(WINDOW* win, char** todo_list, int todo_length) +void +render_step(BoardMenu* boardmenu) { - 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); + for (int i = 0; i < boardmenu->menu_count; i++) { - getmaxyx(win, wheight, wwidth); - set_menu_win(todo_menu, win); - set_menu_sub(todo_menu, derwin(win, wheight-2, wwidth-2, 1, 1)); - set_menu_mark(todo_menu, ""); + Menu* curmenu = boardmenu->menu_list[i]; - box(win, 0, 0); //temp + /* update the descriptions - maybe not do this here */ + for (int j = 0; j < get_menu_length(curmenu); j++) { + update_menuitem_descrip(get_menu_item(curmenu, j)); + } - return todo_menu; + render_menu(curmenu); + } } void -free_todo(char** todo_list, int todo_length) +save_to_file(char* filepath, BoardMenu* boardmenu) { - // 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); + Board* writeboard; + writeboard = boardmenu_to_board(boardmenu); + + begin_write(filepath, writeboard); + free_board(writeboard); }