reading from file
[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* lineptr = read_todo(board_file);
42             printf("lineptr: %p\n", lineptr);
43             printf("first: %c\n", *lineptr);
44             free(lineptr);
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             printf("Successfully created %s\n", optarg);
59             break;
60
61         case -1:
62         case '?':
63             printf("Help string\n");
64             return 2;
65     }
66
67
68
69     return 0;
70
71     // start ncurses 
72     initscr();
73     cbreak();
74     /* raw(); */
75     noecho();
76     start_color();
77     
78     init_pair(1, COLOR_CYAN, COLOR_BLACK); 
79     init_pair(2, COLOR_BLACK, COLOR_CYAN); 
80
81     getmaxyx(stdscr, height, width);
82     x = y = 0;
83     refresh();
84
85     todo_win = create_list_win(20, 20, 5, 5);
86     
87     move(y,x);
88     while ((ch = getch()) != 113) { // while not q
89         
90         // ofc the first thing we need is vim keys 
91         switch (ch) {
92             case 104: // h
93                 x -= 1;
94                 break;
95             case 106: // j
96                 y += 1;
97                 break;
98             case 107: // k
99                 y -= 1;
100                 break;
101             case 108: // l
102                 x += 1;
103                 break;
104         } 
105
106         move(y,x);
107         refresh();
108         /* clear(); */
109     }
110
111     endwin();
112     return 0;
113 }
114
115 void 
116 winch_handler(int sig) 
117 {
118     endwin();
119     refresh();
120 }
121
122
123 char*
124 read_todo(FILE* file) 
125 { // apparently getline isn't rly that portable, so consider other options
126     char* lineptr;
127     size_t len;
128     ssize_t nread;
129
130     lineptr = NULL;
131     len = 0;
132
133     while ((nread = getline(&lineptr, &len, file)) != -1) {
134         printf("Read line of size %zd\n", nread);
135     }
136
137     return lineptr;
138 }
139
140 WINDOW* 
141 create_list_win(int height, int width, int y, int x)
142 {
143     WINDOW* new_win = newwin(height, width, y, x);
144     box(new_win, 0, 0);
145     wrefresh(new_win);
146     return new_win;
147 }