From: Daniel Liu Date: Tue, 16 Feb 2021 20:46:19 +0000 (-0500) Subject: refactor + man X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=bcb6f1950bfd2867df61d5c93aa73b73e093edbc;p=taskasaur.git refactor + man --- diff --git a/headers/render.h b/headers/render.h index 5a13100..a0fa083 100644 --- a/headers/render.h +++ b/headers/render.h @@ -3,6 +3,12 @@ #define __RENDER_H__ #include +#include "parser.h" +#include "menu.h" +#include "utils.h" + +// this is temp +#define MENU_WIDTH 40 enum TaskasaurColors { TS_SELECTED = 1, @@ -11,6 +17,12 @@ enum TaskasaurColors { TS_MENU_NONSELECTED, }; +typedef struct BoardMenu { + Menu** menu_list; + int menu_count; + int selected; +} BoardMenu; + /* the ts is used to not conflict with builtin names */ extern int init_tscurses(void); extern int exit_tscurses(void); @@ -18,6 +30,18 @@ extern int exit_tscurses(void); extern int curs_on(void); extern int curs_off(void); +/* board menu */ +extern BoardMenu* create_board_menu(Board* board); +extern Menu** make_menus(Board* board, int todolist_length); +extern MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length); +extern Board* boardmenu_to_board(BoardMenu* boardmenu); + +extern int set_selected_menu(BoardMenu* boardmenu, int index); +extern int swap_menu(BoardMenu* boardmenu, int src_index, int dest_index); + +/* popup menu for menu item */ +extern WINDOW* create_popup_win(); + /* some helpers */ extern int ungetstr(char* str); diff --git a/menu.c b/menu.c index e9071d6..96ad1e5 100644 --- a/menu.c +++ b/menu.c @@ -19,6 +19,7 @@ typedef struct MenuItem { char* title; char* description; + void* user_data; } MenuItem; typedef struct Menu { @@ -32,6 +33,7 @@ typedef struct Menu { WINDOW* sub_win; int max_height; int max_width; + void* user_data; } Menu; int swap_item(Menu* menu, int src_index, int dest_index); diff --git a/render.c b/render.c index ebaec93..657b8c6 100644 --- a/render.c +++ b/render.c @@ -4,7 +4,6 @@ #include "config.h" int init_tscolors(void); - int create_todowin(void); /* init stuff */ @@ -71,12 +70,171 @@ curs_off(void) int create_todowin(void) { + return 0; +} + +/* board menu */ +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; +} + +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; + } + + return menu_list; +} + +MenuItem** +todolist_to_menuitem(TodoItem** item_list, int list_length) +{ + 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; +} +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; + + } + + newboard->board_name = strdup(""); + newboard->todolist_list = new_todolist_list; + newboard->todolist_count = boardmenu->menu_count; + + 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; +} + +int +swap_menu(BoardMenu* boardmenu, int src_index, int dest_index) +{ + /* 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; } +/* popup */ +WINDOW* +create_popup_win() +{ + return NULL; +} + + int ungetstr(char* str) { diff --git a/taskasaur.1 b/taskasaur.1 index 352412b..f9dbfed 100644 --- a/taskasaur.1 +++ b/taskasaur.1 @@ -6,6 +6,28 @@ taskasaur \- suckless kanban [taskasaur options] .SH DESCRIPTION \fBtaskasaur\fP is a suckless style kanban program that runs on the command line with ncurses graphics. It stores data in the form of markdown for portability and readability. +.SH DEFAULT KEYBINDINGS +The default keybindings are vim inspired, you can change these in the config.h +.SS cursor movement +.TP +[\fBh, j, k, l\fP] to move up, down and across the todo list. +.TP +[\fBg, G\fP] jump to top and bottom of list, respectively. +.SS moving todo items +.TP +[\fBH, J, K, L\fP] to move the current hovered item. +.SS creating todo item +.TP +[\fBa, o, O\fP] all create a new entry in the currently selected todo list. \fBa\fP inserts new item at the end of the list, \fBo\fP inserts below the cursor, while \fBO\fP inserts above. +.SS deleting todo items +.TP +[\fBD\fP] to delete hovered item. +.SS editing todo items +.TP +[\fBi\fP] to switch to insert mode on hovered item. +.SS exiting +.TP +[\fBq\fP] to quit. .SH OPTIONS .SH AUTHORS \fBtaskasaur\fP was written by Daniel Liu diff --git a/taskasaur.c b/taskasaur.c index d3b8ba8..bdc72f5 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -5,23 +5,6 @@ #include "headers/utils.h" #include "config.h" -// this is temp -#define MENU_WIDTH 40 - -typedef struct BoardMenu { - Menu** menu_list; - int menu_count; - int selected; -} BoardMenu; - -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) { @@ -190,157 +173,3 @@ main(int argc, char** argv) 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; - - } - - newboard->board_name = strdup(""); - newboard->todolist_list = new_todolist_list; - newboard->todolist_count = boardmenu->menu_count; - - 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; -} - -MenuItem** -todolist_to_menuitem(TodoItem** item_list, int list_length) -{ - 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; -} - -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; - } - - return menu_list; -} - -int -swap_menu(BoardMenu* boardmenu, int src_index, int dest_index) -{ - /* 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; -} -