From: Daniel Liu Date: Thu, 18 Feb 2021 03:22:12 +0000 (-0500) Subject: render description X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=b93f9808ed43d0b1ef875aaaa4047a9ce8123eda;p=taskasaur.git render description --- diff --git a/headers/menu.h b/headers/menu.h index c11fbdd..39fe8d5 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -35,12 +35,17 @@ extern MenuItem* get_menu_item(Menu* menu, int index); extern int get_selected_item(Menu* menu); extern int get_menu_length(Menu* menu); extern char* get_menu_name(Menu* menu); +extern void* get_menu_userdata(Menu* menu); extern char* get_menuitem_title(MenuItem* menuitem); extern char* get_menuitem_descrip(MenuItem* menuitem); +extern void* get_menuitem_userdata(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 set_menu_userdata(Menu* menu, void* userdata); +extern int set_menuitem_descrip(MenuItem* menuitem, char* descrip); +extern int set_menuitem_userdata(MenuItem* menuitem, void* userdata); extern int insert_item(Menu* menu, MenuItem* menuitem, int index); extern int delete_item(Menu* menu, int index); diff --git a/headers/render.h b/headers/render.h index a0fa083..e54a226 100644 --- a/headers/render.h +++ b/headers/render.h @@ -39,6 +39,9 @@ extern Board* boardmenu_to_board(BoardMenu* boardmenu); extern int set_selected_menu(BoardMenu* boardmenu, int index); extern int swap_menu(BoardMenu* boardmenu, int src_index, int dest_index); +/* menuitem stuff */ +extern int update_menuitem_descrip(MenuItem* menuitem); + /* popup menu for menu item */ extern WINDOW* create_popup_win(); diff --git a/menu.c b/menu.c index 96ad1e5..0ee76a5 100644 --- a/menu.c +++ b/menu.c @@ -19,7 +19,7 @@ typedef struct MenuItem { char* title; char* description; - void* user_data; + void* userdata; } MenuItem; typedef struct Menu { @@ -33,7 +33,7 @@ typedef struct Menu { WINDOW* sub_win; int max_height; int max_width; - void* user_data; + void* userdata; } Menu; int swap_item(Menu* menu, int src_index, int dest_index); @@ -119,6 +119,12 @@ get_menu_name(Menu* menu) return menu->menu_name; } +void* +get_menu_userdata(Menu* menu) +{ + return menu->userdata; +} + char* get_menuitem_title(MenuItem* menuitem) { @@ -131,6 +137,12 @@ get_menuitem_descrip(MenuItem* menuitem) return menuitem->description; } +void* +get_menuitem_userdata(MenuItem* menuitem) +{ + return menuitem->userdata; +} + /* setters */ int @@ -159,7 +171,6 @@ int set_selected_item(Menu* menu, int selected_item) { menu->selected_item = selected_item; - return 0; } @@ -167,7 +178,27 @@ int set_menu_focus(Menu* menu, bool focus) { menu->focused = focus; - + return 0; +} + +int +set_menu_userdata(Menu* menu, void* userdata) +{ + menu->userdata = userdata; + return 0; +} + +int +set_menuitem_descrip(MenuItem* menuitem, char* descrip) +{ + menuitem->description = descrip; + return 0; +} + +int +set_menuitem_userdata(MenuItem* menuitem, void* userdata) +{ + menuitem->userdata = userdata; return 0; } @@ -339,12 +370,15 @@ render_menu(Menu* menu) /* draw inner menu */ int cur_line = 0; for (int i = 0; i < menu->menu_length-menu->scroll_offset; i++) { - + + MenuItem* curitem; int wrapped_lines; char* wrapped_text; + + curitem = menu->menu_items[i]; /* wrap text by inserting newlines (maxwidth-1 for newline char)*/ - wrapped_text = wrap_text(menu->menu_items[i]->title, menu->max_width-1, &wrapped_lines); + wrapped_text = wrap_text(curitem->title, menu->max_width-1, &wrapped_lines); /* color selected item */ wattron(menu->sub_win, COLOR_PAIR( @@ -357,7 +391,10 @@ render_menu(Menu* menu) cur_line += wrapped_lines; /* display number of items */ - /* if (menu->menu_items[i]->) */ + if (strlen(curitem->description) > 0) { + mvwprintw(menu->sub_win, cur_line, 0, curitem->description); + cur_line += 1; + } free(wrapped_text); diff --git a/parser.c b/parser.c index b752085..bad287a 100644 --- a/parser.c +++ b/parser.c @@ -147,8 +147,8 @@ enter_todoitem(State* state, char* item_name) new_todoitem = malloc(sizeof(TodoItem)); new_todoitem->item_name = item_name; - new_todoitem->description = NULL; - new_todoitem->due = NULL; + new_todoitem->description = strdup(""); + new_todoitem->due = strdup(""); new_todoitem->subtask_list = malloc(0); new_todoitem->subtask_count = 0; @@ -181,7 +181,7 @@ set_description(State* state, char* description) void set_due(State* state, char* due) { - + state->cur_todoitem->due = due; } void @@ -281,7 +281,7 @@ leave_span(MD_SPANTYPE type, void* detail, void* userdata) switch (type) { case MD_SPAN_STRONG: - printf("date, %s\n", state->last_block_text); + set_due(state, state->last_block_text); break; } return 0; diff --git a/render.c b/render.c index 657b8c6..d6a04ff 100644 --- a/render.c +++ b/render.c @@ -129,7 +129,12 @@ todolist_to_menuitem(TodoItem** item_list, int list_length) items = malloc((list_length+1)*sizeof(MenuItem*)); for (int i = 0; i < list_length; i++) { - items[i] = create_menuitem(item_list[i]->item_name); + MenuItem* new_menuitem; + new_menuitem = create_menuitem(item_list[i]->item_name); + /* using same struct, careful if it gets freed */ + set_menuitem_userdata(new_menuitem, item_list[i]); + + items[i] = new_menuitem; } items[list_length] = 0; //null terminate @@ -227,6 +232,42 @@ swap_menu(BoardMenu* boardmenu, int src_index, int dest_index) return 0; } +/* menuitem */ +int +update_menuitem_descrip(MenuItem* menuitem) +{ /* need to do something about colored text */ + + TodoItem* item_data; + char* new_descrip; + + item_data = (TodoItem*)get_menuitem_userdata(menuitem); + new_descrip = strdup(""); + + if (strlen(item_data->description) > 0) { + /* strcat(new_descrip, "☰ "); */ + strcat(new_descrip, "~ "); + } + if (strlen(item_data->due) > 0) { + strcat(new_descrip, item_data->due); + strcat(new_descrip, " "); + } + if (item_data->subtask_count > 0) { + /* [, # done, /, # total, ], null */ + int substask_len = 1+item_data->subtask_count+1+ 1+1; + char subtask_text[substask_len]; + sprintf(subtask_text, "[%d/]", item_data->subtask_count); + strcat(new_descrip, subtask_text); + } + + /* free old string */ + if (strlen(new_descrip) > 0) { + free(get_menuitem_descrip(menuitem)); + set_menuitem_descrip(menuitem, new_descrip); + } + + return 0; +} + /* popup */ WINDOW* create_popup_win() @@ -234,7 +275,7 @@ create_popup_win() return NULL; } - +/* helpers */ int ungetstr(char* str) { diff --git a/taskasaur.c b/taskasaur.c index bdc72f5..cb929e9 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -5,6 +5,8 @@ #include "headers/utils.h" #include "config.h" +void render_step(BoardMenu* boardmenu); + int main(int argc, char** argv) { @@ -22,10 +24,8 @@ main(int argc, char** argv) 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]); - } + /* need to render before user presses anything */ + render_step(boardmenu); char ch; while ((ch = getch()) != BINDING_QUIT) { @@ -163,9 +163,7 @@ main(int argc, char** argv) break; } - for (int i = 0; i < boardmenu->menu_count; i++) { - render_menu(boardmenu->menu_list[i]); - } + render_step(boardmenu); } @@ -173,3 +171,18 @@ main(int argc, char** argv) return 0; } +void +render_step(BoardMenu* boardmenu) +{ + for (int i = 0; i < boardmenu->menu_count; i++) { + + Menu* curmenu = boardmenu->menu_list[i]; + + /* 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)); + } + + render_menu(curmenu); + } +}