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