fixed scrolling
[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 void render_step(BoardMenu* boardmenu);
9 void save_to_file(char* filepath, BoardMenu* boardmenu);
10
11 int
12 main(int argc, char** argv)
13 {
14     char* boardfile_name = "test_board.md"; 
15     printf("%c]0;%s - %s%c", '\033', "taskasaur", boardfile_name, '\007'); // need to reset after program exits
16
17     /* read from todo file */
18     Board* board;
19     board = begin_parse(boardfile_name);
20     /* log_todo(board); */
21
22     /* init curses */
23     init_tscurses();
24
25     BoardMenu* boardmenu;
26     boardmenu = create_board_menu(board);
27
28     /* need to render before user presses anything */
29     render_step(boardmenu);
30
31     int ch;
32     while ((ch = getch()) != BINDING_QUIT) {
33
34         Menu* active_menu;
35         active_menu = boardmenu->menu_list[boardmenu->selected];
36
37         switch (ch) {
38
39             case BINDING_SCROLL_UP:
40                 menu_driver(active_menu, MENU_UP);
41                 break;
42             case BINDING_SCROLL_DOWN:
43                 menu_driver(active_menu, MENU_DOWN);
44                 break;
45             case BINDING_SCROLL_LEFT:
46                 if (boardmenu->selected-1 < 0) break;
47                 set_selected_menu(boardmenu, boardmenu->selected-1);
48                 break;
49             case BINDING_SCROLL_RIGHT:
50                 if (boardmenu->selected+1 > boardmenu->menu_count-1) break;
51                 set_selected_menu(boardmenu, boardmenu->selected+1);
52                 break;
53             case BINDING_JUMP_TOP:
54                 menu_driver(active_menu, MENU_TOP);
55                 break;
56             case BINDING_JUMP_BOTTOM:
57                 menu_driver(active_menu, MENU_BOTTOM);
58                 break;
59             case BINDING_MOVE_ITEM_UP:
60                 menu_driver(active_menu, MENU_MOVE_UP);
61                 break;
62             case BINDING_MOVE_ITEM_DOWN:
63                 menu_driver(active_menu, MENU_MOVE_DOWN);
64                 break;
65             case BINDING_MOVE_ITEM_LEFT:
66                 if (boardmenu->selected-1 < 0) break;
67                 if (get_menu_length(boardmenu->menu_list[boardmenu->selected]) == 0) break;
68                 {
69                     Menu* from_menu;
70                     Menu* to_menu;
71
72                     from_menu = boardmenu->menu_list[boardmenu->selected];
73                     to_menu = boardmenu->menu_list[boardmenu->selected-1];
74
75                     insert_item(
76                         to_menu,
77                         get_menu_item(
78                             from_menu,
79                             get_selected_item(from_menu)
80                         ),
81                         min(
82                             get_selected_item(from_menu),
83                             get_menu_length(to_menu)
84                         )
85                     );
86                     delete_item(
87                         from_menu,
88                         get_selected_item(from_menu) 
89                     );
90                     set_selected_menu(boardmenu, boardmenu->selected-1);
91                 }
92
93                 break;
94             case BINDING_MOVE_ITEM_RIGHT:
95                 if (boardmenu->selected >= boardmenu->menu_count-1) break;
96                 if (get_menu_length(boardmenu->menu_list[boardmenu->selected]) == 0) break;
97                 // this is legit cpy paste please fix this
98                 {
99                     Menu* from_menu;
100                     Menu* to_menu;
101
102                     from_menu = boardmenu->menu_list[boardmenu->selected];
103                     to_menu = boardmenu->menu_list[boardmenu->selected+1];
104
105                     insert_item(
106                         to_menu,
107                         get_menu_item(
108                             from_menu,
109                             get_selected_item(from_menu)
110                         ),
111                         min(
112                             get_selected_item(from_menu),
113                             get_menu_length(to_menu)
114                         )
115                     );
116                     delete_item(
117                         from_menu,
118                         get_selected_item(from_menu) 
119                     );
120                     set_selected_menu(boardmenu, boardmenu->selected+1);
121                 }
122
123                 break;
124             case BINDING_DELETE_ITEM:
125                 menu_driver(active_menu, MENU_DELETE);
126                 break;
127             case BINDING_APPEND_ITEM:
128                 menu_driver(active_menu, MENU_APPEND);
129                 break;
130             case BINDING_INSERT_ABOVE:
131                 menu_driver(active_menu, MENU_INSERT_ABOVE);
132                 break;
133             case BINDING_INSERT_BELOW:
134                 menu_driver(active_menu, MENU_INSERT_BELOW);
135                 break;
136             /* case BINDING_MOVE_MENU_LEFT: */
137             /*     if (boardmenu->selected-1 < 0) break; */
138
139             /*     swap_menu(boardmenu, boardmenu->selected, boardmenu->selected-1); */
140                 /* boardmenu->selected -= 1; */
141                 /* set_selected_menu(boardmenu, boardmenu->selected); */
142
143                 /* break; */
144             /* case BINDING_MOVE_MENU_RIGHT: */
145                 /* if (boardmenu->selected >= boardmenu->menu_count-1) break; */
146                 /* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected+1); */
147                 /* boardmenu->selected += 1; */
148                 /* set_selected_menu(boardmenu, boardmenu->selected); */
149
150                 /* break; */
151             case BINDING_EDIT_ITEM:
152                 menu_driver(active_menu, MENU_EDIT);
153                 break;
154             case BINDING_SELECT:
155                 break;
156             case BINDING_WRITE:
157                 save_to_file(boardfile_name, boardmenu);
158                 break;
159             case KEY_RESIZE:
160                 /* ; */
161                 /* int y, x; */
162                 /* char out[10]; */
163                 /* getmaxyx(stdscr, y, x); */
164                 /* sprintf(out, "%d,%d", y, x); */
165
166                 /* mvprintw(20, 20, out); */
167                 /* resize_term(y, x); */
168                 break;
169         }
170
171         render_step(boardmenu);
172
173     }
174     
175     /* save on exit - this causes weird stuff to happen, maybe it's not given enough time to write before program exits? */
176     /* save_to_file(boardfile_name, boardmenu); */
177
178     exit_tscurses();
179     return 0;    
180 }
181
182 void
183 render_step(BoardMenu* boardmenu)
184 {
185         for (int i = 0; i < boardmenu->menu_count; i++) {
186
187             Menu* curmenu = boardmenu->menu_list[i];
188
189             /* update the descriptions - maybe not do this here */ 
190             for (int j = 0; j < get_menu_length(curmenu); j++) {
191                 update_menuitem_descrip(get_menu_item(curmenu, j));
192             }
193
194             render_menu(curmenu);
195         }
196 }
197
198 void
199 save_to_file(char* filepath, BoardMenu* boardmenu)
200 {
201     Board* writeboard;
202     writeboard = boardmenu_to_board(boardmenu);
203
204     begin_write(filepath, writeboard);
205     free_board(writeboard);
206 }