display list
[taskasaur.git] / taskasaur.c
1 #include <stdio.h>
2 #include <ncurses.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 void winch_handler(int sig); 
8
9 char** read_todo(FILE* file, int* length);
10
11 WINDOW* create_list_win(int height, int width, int y, int x);
12
13 #include "config.h"
14
15 int 
16 main(int argc, char** argv) 
17 {
18     int flag;
19     FILE* board_file;
20     char** todos;
21     int todo_length;
22     int height, width;
23     int x, y;
24     int ch;
25     WINDOW* todo_win;
26
27     signal(SIGWINCH, winch_handler);
28
29     // read command line args
30     flag = getopt(argc, argv, "o:n:");
31     switch (flag) {
32         case 'o':
33
34             // read from task file (might need to check for read and write permissions)
35             board_file = fopen(optarg, "r");
36             if (!board_file) {
37                 printf("%s does not exist\n", optarg);
38                 return 1;
39             }   
40             
41             todos = read_todo(board_file, &todo_length);
42             fclose(board_file);
43
44             break;
45
46         case 'n':
47
48             // make sure file does not exist
49             // however, it maybe be possible that an different error has occured (besides the file not existing)
50             if (access(optarg, F_OK) == 0) { 
51                 printf("%s already exists\n", optarg);
52                 return 1;
53             }
54             // create a file here
55             board_file = fopen(optarg, "w");
56             // write init stuff here
57             fclose(board_file);
58             printf("Successfully created %s\n", optarg);
59
60             todos = malloc(0);
61             todo_length = 0;
62
63             break;
64
65         case -1:
66         case '?':
67             printf("Help string\n");
68             return 2;
69     }
70
71
72     // start ncurses 
73     initscr();
74     cbreak();
75     /* raw(); */
76     noecho();
77     curs_set(0);
78     start_color();
79     
80     init_pair(1, COLOR_CYAN, COLOR_BLACK); 
81     init_pair(2, COLOR_BLACK, COLOR_CYAN); 
82
83     getmaxyx(stdscr, height, width);
84     x = y = 0;
85     refresh();
86
87     todo_win = create_list_win(20, 40, 5, 5);
88     for (int i = 0; i < todo_length; i++) {
89         mvwprintw(todo_win, i+1, 2, todos[i]);
90     }
91     wrefresh(todo_win);
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         // remove new line character (maybe just write own new line func later)
150         /* lineptr = realloc(*lineptr, len-2); */
151         /* *(lineptr+len-1) = '\0'; */
152         /* printf(lineptr); */
153         /* lineptr = realloc(lineptr, len-1); //maybe watch out for empty lines */
154         /* *(lineptr+len-3) = '\0'; */
155
156         out_arr[out_len-1] = lineptr;
157
158         lineptr = NULL;
159         len = 0;
160     }
161     
162     *length = out_len;
163     return out_arr;
164 }
165
166 WINDOW* 
167 create_list_win(int height, int width, int y, int x)
168 {
169     WINDOW* new_win = newwin(height, width, y, x);
170     box(new_win, 0, 0);
171     wrefresh(new_win);
172     return new_win;
173 }