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