-#include <stdio.h>
-#include <ncurses.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <unistd.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);
+// this is temp
+#define MENU_WIDTH 40
-WINDOW* create_list_win(int height, int width, int y, int x);
+typedef struct BoardMenu {
+ Menu** menu_list;
+ int menu_count;
+ int selected;
+} BoardMenu;
-#include "config.h"
+BoardMenu* create_board_menu(Board* board);
+Board* boardmenu_to_board(BoardMenu* boardmenu);
+int set_selected_menu(BoardMenu* boardmenu, int index);
+
+MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length);
+Menu** make_menus(Board* board, int todolist_length);
+int swap_menu(BoardMenu* boardmenu, int src_index, int dest_index);
-int
-main(int argc, char** argv)
+int
+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;
-
- 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;
- }
-
- todos = read_todo(board_file, &todo_length);
- 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
- 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:
- 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); */
- // 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);
+ /* init curses */
+ init_tscurses();
- getmaxyx(stdscr, height, width);
- x = y = 0;
- refresh();
+ BoardMenu* boardmenu;
+ boardmenu = create_board_menu(board);
- 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]);
+ // this is temp
+ for (int i = 0; i < boardmenu->menu_count; i++) {
+ render_menu(boardmenu->menu_list[i]);
}
- wrefresh(todo_win);
-
- move(y,x);
- while ((ch = getch()) != 113) { // while not q
-
- // ofc the first thing we need is vim keys
+
+ char ch;
+ while ((ch = getch()) != BINDING_QUIT) {
+
+ Menu* active_menu;
+ active_menu = boardmenu->menu_list[boardmenu->selected];
+
switch (ch) {
- case 104: // h
- x -= 1;
+
+ case BINDING_SCROLL_UP:
+ menu_driver(active_menu, MENU_UP);
break;
- case 106: // j
- y += 1;
+ case BINDING_SCROLL_DOWN:
+ menu_driver(active_menu, MENU_DOWN);
break;
- case 107: // k
- y -= 1;
+ case BINDING_SCROLL_LEFT:
+ if (boardmenu->selected-1 < 0) break;
+ set_selected_menu(boardmenu, boardmenu->selected-1);
break;
- case 108: // l
- x += 1;
+ 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 BINDING_MOVE_ITEM_UP:
+ menu_driver(active_menu, MENU_MOVE_UP);
+ break;
+ case BINDING_MOVE_ITEM_DOWN:
+ menu_driver(active_menu, MENU_MOVE_DOWN);
+ break;
+ 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:
+ {
+ Board* writeboard;
+ writeboard = boardmenu_to_board(boardmenu);
+
+ begin_write(boardfile_name, writeboard);
+ free_board(writeboard);
+ }
+
+ break;
+ }
+
+ for (int i = 0; i < boardmenu->menu_count; i++) {
+ render_menu(boardmenu->menu_list[i]);
+ }
+
+ }
+
+ exit_tscurses();
+ return 0;
+}
+
+BoardMenu*
+create_board_menu(Board* board)
+{
+ BoardMenu* new_boardmenu;
+
+ new_boardmenu = malloc(sizeof(BoardMenu));
+
+ new_boardmenu->menu_list = make_menus(board, board->todolist_count);
+ new_boardmenu->menu_count = board->todolist_count;
+ new_boardmenu->selected = 0;
+
+ return new_boardmenu;
+}
+
+Board*
+boardmenu_to_board(BoardMenu* boardmenu)
+{ // STRINGS are sharing the same address as the one in MENU
+ // and MENUITEM, this may break something if u free this board
+ // consider copying the string
+
+ Board* newboard = malloc(sizeof(Board));
+ TodoList** new_todolist_list = malloc(sizeof(TodoList*));
+
+ for (int i = 0; i < boardmenu->menu_count; i++) {
+ Menu* curmenu = boardmenu->menu_list[i];
+
+ TodoList* new_todolist = malloc(sizeof(TodoList));
+ TodoItem** new_item_list = malloc(sizeof(TodoItem*));
+ new_todolist->list_name = strdup(get_menu_name(curmenu));
+ new_todolist->item_count = get_menu_length(curmenu);
+
+ for (int j = 0; j < get_menu_length(curmenu); j++) {
+ MenuItem* curmenuitem = get_menu_item(curmenu, j);
+
+ TodoItem* new_todoitem = malloc(sizeof(TodoItem));
+
+ new_todoitem->item_name = strdup(get_menuitem_title(curmenuitem));
+ new_todoitem->description = strdup(get_menuitem_descrip(curmenuitem));
+ new_todoitem->due = strdup(""); //TEMP!
+ new_todoitem->subtask_list = malloc(0); //TEMP!
+ new_todoitem->subtask_count = 0; //TEMP!
+
+ new_item_list[j] = new_todoitem;
+ }
+
+ new_todolist->item_list = new_item_list;
+ new_todolist_list[i] = new_todolist;
- move(y,x);
- refresh();
- /* clear(); */
}
- endwin();
+ newboard->board_name = strdup("");
+ newboard->todolist_list = new_todolist_list;
+ newboard->todolist_count = boardmenu->menu_count;
- /* Free mem */
- free(todos); // prob need to free each string in list too
+ return newboard;
+
+}
+
+int
+set_selected_menu(BoardMenu* boardmenu, int index)
+{
+ Menu* old_menu;
+ Menu* new_menu;
+ int new_pos;
+
+ old_menu = boardmenu->menu_list[boardmenu->selected];
+ new_menu = boardmenu->menu_list[index];
+
+ set_menu_focus(old_menu, false);
+ set_menu_focus(new_menu, true);
+
+ /* also try to jump to a similar position if possible */
+ /* rn theres a bug if old menu is empty */
+ new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
+ set_selected_item(new_menu, new_pos);
+
+ boardmenu->selected = index;
return 0;
}
-void
-winch_handler(int sig)
+MenuItem**
+todolist_to_menuitem(TodoItem** item_list, int list_length)
{
- endwin();
- refresh();
+ MenuItem** items;
+
+ items = malloc((list_length+1)*sizeof(MenuItem*));
+ for (int i = 0; i < list_length; i++) {
+ items[i] = create_menuitem(item_list[i]->item_name);
+ }
+
+ items[list_length] = 0; //null terminate
+ return items;
}
-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;
-
- 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?
- // 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'; */
-
- out_arr[out_len-1] = lineptr;
-
- lineptr = NULL;
- len = 0;
+Menu**
+make_menus(Board* board, int todolist_length)
+{
+
+ Menu** menu_list;
+
+ menu_list = malloc(todolist_length*sizeof(Menu*));
+
+ for (int i = 0; i < todolist_length; i++) {
+
+ /* read from parsed */
+ TodoList* todo_list = board->todolist_list[i];
+ MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count);
+
+ Menu* new_menu = create_menu(todo_list->list_name, item_list);
+
+ /* make window */
+ WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
+ box(win, 0, 0);
+
+ /* some menu settings */
+ set_menu_win(new_menu, win);
+ set_menu_focus(new_menu, i == 0); // make first win focused
+
+ /* refresh */
+ refresh();
+ wrefresh(win);
+
+ menu_list[i] = new_menu;
}
-
- *length = out_len;
- return out_arr;
+
+ return menu_list;
}
-WINDOW*
-create_list_win(int height, int width, int y, int x)
+int
+swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
{
- WINDOW* new_win = newwin(height, width, y, x);
- box(new_win, 0, 0);
- wrefresh(new_win);
- return new_win;
+ /* reposition menus */
+ mvwin(get_menu_win(boardmenu->menu_list[src_index]),
+ 1, 1+MENU_WIDTH*dest_index
+ );
+ mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
+ 1, 1+MENU_WIDTH*src_index
+ );
+ refresh();
+ wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
+ wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
+ /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
+ /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
+ /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
+ /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
+ clear();
+
+ /* swap in array */
+ ar_swap_item((void*)boardmenu->menu_list, src_index, dest_index);
+
+ return 0;
}
+