work on writing
[taskasaur.git] / taskasaur.c
1
2 #include "headers/parser.h"
3 #include "headers/render.h"
4 #include "headers/menu.h"
5 #include "headers/utils.h"
6 #include "config.h"
7
8 // this is temp
9 #define MENU_WIDTH 40
10
11 typedef struct BoardMenu {
12     Menu** menu_list;
13     int menu_count;
14     int selected;
15 } BoardMenu;
16
17 BoardMenu* create_board_menu(Board* board);
18 Board* boardmenu_to_board(BoardMenu* boardmenu);
19 int set_selected_menu(BoardMenu* boardmenu, int index);
20
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);
24
25 int
26 main(int argc, char** argv)
27 {
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
30
31     /* read from todo file */
32     Board* board;
33     board = begin_parse(boardfile_name);
34     /* log_todo(board); */
35
36     /* init curses */
37     init_tscurses();
38
39     BoardMenu* boardmenu;
40     boardmenu = create_board_menu(board);
41
42     // this is temp
43     for (int i = 0; i < boardmenu->menu_count; i++) {
44         render_menu(boardmenu->menu_list[i]);
45     }
46
47     char ch;
48     while ((ch = getch()) != BINDING_QUIT) {
49
50         Menu* active_menu;
51         active_menu = boardmenu->menu_list[boardmenu->selected];
52
53         switch (ch) {
54
55             case BINDING_SCROLL_UP:
56                 menu_driver(active_menu, MENU_UP);
57                 break;
58             case BINDING_SCROLL_DOWN:
59                 menu_driver(active_menu, MENU_DOWN);
60                 break;
61             case BINDING_SCROLL_LEFT:
62                 if (boardmenu->selected-1 < 0) break;
63                 set_selected_menu(boardmenu, boardmenu->selected-1);
64                 break;
65             case BINDING_SCROLL_RIGHT:
66                 if (boardmenu->selected+1 > boardmenu->menu_count-1) break;
67                 set_selected_menu(boardmenu, boardmenu->selected+1);
68                 break;
69             case BINDING_JUMP_TOP:
70                 menu_driver(active_menu, MENU_TOP);
71                 break;
72             case BINDING_JUMP_BOTTOM:
73                 menu_driver(active_menu, MENU_BOTTOM);
74                 break;
75             case BINDING_MOVE_ITEM_UP:
76                 menu_driver(active_menu, MENU_MOVE_UP);
77                 break;
78             case BINDING_MOVE_ITEM_DOWN:
79                 menu_driver(active_menu, MENU_MOVE_DOWN);
80                 break;
81             case BINDING_MOVE_ITEM_LEFT:
82                 if (boardmenu->selected-1 < 0) break;
83                 {
84                     Menu* from_menu;
85                     Menu* to_menu;
86
87                     from_menu = boardmenu->menu_list[boardmenu->selected],
88                     to_menu = boardmenu->menu_list[boardmenu->selected-1],
89
90                     insert_item(
91                         to_menu,
92                         get_menu_item(
93                             from_menu,
94                             get_selected_item(from_menu)
95                         ),
96                         min(
97                             get_selected_item(from_menu),
98                             get_menu_length(to_menu)
99                         )
100                     );
101                     delete_item(
102                         from_menu,
103                         get_selected_item(from_menu) 
104                     );
105                     set_selected_menu(boardmenu, boardmenu->selected-1);
106                 }
107
108                 break;
109             case BINDING_MOVE_ITEM_RIGHT:
110                 if (boardmenu->selected >= boardmenu->menu_count-1) break;
111                 // this is legit cpy paste please fix this
112                 {
113                     Menu* from_menu;
114                     Menu* to_menu;
115
116                     from_menu = boardmenu->menu_list[boardmenu->selected],
117                     to_menu = boardmenu->menu_list[boardmenu->selected+1],
118
119                     insert_item(
120                         to_menu,
121                         get_menu_item(
122                             from_menu,
123                             get_selected_item(from_menu)
124                         ),
125                         min(
126                             get_selected_item(from_menu),
127                             get_menu_length(to_menu)
128                         )
129                     );
130                     delete_item(
131                         from_menu,
132                         get_selected_item(from_menu) 
133                     );
134                     set_selected_menu(boardmenu, boardmenu->selected+1);
135                 }
136
137                 break;
138             case BINDING_DELETE_ITEM:
139                 menu_driver(active_menu, MENU_DELETE);
140                 break;
141             case BINDING_APPEND_ITEM:
142                 menu_driver(active_menu, MENU_APPEND);
143                 break;
144             case BINDING_INSERT_ABOVE:
145                 menu_driver(active_menu, MENU_INSERT_ABOVE);
146                 break;
147             case BINDING_INSERT_BELOW:
148                 menu_driver(active_menu, MENU_INSERT_BELOW);
149                 break;
150             /* case BINDING_MOVE_MENU_LEFT: */
151             /*     if (boardmenu->selected-1 < 0) break; */
152
153             /*     swap_menu(boardmenu, boardmenu->selected, boardmenu->selected-1); */
154                 /* boardmenu->selected -= 1; */
155                 /* set_selected_menu(boardmenu, boardmenu->selected); */
156
157                 /* break; */
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); */
163
164                 /* break; */
165             case BINDING_EDIT_ITEM:
166                 menu_driver(active_menu, MENU_EDIT);
167                 break;
168             case BINDING_SELECT:
169                 
170                 break;
171             case BINDING_WRITE:
172                 break;
173         }
174
175         for (int i = 0; i < boardmenu->menu_count; i++) {
176             render_menu(boardmenu->menu_list[i]);
177         }
178
179
180     }
181
182     exit_tscurses();
183     return 0;    
184 }
185
186 BoardMenu*
187 create_board_menu(Board* board)
188 {
189     BoardMenu* new_boardmenu;
190
191     new_boardmenu = malloc(sizeof(BoardMenu));
192
193     new_boardmenu->menu_list = make_menus(board, board->todolist_count);
194     new_boardmenu->menu_count = board->todolist_count;
195     new_boardmenu->selected = 0;
196
197     return new_boardmenu;
198 }
199
200 Board*
201 boardmenu_to_board(BoardMenu* boardmenu)
202 {
203     Board newboard = malloc(sizeof(Board));
204     TodoList** todolist_list = malloc(sizeof(TodoList*));
205
206     for (int i = 0; i < boardmenu->menu_count; i++) {
207         Menu* curmenu = boardmenu->menu_list[i];
208
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);
213
214         /* for (int j = 0; j < get_menu_length(curmenu); j++) { */
215              
216         /* } */
217
218         new_todolist->item_list = item_list;
219
220     }
221
222     newboard->todolist_list = todolist_list;
223     newboard->todolist_count = boardmenu->menu_count;
224
225     return newboard;
226     
227 }
228
229 int
230 set_selected_menu(BoardMenu* boardmenu, int index)
231 {
232     Menu* old_menu;
233     Menu* new_menu;
234     int new_pos;
235     
236     old_menu = boardmenu->menu_list[boardmenu->selected];
237     new_menu = boardmenu->menu_list[index];
238
239     set_menu_focus(old_menu, false);
240     set_menu_focus(new_menu, true);
241
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);
246
247     boardmenu->selected = index;
248
249     return 0;
250 }
251
252 MenuItem** 
253 todolist_to_menuitem(TodoItem** item_list, int list_length)
254 {
255     MenuItem** items;
256
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);
260     }
261
262     items[list_length] = 0; //null terminate
263     return items;
264 }
265
266 Menu**
267 make_menus(Board* board, int todolist_length)
268 {
269
270     Menu** menu_list;
271
272     menu_list = malloc(todolist_length*sizeof(Menu*));
273
274     for (int i = 0; i < todolist_length; i++) {
275
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);
279
280         Menu* new_menu = create_menu(todo_list->list_name, item_list);
281
282         /* make window */
283         WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
284         box(win, 0, 0);
285
286         /* some menu settings */
287         set_menu_win(new_menu, win);
288         set_menu_focus(new_menu, i == 0); // make first win focused
289
290         /* refresh */
291         refresh();
292         wrefresh(win);
293
294         menu_list[i] = new_menu;
295     }
296
297     return menu_list;
298 }
299
300 int
301 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
302 {
303     /* reposition menus */
304     mvwin(get_menu_win(boardmenu->menu_list[src_index]),
305         1, 1+MENU_WIDTH*dest_index
306     );
307     mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
308         1, 1+MENU_WIDTH*src_index
309     );
310     refresh();
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])); */
317     clear();
318
319     /* swap in array */
320     ar_swap_item(boardmenu->menu_list, src_index, dest_index);
321
322     return 0;
323 }
324