2 #include "headers/render.h"
6 int init_tscolors(void);
7 int create_todowin(void);
18 /* need to error check this */
19 if (has_colors() == FALSE) {
20 fprintf(stderr, "Your terminal does not support color.\n");
22 /* maybe just return 1 */
23 /* exit_tscurses(); */
44 init_pair(TS_SELECTED, selected_color, COLOR_BLACK);
45 init_pair(TS_NONSELECTED, non_selected_color, COLOR_BLACK);
46 init_pair(TS_MENU_SELECTED, menu_selected_color, COLOR_BLACK);
47 init_pair(TS_MENU_NONSELECTED, menu_non_selected_color, COLOR_BLACK);
78 create_board_menu(Board* board)
80 BoardMenu* new_boardmenu;
82 new_boardmenu = malloc(sizeof(BoardMenu));
84 new_boardmenu->menu_list = make_menus(board, board->todolist_count);
85 new_boardmenu->menu_count = board->todolist_count;
86 new_boardmenu->selected = 0;
92 make_menus(Board* board, int todolist_length)
97 menu_list = malloc(todolist_length*sizeof(Menu*));
99 for (int i = 0; i < todolist_length; i++) {
101 /* read from parsed */
102 TodoList* todo_list = board->todolist_list[i];
103 MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count);
105 Menu* new_menu = create_menu(todo_list->list_name, item_list);
108 WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
111 /* some menu settings */
112 set_menu_win(new_menu, win);
113 set_menu_focus(new_menu, i == 0); // make first win focused
119 menu_list[i] = new_menu;
126 todolist_to_menuitem(TodoItem** item_list, int list_length)
130 items = malloc((list_length+1)*sizeof(MenuItem*));
131 for (int i = 0; i < list_length; i++) {
132 MenuItem* new_menuitem;
133 new_menuitem = create_menuitem(item_list[i]->item_name);
134 /* using same struct, careful if it gets freed */
135 set_menuitem_userdata(new_menuitem, item_list[i]);
137 items[i] = new_menuitem;
140 items[list_length] = 0; //null terminate
145 boardmenu_to_board(BoardMenu* boardmenu)
146 { // STRINGS are sharing the same address as the one in MENU
147 // and MENUITEM, this may break something if u free this board
148 // consider copying the string
150 Board* newboard = malloc(sizeof(Board));
151 TodoList** new_todolist_list = malloc(sizeof(TodoList*));
153 for (int i = 0; i < boardmenu->menu_count; i++) {
154 Menu* curmenu = boardmenu->menu_list[i];
156 TodoList* new_todolist = malloc(sizeof(TodoList));
157 TodoItem** new_item_list = malloc(sizeof(TodoItem*));
158 new_todolist->list_name = strdup(get_menu_name(curmenu));
159 new_todolist->item_count = get_menu_length(curmenu);
161 for (int j = 0; j < get_menu_length(curmenu); j++) {
162 MenuItem* curmenuitem = get_menu_item(curmenu, j);
163 TodoItem* itemdata = get_menuitem_userdata(curmenuitem);
165 TodoItem* new_todoitem = malloc(sizeof(TodoItem));
166 SubTask** new_subtask_list = malloc(itemdata->subtask_count*sizeof(SubTask*));
168 new_todoitem->item_name = strdup(get_menuitem_title(curmenuitem));
169 new_todoitem->description = strdup(itemdata->description);
170 new_todoitem->due = strdup(itemdata->due);
171 new_todoitem->subtask_count = itemdata->subtask_count;
172 for (int k = 0; k < itemdata->subtask_count; k++) {
173 SubTask* new_subtask = malloc(sizeof(SubTask));
175 new_subtask->subtask_name = itemdata->subtask_list[k]->subtask_name;
176 new_subtask->done = itemdata->subtask_list[k]->done;
178 new_subtask_list[k] = new_subtask;
181 new_todoitem->subtask_list = new_subtask_list;
183 new_item_list[j] = new_todoitem;
186 new_todolist->item_list = new_item_list;
187 new_todolist_list[i] = new_todolist;
191 newboard->board_name = strdup("");
192 newboard->todolist_list = new_todolist_list;
193 newboard->todolist_count = boardmenu->menu_count;
199 set_selected_menu(BoardMenu* boardmenu, int index)
205 old_menu = boardmenu->menu_list[boardmenu->selected];
206 new_menu = boardmenu->menu_list[index];
208 set_menu_focus(old_menu, false);
209 set_menu_focus(new_menu, true);
211 /* also try to jump to a similar position if possible */
212 /* rn theres a bug if old menu is empty */
213 new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
214 if (new_pos < 0) new_pos = 0;
215 set_selected_item(new_menu, new_pos);
217 boardmenu->selected = index;
223 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
225 /* reposition menus */
226 mvwin(get_menu_win(boardmenu->menu_list[src_index]),
227 1, 1+MENU_WIDTH*dest_index
229 mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
230 1, 1+MENU_WIDTH*src_index
233 wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
234 wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
235 /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
236 /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
237 /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
238 /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
242 ar_swap_item((void*)boardmenu->menu_list, src_index, dest_index);
249 update_menuitem_descrip(MenuItem* menuitem)
250 { /* need to do something about colored text */
255 item_data = (TodoItem*)get_menuitem_userdata(menuitem);
256 new_descrip = strdup("");
258 if (strlen(item_data->description) > 0) {
259 /* strcat(new_descrip, "☰ "); */
260 strcat(new_descrip, "# ");
262 if (strlen(item_data->due) > 0) {
263 strcat(new_descrip, item_data->due);
264 strcat(new_descrip, " ");
266 if (item_data->subtask_count > 0) {
268 int tasks_complete = 0;
269 for (int i = 0; i < item_data->subtask_count; i++) {
270 if (item_data->subtask_list[i]->done == SubTaskState_done) {
275 /* [, # done, /, # total, ], null */
276 char subtask_done[4]; // assume there wont be more than 999 subtasks (possibly danger?)
277 snprintf(subtask_done, 4, "%d", tasks_complete);
278 int substask_len = 1+item_data->subtask_count+1+strlen(subtask_done)+1+1;
279 char subtask_text[substask_len];
280 sprintf(subtask_text, "[%s/%d]", subtask_done, item_data->subtask_count);
281 strcat(new_descrip, subtask_text);
284 /* free old string */
285 if (strlen(new_descrip) > 0) {
286 free(get_menuitem_descrip(menuitem));
287 set_menuitem_descrip(menuitem, new_descrip);
304 // ignore null character (it's fine even if strlen = 0)
305 for (int i = strlen(str)-1; i >= 0; i--) {