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