multiple menus
[taskasaur.git] / old.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <unistd.h>
6 #include <ncurses.h>
7 #include <menu.h>
8
9 #define SELECTED_COLOR 1
10 #define NON_SELECTED_COLOR 2
11
12 struct todo_item {
13     char* name;
14     char* description;
15     char** items;
16 };
17
18 void winch_handler(int sig); 
19
20 char** read_todo(FILE* file, int* length);
21
22 WINDOW* create_win(int height, int width, int y, int x);
23 MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length);
24
25 void on_select(char *item);
26
27 void free_todo(char** todo_list, int todo_length);
28
29 #include "config.h"
30
31 int 
32 main(int argc, char** argv) 
33 {
34     int flag;
35     FILE* board_file;
36     char** todos;
37     int todo_length;
38
39     int height, width;
40     int ch;
41
42     WINDOW* todo_win;
43     MENU* todo_menu;
44
45     signal(SIGWINCH, winch_handler);
46
47     // read command line args
48     flag = getopt(argc, argv, "o:n:");
49     switch (flag) {
50         case 'o':
51
52             // read from task file (might need to check for read and write permissions)
53             board_file = fopen(optarg, "r");
54             if (!board_file) {
55                 printf("%s does not exist\n", optarg);
56                 return 1;
57             }   
58             
59             todos = read_todo(board_file, &todo_length);
60             fclose(board_file);
61
62             break;
63
64         case 'n':
65
66             // make sure file does not exist
67             // however, it maybe be possible that an different error has occured (besides the file not existing)
68             if (access(optarg, F_OK) == 0) { 
69                 printf("%s already exists\n", optarg);
70                 return 1;
71             }
72             // create a file here
73             board_file = fopen(optarg, "w");
74             // write init stuff here
75             fclose(board_file);
76             printf("Successfully created %s\n", optarg);
77
78             todos = malloc(0);
79             todo_length = 0;
80
81             break;
82
83         case -1:
84         case '?':
85             printf("Help string\n");
86             return 2;
87     }
88
89     // start ncurses 
90     initscr();
91     cbreak();
92     noecho();
93     curs_set(0);
94     keypad(stdscr, TRUE);
95     start_color();
96
97     /* colors */
98     init_pair(SELECTED_COLOR, selected_color, COLOR_BLACK);
99     init_pair(NON_SELECTED_COLOR, non_selected_color, COLOR_BLACK);
100
101     getmaxyx(stdscr, height, width);
102
103     todo_win = create_win(20, 40, 5, 5);
104     todo_menu = create_todo_menu(todo_win, todos, todo_length);
105     post_menu(todo_menu);
106     refresh();
107     wrefresh(todo_win);
108     
109     while ((ch = getch()) != BINDING_QUIT) {
110         
111         switch (ch) {
112             case BINDING_SCROLL_UP:
113                 menu_driver(todo_menu, REQ_UP_ITEM);
114                 break;
115             case BINDING_SCROLL_DOWN:
116                 menu_driver(todo_menu, REQ_DOWN_ITEM);
117                 break;
118             case BINDING_JUMP_TOP:
119                 menu_driver(todo_menu, REQ_FIRST_ITEM);
120                 break;
121             case BINDING_JUMP_BOTTOM:
122                 menu_driver(todo_menu, REQ_LAST_ITEM);
123                 break;
124             case BINDING_SELECT:
125                 break;
126         } 
127         wrefresh(todo_win);
128
129     }
130
131     endwin();
132
133     /* Free mem */
134     unpost_menu(todo_menu);
135     free_todo(todos, todo_length);
136
137     return 0;
138 }
139
140 void 
141 winch_handler(int sig) 
142 {
143     endwin();
144     refresh();
145 }
146
147 char**
148 read_todo(FILE* file, int* length) 
149 { // apparently getline isn't rly that portable, so consider other options
150     char** out_arr;
151     int out_len;
152     char* lineptr;
153     size_t len;
154     ssize_t nread;
155
156     out_arr = NULL;
157     out_len = 0;
158     lineptr = NULL;
159     len = 0;
160
161     while ((nread = getline(&lineptr, &len, file)) != -1) {
162         out_len++;
163         out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing?
164
165         lineptr[strcspn(lineptr, "\n")] = 0; // remove newline
166         out_arr[out_len-1] = lineptr;
167
168         lineptr = NULL;
169     }
170     
171     *length = out_len;
172     return out_arr;
173 }
174
175 WINDOW* 
176 create_win(int height, int width, int y, int x)
177 {
178     WINDOW* new_win = newwin(height, width, y, x);
179     wrefresh(new_win);
180     return new_win;
181 }
182
183 MENU*
184 create_todo_menu(WINDOW* win, char** todo_list, int todo_length)
185 {
186     MENU* todo_menu;
187     ITEM** item_list;
188     ITEM* cur_item;
189     int wheight, wwidth;
190
191     item_list = malloc((todo_length+1)*sizeof(ITEM*));
192     for (int i = 0; i < todo_length; i++) {
193         item_list[i] = new_item(todo_list[i], "");
194         set_item_userptr(item_list[i], on_select);
195     }
196     item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason?
197
198     todo_menu = new_menu(item_list);
199
200     getmaxyx(win, wheight, wwidth);
201     set_menu_win(todo_menu, win);
202     set_menu_sub(todo_menu, derwin(win, wheight-2, wwidth-2, 1, 2));
203     set_menu_mark(todo_menu, "");
204     set_menu_spacing(todo_menu, 1, 2, 1);
205     set_menu_fore(todo_menu, COLOR_PAIR(SELECTED_COLOR));
206     set_menu_back(todo_menu, COLOR_PAIR(NON_SELECTED_COLOR));
207
208     box(win, 0, 0); //temp
209
210     return todo_menu;
211 }
212
213 void
214 on_select(char *item)
215 {
216     printf("lol");
217 }
218
219 void
220 free_todo(char** todo_list, int todo_length)
221 {
222     // probably check if list is too short or too long
223     for (int i = 0; i < todo_length; i++) {
224         free(todo_list[i]); 
225     }
226     free(todo_list);
227 }