077bf9f7cbadb608073ceec4a5302ea65d125019
[taskasaur.git] / main.c
1 #include <stdio.h>
2 #include <ncurses.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #define MAX_TODO_LENGTH 200
8
9 void winch_handler(int sig); 
10
11 char** read_todo(FILE* file);
12
13 WINDOW* create_list_win(int height, int width, int y, int x);
14
15 #include "config.h"
16
17 int 
18 main(int argc, char** argv) 
19 {
20     int flag;
21     FILE* board_file;
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             char** todos = read_todo(board_file);
42             printf(todos[0]);
43             fclose(board_file);
44
45             break;
46
47         case 'n':
48
49             // make sure file does not exist
50             // however, it maybe be possible that an different error has occured (besides the file not existing)
51             if (access(optarg, F_OK) == 0) { 
52                 printf("%s already exists\n", optarg);
53                 return 1;
54             }
55             // create a file here
56             printf("Successfully created %s\n", optarg);
57             break;
58
59         case -1:
60         case '?':
61             printf("Help string\n");
62             return 2;
63     }
64
65
66
67     return 0;
68
69     // start ncurses 
70     initscr();
71     cbreak();
72     /* raw(); */
73     noecho();
74     start_color();
75     
76     init_pair(1, COLOR_CYAN, COLOR_BLACK); 
77     init_pair(2, COLOR_BLACK, COLOR_CYAN); 
78
79     getmaxyx(stdscr, height, width);
80     x = y = 0;
81     refresh();
82
83     todo_win = create_list_win(20, 20, 5, 5);
84     
85     move(y,x);
86     while ((ch = getch()) != 113) { // while not q
87         
88         // ofc the first thing we need is vim keys 
89         switch (ch) {
90             case 104: // h
91                 x -= 1;
92                 break;
93             case 106: // j
94                 y += 1;
95                 break;
96             case 107: // k
97                 y -= 1;
98                 break;
99             case 108: // l
100                 x += 1;
101                 break;
102         } 
103
104         move(y,x);
105         refresh();
106         /* clear(); */
107     }
108
109     endwin();
110     return 0;
111 }
112
113 void 
114 winch_handler(int sig) 
115 {
116     endwin();
117     refresh();
118 }
119
120
121 char**
122 read_todo(FILE* file) 
123 { // apparently getline isn't rly that portable, so consider other options
124     char** out_arr;
125     int out_len;
126     char* lineptr;
127     size_t len;
128     ssize_t nread;
129
130     out_arr = NULL;
131     out_len = 0;
132     lineptr = NULL;
133     len = 0;
134
135     while ((nread = getline(&lineptr, &len, file)) != -1) {
136         out_len++;
137         out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing?
138         printf("Pointer set to: %p\n", lineptr);
139         out_arr[out_len-1] = lineptr;
140
141         lineptr = NULL;
142         len = 0;
143     }
144
145     return out_arr;
146 }
147
148 WINDOW* 
149 create_list_win(int height, int width, int y, int x)
150 {
151     WINDOW* new_win = newwin(height, width, y, x);
152     box(new_win, 0, 0);
153     wrefresh(new_win);
154     return new_win;
155 }