crude scrolling
[taskasaur.git] / taskasaur.c
index a4c0f4e..dbc3d31 100644 (file)
-#include <stdio.h>
-#include <ncurses.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-void winch_handler(int sig); 
-
-char** read_todo(FILE* file);
-
-WINDOW* create_list_win(int height, int width, int y, int x);
 
+#include "headers/parser.h"
+#include "headers/render.h"
+#include "headers/menu.h"
+#include "headers/utils.h"
 #include "config.h"
 
-int 
-main(int argc, char** argv) 
+void render_step(BoardMenu* boardmenu);
+void save_to_file(char* filepath, BoardMenu* boardmenu);
+
+int
+main(int argc, char** argv)
 {
-    int flag;
-    FILE* board_file;
-    int height, width;
-    int x, y;
-    int ch;
-    WINDOW* todo_win;
-
-    signal(SIGWINCH, winch_handler);
-
-    // read command line args
-    flag = getopt(argc, argv, "o:n:");
-    switch (flag) {
-        case 'o':
-
-            // 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;
-            }   
-            
-            char** todos = read_todo(board_file);
-            printf(todos[0]);
-            fclose(board_file);
-
-            break;
-
-        case 'n':
-
-            // 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;
-            }
-            // create a file here
-            printf("Successfully created %s\n", optarg);
-            break;
-
-        case -1:
-        case '?':
-            printf("Help string\n");
-            return 2;
-    }
+    char* boardfile_name = "test_board.md"; 
+    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();
 
-    return 0;
+    BoardMenu* boardmenu;
+    boardmenu = create_board_menu(board);
 
-    // start ncurses 
-    initscr();
-    cbreak();
-    /* raw(); */
-    noecho();
-    start_color();
-    
-    init_pair(1, COLOR_CYAN, COLOR_BLACK); 
-    init_pair(2, COLOR_BLACK, COLOR_CYAN); 
+    /* need to render before user presses anything */
+    render_step(boardmenu);
 
-    getmaxyx(stdscr, height, width);
-    x = y = 0;
-    refresh();
+    int ch;
+    while ((ch = getch()) != BINDING_QUIT) {
+
+        Menu* active_menu;
+        active_menu = boardmenu->menu_list[boardmenu->selected];
 
-    todo_win = create_list_win(20, 20, 5, 5);
-    
-    move(y,x);
-    while ((ch = getch()) != 113) { // while not q
-        
-        // ofc the first thing we need is vim keys 
         switch (ch) {
-            case 104: // h
-                x -= 1;
+
+            case BINDING_SCROLL_UP:
+                menu_driver(active_menu, MENU_UP);
+                break;
+            case BINDING_SCROLL_DOWN:
+                menu_driver(active_menu, MENU_DOWN);
+                break;
+            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_JUMP_TOP:
+                menu_driver(active_menu, MENU_TOP);
+                break;
+            case BINDING_JUMP_BOTTOM:
+                menu_driver(active_menu, MENU_BOTTOM);
                 break;
-            case 106: // j
-                y += 1;
+            case BINDING_MOVE_ITEM_UP:
+                menu_driver(active_menu, MENU_MOVE_UP);
                 break;
-            case 107: // k
-                y -= 1;
+            case BINDING_MOVE_ITEM_DOWN:
+                menu_driver(active_menu, MENU_MOVE_DOWN);
                 break;
-            case 108: // l
-                x += 1;
+            case BINDING_MOVE_ITEM_LEFT:
+                if (boardmenu->selected-1 < 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_menu_item(
+                            from_menu,
+                            get_selected_item(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_ITEM_RIGHT:
+                if (boardmenu->selected >= boardmenu->menu_count-1) 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_menu_item(
+                            from_menu,
+                            get_selected_item(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_DELETE_ITEM:
+                menu_driver(active_menu, MENU_DELETE);
+                break;
+            case BINDING_APPEND_ITEM:
+                menu_driver(active_menu, MENU_APPEND);
+                break;
+            case BINDING_INSERT_ABOVE:
+                menu_driver(active_menu, MENU_INSERT_ABOVE);
+                break;
+            case BINDING_INSERT_BELOW:
+                menu_driver(active_menu, MENU_INSERT_BELOW);
+                break;
+            /* case BINDING_MOVE_MENU_LEFT: */
+            /*     if (boardmenu->selected-1 < 0) break; */
+
+            /*     swap_menu(boardmenu, boardmenu->selected, boardmenu->selected-1); */
+                /* boardmenu->selected -= 1; */
+                /* set_selected_menu(boardmenu, boardmenu->selected); */
+
+                /* 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); */
+
+                /* break; */
+            case BINDING_EDIT_ITEM:
+                menu_driver(active_menu, MENU_EDIT);
+                break;
+            case BINDING_SELECT:
+                break;
+            case BINDING_WRITE:
+                save_to_file(boardfile_name, 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;
+        }
+
+        render_step(boardmenu);
 
-        move(y,x);
-        refresh();
-        /* clear(); */
     }
+    
+    /* 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); */
 
-    endwin();
-    return 0;
+    exit_tscurses();
+    return 0;    
 }
 
-void 
-winch_handler(int sig) 
+void
+render_step(BoardMenu* boardmenu)
 {
-    endwin();
-    refresh();
-}
+        for (int i = 0; i < boardmenu->menu_count; i++) {
 
+            Menu* curmenu = boardmenu->menu_list[i];
 
-char**
-read_todo(FILE* file) 
-{ // 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;
-
-    out_arr = NULL;
-    out_len = 0;
-    lineptr = NULL;
-    len = 0;
-
-    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);
-        out_arr[out_len-1] = lineptr;
-
-        lineptr = NULL;
-        len = 0;
-    }
+            /* update the descriptions - maybe not do this here */ 
+            for (int j = 0; j < get_menu_length(curmenu); j++) {
+                update_menuitem_descrip(get_menu_item(curmenu, j));
+            }
 
-    return out_arr;
+            render_menu(curmenu);
+        }
 }
 
-WINDOW* 
-create_list_win(int height, int width, int y, int x)
+void
+save_to_file(char* filepath, BoardMenu* boardmenu)
 {
-    WINDOW* new_win = newwin(height, width, y, x);
-    box(new_win, 0, 0);
-    wrefresh(new_win);
-    return new_win;
+    Board* writeboard;
+    writeboard = boardmenu_to_board(boardmenu);
+
+    begin_write(filepath, writeboard);
+    free_board(writeboard);
 }