X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=taskasaur.c;h=28c25a8fa4b6122930563bba9ca25a734c28b24f;hb=8d3a5e5f51153c38f4c5b20a309af2b6e5bf686d;hp=a4c0f4e4938827982894b5ab6b367bdd6e12c68c;hpb=f74c3f9aca7f090530179355c399a08bb9ae164a;p=taskasaur.git diff --git a/taskasaur.c b/taskasaur.c index a4c0f4e..28c25a8 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -1,14 +1,30 @@ #include -#include -#include #include +#include +#include #include +#include +#include + +#define SELECTED_COLOR 1 +#define NON_SELECTED_COLOR 2 + +struct todo_item { + char* name; + char* description; + char** items; +}; void winch_handler(int sig); -char** read_todo(FILE* file); +char** read_todo(FILE* file, int* length); -WINDOW* create_list_win(int height, int width, int y, int x); +WINDOW* create_win(int height, int width, int y, int x); +MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length); + +void on_select(char *item); + +void free_todo(char** todo_list, int todo_length); #include "config.h" @@ -17,10 +33,14 @@ main(int argc, char** argv) { int flag; FILE* board_file; + char** todos; + int todo_length; + int height, width; - int x, y; int ch; + WINDOW* todo_win; + MENU* todo_menu; signal(SIGWINCH, winch_handler); @@ -36,8 +56,7 @@ main(int argc, char** argv) return 1; } - char** todos = read_todo(board_file); - printf(todos[0]); + todos = read_todo(board_file, &todo_length); fclose(board_file); break; @@ -51,7 +70,14 @@ main(int argc, char** argv) return 1; } // create a file here + board_file = fopen(optarg, "w"); + // write init stuff here + fclose(board_file); printf("Successfully created %s\n", optarg); + + todos = malloc(0); + todo_length = 0; + break; case -1: @@ -60,51 +86,54 @@ main(int argc, char** argv) return 2; } - - - return 0; - // start ncurses initscr(); cbreak(); - /* raw(); */ noecho(); + curs_set(0); + keypad(stdscr, TRUE); start_color(); - - init_pair(1, COLOR_CYAN, COLOR_BLACK); - init_pair(2, COLOR_BLACK, COLOR_CYAN); + + /* colors */ + init_pair(SELECTED_COLOR, selected_color, COLOR_BLACK); + init_pair(NON_SELECTED_COLOR, non_selected_color, COLOR_BLACK); getmaxyx(stdscr, height, width); - x = y = 0; - refresh(); - todo_win = create_list_win(20, 20, 5, 5); + todo_win = create_win(20, 40, 5, 5); + todo_menu = create_todo_menu(todo_win, todos, todo_length); + post_menu(todo_menu); + refresh(); + wrefresh(todo_win); - move(y,x); - while ((ch = getch()) != 113) { // while not q + while ((ch = getch()) != BINDING_QUIT) { - // ofc the first thing we need is vim keys switch (ch) { - case 104: // h - x -= 1; + case BINDING_SCROLL_UP: + menu_driver(todo_menu, REQ_UP_ITEM); + break; + case BINDING_SCROLL_DOWN: + menu_driver(todo_menu, REQ_DOWN_ITEM); break; - case 106: // j - y += 1; + case BINDING_JUMP_TOP: + menu_driver(todo_menu, REQ_FIRST_ITEM); break; - case 107: // k - y -= 1; + case BINDING_JUMP_BOTTOM: + menu_driver(todo_menu, REQ_LAST_ITEM); break; - case 108: // l - x += 1; + case BINDING_SELECT: break; } + wrefresh(todo_win); - move(y,x); - refresh(); - /* clear(); */ } endwin(); + + /* Free mem */ + unpost_menu(todo_menu); + free_todo(todos, todo_length); + return 0; } @@ -115,9 +144,8 @@ winch_handler(int sig) refresh(); } - char** -read_todo(FILE* file) +read_todo(FILE* file, int* length) { // apparently getline isn't rly that portable, so consider other options char** out_arr; int out_len; @@ -133,21 +161,67 @@ read_todo(FILE* file) while ((nread = getline(&lineptr, &len, file)) != -1) { out_len++; out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? - printf("Pointer set to: %p\n", lineptr); + + lineptr[strcspn(lineptr, "\n")] = 0; // remove newline out_arr[out_len-1] = lineptr; lineptr = NULL; - len = 0; } - + + *length = out_len; return out_arr; } WINDOW* -create_list_win(int height, int width, int y, int x) +create_win(int height, int width, int y, int x) { WINDOW* new_win = newwin(height, width, y, x); - box(new_win, 0, 0); wrefresh(new_win); return new_win; } + +MENU* +create_todo_menu(WINDOW* win, char** todo_list, int todo_length) +{ + MENU* todo_menu; + ITEM** item_list; + ITEM* cur_item; + int wheight, wwidth; + + item_list = malloc((todo_length+1)*sizeof(ITEM*)); + for (int i = 0; i < todo_length; i++) { + item_list[i] = new_item(todo_list[i], ""); + set_item_userptr(item_list[i], on_select); + } + item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason? + + todo_menu = new_menu(item_list); + + getmaxyx(win, wheight, wwidth); + set_menu_win(todo_menu, win); + set_menu_sub(todo_menu, derwin(win, wheight-2, wwidth-2, 1, 2)); + set_menu_mark(todo_menu, ""); + set_menu_spacing(todo_menu, 1, 2, 1); + set_menu_fore(todo_menu, COLOR_PAIR(SELECTED_COLOR)); + set_menu_back(todo_menu, COLOR_PAIR(NON_SELECTED_COLOR)); + + box(win, 0, 0); //temp + + return todo_menu; +} + +void +on_select(char *item) +{ + printf("lol"); +} + +void +free_todo(char** todo_list, int todo_length) +{ + // probably check if list is too short or too long + for (int i = 0; i < todo_length; i++) { + free(todo_list[i]); + } + free(todo_list); +}