X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=taskasaur.c;h=119411e5e790a3e4c715f5f6da7c12970a583a2b;hb=f270512184ab1c41eee46c76a149b7cbddb1b131;hp=3b28bad53b11881bd8213d14559e455acc91a127;hpb=35016d121af55c36a7c23503848375995e0762f9;p=taskasaur.git diff --git a/taskasaur.c b/taskasaur.c index 3b28bad..119411e 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -1,14 +1,19 @@ #include -#include -#include #include +#include +#include #include +#include +#include void winch_handler(int sig); 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 free_todo(char** todo_list, int todo_length); #include "config.h" @@ -19,10 +24,12 @@ main(int argc, char** argv) 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); @@ -68,56 +75,49 @@ main(int argc, char** argv) return 2; } + /* for (int i = 0; i < todo_length; i++) { */ + /* printf("%p\n", todos[i]); */ + /* printf(todos[i]); */ + /* } */ + /* return 0; */ // start ncurses initscr(); cbreak(); - /* raw(); */ noecho(); curs_set(0); start_color(); - - init_pair(1, COLOR_CYAN, COLOR_BLACK); - init_pair(2, COLOR_BLACK, COLOR_CYAN); getmaxyx(stdscr, height, width); - x = y = 0; - refresh(); - todo_win = create_list_win(20, 40, 5, 5); - for (int i = 0; i < todo_length; i++) { - mvwprintw(todo_win, i+1, 2, todos[i]); - } + todo_win = create_win(20, 40, 5, 5); + + todo_menu = create_todo_menu(todo_win, todos, todo_length); + post_menu(todo_menu); wrefresh(todo_win); - move(y,x); - while ((ch = getch()) != 113) { // while not q + while ((ch = getch()) != 'q') { - // ofc the first thing we need is vim keys switch (ch) { - case 104: // h - x -= 1; - break; - case 106: // j - y += 1; + case 'j': + menu_driver(todo_menu, REQ_DOWN_ITEM); break; - case 107: // k - y -= 1; + case 'k': + menu_driver(todo_menu, REQ_UP_ITEM); break; - case 108: // l - x += 1; + case 'G': // try to implement gg too + menu_driver(todo_menu, REQ_LAST_ITEM); break; } - move(y,x); - refresh(); - /* clear(); */ + wrefresh(todo_win); } endwin(); /* Free mem */ - free(todos); // prob need to free each string in list too + unpost(todo_menu); + free_todo(todos, todo_length); return 0; } @@ -146,17 +146,11 @@ read_todo(FILE* file, int* length) while ((nread = getline(&lineptr, &len, file)) != -1) { out_len++; out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? - // remove new line character (maybe just write own new line func later) - /* lineptr = realloc(*lineptr, len-2); */ - /* *(lineptr+len-1) = '\0'; */ - /* printf(lineptr); */ - /* lineptr = realloc(lineptr, len-1); //maybe watch out for empty lines */ - /* *(lineptr+len-3) = '\0'; */ + lineptr[strcspn(lineptr, "\n")] = 0; // remove newline out_arr[out_len-1] = lineptr; lineptr = NULL; - len = 0; } *length = out_len; @@ -164,10 +158,44 @@ read_todo(FILE* file, int* length) } 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], ""); + } + item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason? + + todo_menu = new_menu(item_list); + + getmaxyx(stdscr, wheight, wwidth); + set_menu_win(todo_menu, win); + set_menu_sub(todo_menu, derwin(win, wheight, wwidth, 0, 0)); + + box(win, 0, 0); //temp + + return todo_menu; +} + +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); +}