changes in popup menu is now preserved
[taskasaur.git] / taskasaur.c
index 83bbab1..c7c224f 100644 (file)
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <signal.h>
-#include <unistd.h>
-#include <ncurses.h>
-#include <menu.h>
 
-void winch_handler(int sig); 
+#include "headers/parser.h"
+#include "headers/render.h"
+#include "headers/menu.h"
+#include "headers/utils.h"
+#include "config.h"
 
-char** read_todo(FILE* file, int* length);
+char* boardfile_name = "test_board.md"; 
 
-WINDOW* create_win(int height, int width, int y, int x);
-MENU* create_todo_menu(char** todo_list, int todo_length);
+void normal_handleinput(BoardMenu* boardmenu, int ch);
+void popup_handleinput(BoardMenu* boardmenu, int ch);
+void generic_handleinput(Menu* menu, int ch);
 
-#include "config.h"
+void renderstep(BoardMenu* boardmenu);
+void save_to_file(char* filepath, BoardMenu* boardmenu);
 
-int 
-main(int argc, char** argv) 
+void exit_step(BoardMenu* boardmenu);
+
+int
+main(int argc, char** argv)
 {
-    int flag;
-    FILE* board_file;
-    char** todos;
-    int todo_length;
+    printf("%c]0;%s - %s%c", '\033', "taskasaur", boardfile_name, '\007'); // need to reset after program exits
+
+    /* read from todo file */
+    Board* board;
+    board = begin_parse(boardfile_name);
+    /* log_todo(board); */
+
+    /* init curses */
+    init_tscurses();
+
+    BoardMenu* boardmenu;
+    boardmenu = create_board_menu(board);
+
+    /* need to render before user presses anything */
+    renderstep(boardmenu);
 
-    int height, width;
     int ch;
+    while (1) {
 
-    WINDOW* todo_win;
-    MENU* todo_menu;
+        ch = getch();
 
-    signal(SIGWINCH, winch_handler);
+        if (boardmenu->popup_open == 0) {
+            normal_handleinput(boardmenu, ch);
+        } else {
+            popup_handleinput(boardmenu, ch);
+        }
 
-    // read command line args
-    flag = getopt(argc, argv, "o:n:");
-    switch (flag) {
-        case 'o':
+        renderstep(boardmenu);
 
-            // read from task file (might need to check for read and write permissions)
-            board_file = fopen(optarg, "r");
-            if (!board_file) {
-                printf("%s does not exist\n", optarg);
-                return 1;
-            }   
-            
-            todos = read_todo(board_file, &todo_length);
-            fclose(board_file);
+    }
+    
+    /* save on exit - this causes weird stuff to happen, maybe it's not given enough time to write before program exits? */
+    /* save_to_file(boardfile_name, boardmenu); */
 
-            break;
+    return 0;    
+}
+
+void
+normal_handleinput(BoardMenu* boardmenu, int ch)
+{
+
+    Menu* active_menu;
+    active_menu = boardmenu->menu_list[boardmenu->selected];
 
-        case 'n':
+    switch (ch) {
 
-            // make sure file does not exist
-            // however, it maybe be possible that an different error has occured (besides the file not existing)
-            if (access(optarg, F_OK) == 0) { 
-                printf("%s already exists\n", optarg);
-                return 1;
+        case BINDING_SCROLL_LEFT:
+            if (boardmenu->selected-1 < 0) break;
+            set_selected_menu(boardmenu, boardmenu->selected-1);
+            break;
+        case BINDING_SCROLL_RIGHT:
+            if (boardmenu->selected+1 > boardmenu->menu_count-1) break;
+            set_selected_menu(boardmenu, boardmenu->selected+1);
+            break;
+        case BINDING_MOVE_ITEM_LEFT:
+            if (boardmenu->selected-1 < 0) break;
+            if (get_menu_length(boardmenu->menu_list[boardmenu->selected]) == 0) break;
+            {
+                Menu* from_menu;
+                Menu* to_menu;
+
+                from_menu = boardmenu->menu_list[boardmenu->selected];
+                to_menu = boardmenu->menu_list[boardmenu->selected-1];
+
+                insert_item(
+                    to_menu,
+                    get_selected_menuitem(from_menu),
+                    min(
+                        get_selected_item(from_menu),
+                        get_menu_length(to_menu)
+                    )
+                );
+                delete_item(
+                    from_menu,
+                    get_selected_item(from_menu) 
+                );
+                set_selected_menu(boardmenu, boardmenu->selected-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 BINDING_MOVE_ITEM_RIGHT:
+            if (boardmenu->selected >= boardmenu->menu_count-1) break;
+            if (get_menu_length(boardmenu->menu_list[boardmenu->selected]) == 0) break;
+            // this is legit cpy paste please fix this
+            {
+                Menu* from_menu;
+                Menu* to_menu;
+
+                from_menu = boardmenu->menu_list[boardmenu->selected];
+                to_menu = boardmenu->menu_list[boardmenu->selected+1];
+
+                insert_item(
+                    to_menu,
+                    get_selected_menuitem(from_menu),
+                    min(
+                        get_selected_item(from_menu),
+                        get_menu_length(to_menu)
+                    )
+                );
+                delete_item(
+                    from_menu,
+                    get_selected_item(from_menu) 
+                );
+                set_selected_menu(boardmenu, boardmenu->selected+1);
+            }
 
             break;
+        /* case BINDING_MOVE_MENU_LEFT: */
+        /*     if (boardmenu->selected-1 < 0) break; */
 
-        case -1:
-        case '?':
-            printf("Help string\n");
-            return 2;
-    }
+        /*     swap_menu(boardmenu, boardmenu->selected, boardmenu->selected-1); */
+            /* boardmenu->selected -= 1; */
+            /* set_selected_menu(boardmenu, boardmenu->selected); */
 
-    /* for (int i = 0; i < todo_length; i++) { */
-    /*     printf("%p\n", todos[i]); */
-    /*     printf(todos[i]); */
-    /* } */    
-    /* return 0; */
+            /* break; */
+        /* case BINDING_MOVE_MENU_RIGHT: */
+            /* if (boardmenu->selected >= boardmenu->menu_count-1) break; */
+            /* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected+1); */
+            /* boardmenu->selected += 1; */
+            /* set_selected_menu(boardmenu, boardmenu->selected); */
 
-    // start ncurses 
-    initscr();
-    cbreak();
-    noecho();
-    curs_set(0);
-    start_color();
+            /* break; */
+        case BINDING_SELECT:
+            {
+                Menu* sel_menu;
+                TodoItem* sel_itemdata;
 
-    getmaxyx(stdscr, height, width);
-    refresh();
+                sel_menu = boardmenu->menu_list[boardmenu->selected];
+                sel_itemdata = (TodoItem*)get_menuitem_userdata(
+                    get_menu_item(sel_menu, get_selected_item(sel_menu))
+                );
 
-    /* todo_win = create_win(20, 40, 5, 5); */
-    char* test_todo[] = {"1lmao", "2pee", "3poo", "4feces"};
-    /* todo_menu = create_todo_menu(test_todo, 4); */
-    todo_menu = create_todo_menu(todos, todo_length);
-    post_menu(todo_menu);
-    
-    while ((ch = getch()) != 'q') {
-        
-        switch (ch) {
-            case 'j':
-                menu_driver(todo_menu, REQ_DOWN_ITEM);
-                break;
-            case 'k':
-                menu_driver(todo_menu, REQ_UP_ITEM);
-                break;
-            case 'G': // try to implement gg too
-                menu_driver(todo_menu, REQ_LAST_ITEM);
-                break;
-        } 
+                /* set mode to popup */
+                boardmenu->popupmenu = make_popupmenu(sel_itemdata);
+                boardmenu->popup_open = 1;
 
-    }
+            } 
 
-    endwin();
+            break;
+        case BINDING_WRITE:
+            save_to_file(boardfile_name, boardmenu);
+            break;
+        case BINDING_QUIT:
+            exit_step(boardmenu);
+            break;
+        case KEY_RESIZE:
+            /* ; */
+            /* int y, x; */
+            /* char out[10]; */
+            /* getmaxyx(stdscr, y, x); */
+            /* sprintf(out, "%d,%d", y, x); */
+
+            /* mvprintw(20, 20, out); */
+            /* resize_term(y, x); */
+            break;
 
-    /* Free mem */
-    free(todos); // prob need to free each string in list too
+        default:
+            generic_handleinput(active_menu, ch);
 
-    return 0;
+    }
 }
 
-void 
-winch_handler(int sig) 
+void
+popup_handleinput(BoardMenu* boardmenu, int ch)
 {
-    endwin();
-    refresh();
-}
+    Menu* popupmenu_menu;
 
-char**
-read_todo(FILE* file, int* length) 
-{ // apparently getline isn't rly that portable, so consider other options
-    char** out_arr;
-    int out_len;
-    char* lineptr;
-    size_t len;
-    ssize_t nread;
+    popupmenu_menu = boardmenu->popupmenu->menu;
 
-    out_arr = NULL;
-    out_len = 0;
-    lineptr = NULL;
-    len = 0;
+    switch (ch) {
 
-    while ((nread = getline(&lineptr, &len, file)) != -1) {
-        out_len++;
-        out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing?
+        case BINDING_QUIT:
+            boardmenu->popup_open = 0;
+            close_popupmenu(popupmenu_menu);
+            
+            /* reset screen */
+            clear();
+            renderstep(boardmenu);
+            break;
 
-        lineptr[strcspn(lineptr, "\n")] = 0; // remove newline
+        case BINDING_TOGGLE_DONE:
+            {
+                SubTask* curitem_data = (SubTask*)get_menuitem_userdata(get_menu_item(popupmenu_menu, get_selected_item(popupmenu_menu))); 
+                curitem_data->done = (curitem_data->done == SubTaskState_todo) ? SubTaskState_done : SubTaskState_todo;
+            }
 
-        out_arr[out_len-1] = lineptr;
+            break;
 
-        lineptr = NULL;
-    }
-    
-    *length = out_len;
-    return out_arr;
+        default:
+            generic_handleinput(popupmenu_menu, ch);
+    }    
 }
 
-WINDOW* 
-create_win(int height, int width, int y, int x)
+void
+generic_handleinput(Menu* menu, int ch)
 {
-    WINDOW* new_win = newwin(height, width, y, x);
-    wrefresh(new_win);
-    return new_win;
+    switch (ch) {
+
+        case BINDING_SCROLL_UP:
+            menu_driver(menu, MENU_UP);
+            break;
+        case BINDING_SCROLL_DOWN:
+            menu_driver(menu, MENU_DOWN);
+            break;
+        case BINDING_JUMP_TOP:
+            menu_driver(menu, MENU_TOP);
+            break;
+        case BINDING_JUMP_BOTTOM:
+            menu_driver(menu, MENU_BOTTOM);
+            break;
+        case BINDING_MOVE_ITEM_UP:
+            menu_driver(menu, MENU_MOVE_UP);
+            break;
+        case BINDING_MOVE_ITEM_DOWN:
+            menu_driver(menu, MENU_MOVE_DOWN);
+            break;
+        case BINDING_DELETE_ITEM:
+            menu_driver(menu, MENU_DELETE);
+            break;
+        case BINDING_APPEND_ITEM:
+            menu_driver(menu, MENU_APPEND);
+            break;
+        case BINDING_INSERT_ABOVE:
+            menu_driver(menu, MENU_INSERT_ABOVE);
+            break;
+        case BINDING_INSERT_BELOW:
+            menu_driver(menu, MENU_INSERT_BELOW);
+            break;
+        case BINDING_EDIT_ITEM:
+            menu_driver(menu, MENU_EDIT);
+            break;
+    }            
 }
 
-MENU*
-create_todo_menu(char** todo_list, int todo_length)
+void
+renderstep(BoardMenu* boardmenu)
 {
-    MENU* todo_menu;
-    ITEM** item_list;
-    ITEM* cur_item;
-
-    item_list = malloc((todo_length+1)*sizeof(ITEM*));
-    for (int i = 0; i < todo_length; i++) {
-        printf(todo_list[i]);
-        item_list[i] = new_item(todo_list[i], "");
+    /* render main board menu */
+    if (boardmenu->popup_open == 0) {
+        for (int i = 0; i < boardmenu->menu_count; i++) {
+
+            Menu* curmenu = boardmenu->menu_list[i];
+
+            render_menu(curmenu);
+        }
+
+    /* render popup if it's open */
+    } else {
+
+        // something weird happened, maybe raise error
+        /* if (boradmenu->popupmenu == NULL) return; */
+
+        render_menu(boardmenu->popupmenu->menu);
+        wrefresh(boardmenu->popupmenu->win);
+
     }
-    item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason?
+    refresh();
+}
 
-    todo_menu = new_menu(item_list);
+void
+save_to_file(char* filepath, BoardMenu* boardmenu)
+{
+    Board* writeboard;
+    writeboard = boardmenu_to_board(boardmenu);
 
-    return todo_menu;
+    begin_write(filepath, writeboard);
+    free_board(writeboard);
+}
+
+void
+exit_step(BoardMenu* boardmenu)
+{
+    exit_tscurses();
+    exit(0);
 }