2 #include "headers/parser.h"
3 #include "headers/render.h"
4 #include "headers/menu.h"
5 #include "headers/utils.h"
11 typedef struct BoardMenu {
17 BoardMenu* create_board_menu(Board* board);
18 Board* boardmenu_to_board(BoardMenu* boardmenu);
19 int set_selected_menu(BoardMenu* boardmenu, int index);
21 MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length);
22 Menu** make_menus(Board* board, int todolist_length);
23 int swap_menu(BoardMenu* boardmenu, int src_index, int dest_index);
26 main(int argc, char** argv)
28 char* boardfile_name = "test_board.md";
29 printf("%c]0;%s - %s%c", '\033', "taskasaur", boardfile_name, '\007'); // need to reset after program exits
31 /* read from todo file */
33 board = begin_parse(boardfile_name);
34 /* log_todo(board); */
40 boardmenu = create_board_menu(board);
43 for (int i = 0; i < boardmenu->menu_count; i++) {
44 render_menu(boardmenu->menu_list[i]);
48 while ((ch = getch()) != BINDING_QUIT) {
51 active_menu = boardmenu->menu_list[boardmenu->selected];
55 case BINDING_SCROLL_UP:
56 menu_driver(active_menu, MENU_UP);
58 case BINDING_SCROLL_DOWN:
59 menu_driver(active_menu, MENU_DOWN);
61 case BINDING_SCROLL_LEFT:
62 if (boardmenu->selected-1 < 0) break;
63 set_selected_menu(boardmenu, boardmenu->selected-1);
65 case BINDING_SCROLL_RIGHT:
66 if (boardmenu->selected+1 > boardmenu->menu_count-1) break;
67 set_selected_menu(boardmenu, boardmenu->selected+1);
69 case BINDING_JUMP_TOP:
70 menu_driver(active_menu, MENU_TOP);
72 case BINDING_JUMP_BOTTOM:
73 menu_driver(active_menu, MENU_BOTTOM);
75 case BINDING_MOVE_ITEM_UP:
76 menu_driver(active_menu, MENU_MOVE_UP);
78 case BINDING_MOVE_ITEM_DOWN:
79 menu_driver(active_menu, MENU_MOVE_DOWN);
81 case BINDING_MOVE_ITEM_LEFT:
82 if (boardmenu->selected-1 < 0) break;
87 from_menu = boardmenu->menu_list[boardmenu->selected],
88 to_menu = boardmenu->menu_list[boardmenu->selected-1],
94 get_selected_item(from_menu)
97 get_selected_item(from_menu),
98 get_menu_length(to_menu)
103 get_selected_item(from_menu)
105 set_selected_menu(boardmenu, boardmenu->selected-1);
109 case BINDING_MOVE_ITEM_RIGHT:
110 if (boardmenu->selected >= boardmenu->menu_count-1) break;
111 // this is legit cpy paste please fix this
116 from_menu = boardmenu->menu_list[boardmenu->selected],
117 to_menu = boardmenu->menu_list[boardmenu->selected+1],
123 get_selected_item(from_menu)
126 get_selected_item(from_menu),
127 get_menu_length(to_menu)
132 get_selected_item(from_menu)
134 set_selected_menu(boardmenu, boardmenu->selected+1);
138 case BINDING_DELETE_ITEM:
139 menu_driver(active_menu, MENU_DELETE);
141 case BINDING_APPEND_ITEM:
142 menu_driver(active_menu, MENU_APPEND);
144 case BINDING_INSERT_ABOVE:
145 menu_driver(active_menu, MENU_INSERT_ABOVE);
147 case BINDING_INSERT_BELOW:
148 menu_driver(active_menu, MENU_INSERT_BELOW);
150 /* case BINDING_MOVE_MENU_LEFT: */
151 /* if (boardmenu->selected-1 < 0) break; */
153 /* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected-1); */
154 /* boardmenu->selected -= 1; */
155 /* set_selected_menu(boardmenu, boardmenu->selected); */
158 /* case BINDING_MOVE_MENU_RIGHT: */
159 /* if (boardmenu->selected >= boardmenu->menu_count-1) break; */
160 /* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected+1); */
161 /* boardmenu->selected += 1; */
162 /* set_selected_menu(boardmenu, boardmenu->selected); */
165 case BINDING_EDIT_ITEM:
166 menu_driver(active_menu, MENU_EDIT);
175 for (int i = 0; i < boardmenu->menu_count; i++) {
176 render_menu(boardmenu->menu_list[i]);
187 create_board_menu(Board* board)
189 BoardMenu* new_boardmenu;
191 new_boardmenu = malloc(sizeof(BoardMenu));
193 new_boardmenu->menu_list = make_menus(board, board->todolist_count);
194 new_boardmenu->menu_count = board->todolist_count;
195 new_boardmenu->selected = 0;
197 return new_boardmenu;
201 boardmenu_to_board(BoardMenu* boardmenu)
203 Board newboard = malloc(sizeof(Board));
204 TodoList** todolist_list = malloc(sizeof(TodoList*));
206 for (int i = 0; i < boardmenu->menu_count; i++) {
207 Menu* curmenu = boardmenu->menu_list[i];
209 TodoList* new_todolist = malloc(sizeof(TodoList));
210 TodoItem** item_list = malloc(sizeof(TodoItem*));
211 new_todolist->list_name = get_menu_name(curmenu);
212 new_todolist->item_count = get_menu_length(curmenu);
214 /* for (int j = 0; j < get_menu_length(curmenu); j++) { */
218 new_todolist->item_list = item_list;
222 newboard->todolist_list = todolist_list;
223 newboard->todolist_count = boardmenu->menu_count;
230 set_selected_menu(BoardMenu* boardmenu, int index)
236 old_menu = boardmenu->menu_list[boardmenu->selected];
237 new_menu = boardmenu->menu_list[index];
239 set_menu_focus(old_menu, false);
240 set_menu_focus(new_menu, true);
242 /* also try to jump to a similar position if possible */
243 /* rn theres a bug if old menu is empty */
244 new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
245 set_selected_item(new_menu, new_pos);
247 boardmenu->selected = index;
253 todolist_to_menuitem(TodoItem** item_list, int list_length)
257 items = malloc((list_length+1)*sizeof(MenuItem*));
258 for (int i = 0; i < list_length; i++) {
259 items[i] = create_menuitem(item_list[i]->item_name);
262 items[list_length] = 0; //null terminate
267 make_menus(Board* board, int todolist_length)
272 menu_list = malloc(todolist_length*sizeof(Menu*));
274 for (int i = 0; i < todolist_length; i++) {
276 /* read from parsed */
277 TodoList* todo_list = board->todolist_list[i];
278 MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count);
280 Menu* new_menu = create_menu(todo_list->list_name, item_list);
283 WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
286 /* some menu settings */
287 set_menu_win(new_menu, win);
288 set_menu_focus(new_menu, i == 0); // make first win focused
294 menu_list[i] = new_menu;
301 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
303 /* reposition menus */
304 mvwin(get_menu_win(boardmenu->menu_list[src_index]),
305 1, 1+MENU_WIDTH*dest_index
307 mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
308 1, 1+MENU_WIDTH*src_index
311 wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
312 wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
313 /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
314 /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
315 /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
316 /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
320 ar_swap_item(boardmenu->menu_list, src_index, dest_index);