From: Daniel Liu Date: Tue, 16 Feb 2021 03:28:02 +0000 (-0500) Subject: writing board X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=afa94c9493bdb91807c1dc2b4e1ffddbe2df1fd6;p=taskasaur.git writing board --- diff --git a/headers/menu.h b/headers/menu.h index d1dc51e..c11fbdd 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -25,18 +25,22 @@ typedef enum { typedef struct Menu Menu; typedef struct MenuItem MenuItem; -extern MenuItem* create_menuitem(char* contents); +extern MenuItem* create_menuitem(char* title); extern Menu* create_menu(char* menu_name, MenuItem** item_list); + extern WINDOW* get_menu_win(Menu* menu); -extern int set_menu_win(Menu* menu, WINDOW* win); extern WINDOW* get_menu_subwin(Menu* menu); extern MenuItem* get_menu_item(Menu* menu, int index); -extern int set_menu_focus(Menu* menu, bool focus); extern int get_selected_item(Menu* menu); -extern int set_selected_item(Menu* menu, int selected_item); extern int get_menu_length(Menu* menu); extern char* get_menu_name(Menu* menu); +extern char* get_menuitem_title(MenuItem* menuitem); +extern char* get_menuitem_descrip(MenuItem* menuitem); + +extern int set_menu_win(Menu* menu, WINDOW* win); +extern int set_selected_item(Menu* menu, int selected_item); +extern int set_menu_focus(Menu* menu, bool focus); extern int insert_item(Menu* menu, MenuItem* menuitem, int index); extern int delete_item(Menu* menu, int index); diff --git a/headers/parser.h b/headers/parser.h index 4506364..e924f3e 100644 --- a/headers/parser.h +++ b/headers/parser.h @@ -8,6 +8,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + typedef enum SubTaskState { SubTaskState_todo, SubTaskState_done @@ -39,8 +43,11 @@ typedef struct Board { } Board; extern Board* begin_parse(char* board_path); -extern int begin_write(Board* Board); - +extern int begin_write(char* board_path, Board* board); extern void log_todo(Board* board); +#ifdef __cplusplus +} +#endif + #endif diff --git a/menu.c b/menu.c index 3067b16..d66ee07 100644 --- a/menu.c +++ b/menu.c @@ -17,7 +17,7 @@ #define MAX_CONTENTS_LENGTH 256 typedef struct MenuItem { - char* contents; + char* title; char* description; } MenuItem; @@ -44,12 +44,13 @@ MenuItem* create_blank_menuitem(void); MenuItem* -create_menuitem(char* contents) +create_menuitem(char* title) { MenuItem* new_menuitem; new_menuitem = malloc(sizeof(MenuItem)); - new_menuitem->contents = contents; + new_menuitem->title = title; + new_menuitem->description = 0; //TEMP FOR NOW return new_menuitem; } @@ -77,34 +78,13 @@ create_menu(char* menu_name, MenuItem** item_list) return new_menu; } +/* getters */ WINDOW* get_menu_win(Menu* menu) { return menu->menu_win; } -int -set_menu_win(Menu* menu, WINDOW* win) -{ - int height, width; - - menu->menu_win = win; - getmaxyx(menu->menu_win, height, width); - - /* create a subwin (also prob free old subwin?) */ - menu->max_height = height-MENU_PAD_TOP-MENU_PAD_BOTTOM; - menu->max_width = width-MENU_PAD_LEFT-MENU_PAD_RIGHT; - menu->sub_win = derwin( - menu->menu_win, - menu->max_height, - menu->max_width, - MENU_PAD_TOP, - MENU_PAD_LEFT - ); - - return 0; -} - WINDOW* get_menu_subwin(Menu* menu) { @@ -120,43 +100,80 @@ get_menu_item(Menu* menu, int index) } int -set_menu_focus(Menu* menu, bool focus) +get_selected_item(Menu* menu) { - menu->focused = focus; - - return 0; + return menu->selected_item; } int -get_selected_item(Menu* menu) +get_menu_length(Menu* menu) { - return menu->selected_item; + return menu->menu_length; +} + +char* +get_menu_name(Menu* menu) +{ + return menu->menu_name; +} + +char* +get_menuitem_title(MenuItem* menuitem) +{ + return menuitem->title; +} + +char* +get_menuitem_descrip(MenuItem* menuitem) +{ + return menuitem->description; } + +/* setters */ int -set_selected_item(Menu* menu, int selected_item) +set_menu_win(Menu* menu, WINDOW* win) { - menu->selected_item = selected_item; + int height, width; + + menu->menu_win = win; + getmaxyx(menu->menu_win, height, width); + + /* create a subwin (also prob free old subwin?) */ + menu->max_height = height-MENU_PAD_TOP-MENU_PAD_BOTTOM; + menu->max_width = width-MENU_PAD_LEFT-MENU_PAD_RIGHT; + menu->sub_win = derwin( + menu->menu_win, + menu->max_height, + menu->max_width, + MENU_PAD_TOP, + MENU_PAD_LEFT + ); return 0; } int -get_menu_length(Menu* menu) +set_selected_item(Menu* menu, int selected_item) { - return menu->menu_length; + menu->selected_item = selected_item; + + return 0; } -char* -get_menu_name(Menu* menu) +int +set_menu_focus(Menu* menu, bool focus) { - return menu->menu_name; + menu->focused = focus; + + return 0; } + int swap_item(Menu* menu, int src_index, int dest_index) { - ar_swap_item(menu->menu_items, src_index, dest_index); + ar_swap_item((void**)menu->menu_items, src_index, dest_index); return 0; } @@ -219,7 +236,7 @@ menu_insert_mode(Menu* menu, int insert_index) curs_on(); /* move cursor to right spot */ - ungetstr(menu->menu_items[insert_index]->contents); + ungetstr(menu->menu_items[insert_index]->title); mvwgetnstr(menu->sub_win, insert_index, // account for wrap later too 0, @@ -230,7 +247,7 @@ menu_insert_mode(Menu* menu, int insert_index) /* copy out */ new_contents = strdup(temp); - menu->menu_items[insert_index]->contents = new_contents; + menu->menu_items[insert_index]->title = new_contents; /* delete if empty - maybe move this to a cleanup stage */ if (strlen(new_contents) == 0) { @@ -325,7 +342,7 @@ render_menu(Menu* menu) char* wrapped_text; /* wrap text by inserting newlines (maxwidth-1 for newline char)*/ - wrapped_text = wrap_text(menu->menu_items[i]->contents, menu->max_width-1, &wrapped_lines); + wrapped_text = wrap_text(menu->menu_items[i]->title, menu->max_width-1, &wrapped_lines); /* color selected item */ wattron(menu->sub_win, COLOR_PAIR( diff --git a/parser.c b/parser.c index 326d3c7..89c1ec0 100644 --- a/parser.c +++ b/parser.c @@ -44,35 +44,7 @@ const MD_PARSER parser = { }; -void -log_todo(Board* board) -{ - 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); - } - } - } -} - +/* reading */ Board* begin_parse(char* board_path) { @@ -112,7 +84,7 @@ read_file(char* file_name, long* size) file = fopen(file_name, "r"); if (file == NULL) { - printf("Something went wrong opening file\n"); + perror("read_file > fopen"); return NULL; } @@ -189,20 +161,15 @@ exit_todoitem(State* state) { #define st state->cur_todolist - TodoList* todolist; - TodoItem** item_list; - if (state->cur_todoitem == NULL) return; /* append current item to todo list */ - todolist = state->cur_todolist; - item_list = todolist->item_list; - st->item_count += 1; st->item_list = realloc(st->item_list, st->item_count*sizeof(TodoItem*)); st->item_list[st->item_count-1] = state->cur_todoitem; state->cur_todoitem = NULL; + #undef st } void @@ -350,3 +317,67 @@ syntax(void) { return; } + +/* writing */ +int +begin_write(char* board_path, Board* board) +{ // TODO, make a backup file of board before write in case it crashes + + FILE* file; + + file = fopen(board_path, "w+"); + if (file == NULL) { + perror("begin_write > fopen"); + return -1; + } + + for (int i = 0; i < board->todolist_count; i++) { + TodoList* cur_todolist = board->todolist_list[i]; + + fprintf(file, "## %s\n", cur_todolist->list_name); + + for (int j = 0; j < cur_todolist->item_count; j++) { + TodoItem* cur_todoitem = cur_todolist->item_list[j]; + + fprintf(file, "### %s\n", cur_todoitem->item_name); + + // write the other fields later!!! + + } + + } + + fclose(file); + + return 0; +} + +/* others */ +void +log_todo(Board* board) +{ + 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); + } + } + } +} diff --git a/taskasaur.c b/taskasaur.c index a679bfb..353ca5d 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -169,6 +169,18 @@ main(int argc, char** argv) break; case BINDING_WRITE: + { + Board* writeboard; + + writeboard = boardmenu_to_board(boardmenu); + + /* mvprintw(30, 5, "%s", writeboard->todolist_list[0]->list_name); */ + begin_write(boardfile_name, writeboard); + + /* now free the writeborad */ + + } + break; } @@ -176,7 +188,6 @@ main(int argc, char** argv) render_menu(boardmenu->menu_list[i]); } - } exit_tscurses(); @@ -199,9 +210,12 @@ create_board_menu(Board* board) Board* boardmenu_to_board(BoardMenu* boardmenu) -{ - Board newboard = malloc(sizeof(Board)); - TodoList** todolist_list = malloc(sizeof(TodoList*)); +{ // STRINGS are sharing the same address as the one in MENU + // and MENUITEM, this may break something if u free this board + // consider copying the string + + Board* newboard = malloc(sizeof(Board)); + TodoList** new_todolist_list = malloc(sizeof(TodoList*)); for (int i = 0; i < boardmenu->menu_count; i++) { Menu* curmenu = boardmenu->menu_list[i]; @@ -211,15 +225,28 @@ boardmenu_to_board(BoardMenu* boardmenu) new_todolist->list_name = get_menu_name(curmenu); new_todolist->item_count = get_menu_length(curmenu); - /* for (int j = 0; j < get_menu_length(curmenu); j++) { */ + for (int j = 0; j < get_menu_length(curmenu); j++) { + MenuItem* curmenuitem = get_menu_item(curmenu, j); + + TodoItem* new_todoitem = malloc(sizeof(TodoItem)); + + new_todoitem->item_name = get_menuitem_title(curmenuitem); + new_todoitem->description = get_menuitem_descrip(curmenuitem); + new_todoitem->due = 0; //TEMP! + new_todoitem->subtask_list = 0; //TEMP! + new_todoitem->subtask_count = 0; //TEMP! + + item_list[j] = new_todoitem; - /* } */ + } new_todolist->item_list = item_list; + + new_todolist_list[i] = new_todolist; } - newboard->todolist_list = todolist_list; + newboard->todolist_list = new_todolist_list; newboard->todolist_count = boardmenu->menu_count; return newboard; @@ -317,7 +344,7 @@ swap_menu(BoardMenu* boardmenu, int src_index, int dest_index) clear(); /* swap in array */ - ar_swap_item(boardmenu->menu_list, src_index, dest_index); + ar_swap_item((void*)boardmenu->menu_list, src_index, dest_index); return 0; }