menu test
[taskasaur.git] / taskasaur.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <ncurses.h>
6 #include <menu.h>
7
8 void winch_handler(int sig); 
9
10 char** read_todo(FILE* file, int* length);
11
12 WINDOW* create_list_win(int height, int width, int y, int x);
13
14 void draw_todo(WINDOW* todo_win, 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     int height, width;
26     int x, y;
27     int ch;
28     WINDOW* todo_win;
29
30     signal(SIGWINCH, winch_handler);
31
32     // read command line args
33     flag = getopt(argc, argv, "o:n:");
34     switch (flag) {
35         case 'o':
36
37             // read from task file (might need to check for read and write permissions)
38             board_file = fopen(optarg, "r");
39             if (!board_file) {
40                 printf("%s does not exist\n", optarg);
41                 return 1;
42             }   
43             
44             todos = read_todo(board_file, &todo_length);
45             fclose(board_file);
46
47             break;
48
49         case 'n':
50
51             // make sure file does not exist
52             // however, it maybe be possible that an different error has occured (besides the file not existing)
53             if (access(optarg, F_OK) == 0) { 
54                 printf("%s already exists\n", optarg);
55                 return 1;
56             }
57             // create a file here
58             board_file = fopen(optarg, "w");
59             // write init stuff here
60             fclose(board_file);
61             printf("Successfully created %s\n", optarg);
62
63             todos = malloc(0);
64             todo_length = 0;
65
66             break;
67
68         case -1:
69         case '?':
70             printf("Help string\n");
71             return 2;
72     }
73
74
75     // start ncurses 
76     initscr();
77     cbreak();
78     /* raw(); */
79     noecho();
80     curs_set(0);
81     start_color();
82     
83     init_pair(1, COLOR_CYAN, COLOR_BLACK); 
84     init_pair(2, COLOR_BLACK, COLOR_CYAN); 
85
86     getmaxyx(stdscr, height, width);
87     x = y = 0;
88     refresh();
89
90     todo_win = create_list_win(20, 40, 5, 5);
91     draw_todo(todo_win, todos, todo_length);
92     
93     move(y,x);
94     while ((ch = getch()) != 113) { // while not q
95         
96         // ofc the first thing we need is vim keys 
97         switch (ch) {
98             case 104: // h
99                 x -= 1;
100                 break;
101             case 106: // j
102                 y += 1;
103                 break;
104             case 107: // k
105                 y -= 1;
106                 break;
107             case 108: // l
108                 x += 1;
109                 break;
110         } 
111
112         move(y,x);
113         refresh();
114         /* clear(); */
115     }
116
117     endwin();
118
119     /* Free mem */
120     free(todos); // prob need to free each string in list too
121
122     return 0;
123 }
124
125 void 
126 winch_handler(int sig) 
127 {
128     endwin();
129     refresh();
130 }
131
132 char**
133 read_todo(FILE* file, int* length) 
134 { // apparently getline isn't rly that portable, so consider other options
135     char** out_arr;
136     int out_len;
137     char* lineptr;
138     size_t len;
139     ssize_t nread;
140
141     out_arr = NULL;
142     out_len = 0;
143     lineptr = NULL;
144     len = 0;
145
146     while ((nread = getline(&lineptr, &len, file)) != -1) {
147         out_len++;
148         out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing?
149         out_arr[out_len-1] = lineptr;
150
151         lineptr = NULL;
152         len = 0;
153     }
154     
155     *length = out_len;
156     return out_arr;
157 }
158
159 WINDOW* 
160 create_list_win(int height, int width, int y, int x)
161 {
162     WINDOW* new_win = newwin(height, width, y, x);
163     wrefresh(new_win);
164     return new_win;
165 }
166
167 void 
168 draw_todo(WINDOW* todo_win, char** todo_list, int todo_length) {
169     for (int i = 0; i < todo_length; i++) {
170         mvwprintw(todo_win, i+1, 2, todo_list[i]);
171     }
172     box(todo_win, 0, 0);
173     wrefresh(todo_win);
174 }