diff options
| author | Fuwn <[email protected]> | 2022-02-03 10:54:07 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-02-03 10:54:07 +0000 |
| commit | a63a2d5ac3d408e5057df15ff6cded7ae410b283 (patch) | |
| tree | f7298bf67f00fd3857df43224da50c7975b7f79b | |
| parent | feat(log): coloured logging (diff) | |
| download | viv-a63a2d5ac3d408e5057df15ff6cded7ae410b283.tar.xz viv-a63a2d5ac3d408e5057df15ff6cded7ae410b283.zip | |
feat: implement a working, alright ui
| -rw-r--r-- | include/viv/ui.h | 4 | ||||
| -rw-r--r-- | include/viv/viv.h | 1 | ||||
| -rw-r--r-- | viv/ui.c | 89 | ||||
| -rw-r--r-- | viv/viv.c | 37 |
4 files changed, 63 insertions, 68 deletions
diff --git a/include/viv/ui.h b/include/viv/ui.h index 26ff4b4..3b37a42 100644 --- a/include/viv/ui.h +++ b/include/viv/ui.h @@ -8,8 +8,8 @@ #include <curses.h> -void UI_initialise(void); +void UI_initialise(char *[], int); /* void UI_free(void); */ -void UI_print_in_middle(WINDOW *, int, int, int, char *, chtype); +/* void UI_print_in_middle(WINDOW *, int, int, int, char *, chtype); */ #endif /* VIV_UI_H */ diff --git a/include/viv/viv.h b/include/viv/viv.h index bbb608c..2bd2546 100644 --- a/include/viv/viv.h +++ b/include/viv/viv.h @@ -13,6 +13,7 @@ typedef void(*split_fn)(const char *, int, void *); int VIV_exit(int, const char *, ...); /* http://www.martinbroadhurst.com/split-a-string-in-c.html */ void VIV_split(const char *, char, split_fn, void *); +void VIV_add_to_dynamic_array(const char *, int, void *); char *strsep(char **__restrict, const char *__restrict); @@ -5,55 +5,16 @@ #include "viv/ui.h" -#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) - #include <menu.h> #include <stdlib.h> #include <string.h> -char *choices[] = { - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - "mix", - "pop", - (char *)NULL -}; - -void UI_initialise(void) { - // int height, width, i; +void UI_initialise(char *content[], int content_length) { WINDOW *window; ITEM **window_items; MENU *menu; int character; - int choices_size; + int max_y; initscr(); start_color(); @@ -63,53 +24,55 @@ void UI_initialise(void) { init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK); - choices_size = ARRAY_SIZE(choices); - window_items = (ITEM **)calloc(choices_size, sizeof(ITEM *)); + window_items = (ITEM **)calloc(content_length, sizeof(ITEM *)); + max_y = getmaxy(stdscr); - for (int i = 0; i < choices_size; ++i) { - window_items[i] = new_item(choices[i], choices[i]); + for (int i = 0; i < content_length; ++i) { + window_items[i] = new_item(content[i], content[i]); } menu = new_menu((ITEM **)window_items); - window = newwin(10, 40, 4, 4); + window = newwin(max_y, 60, 0, 0); keypad(window, TRUE); set_menu_win(menu, window); - set_menu_sub(menu, derwin(window, 6, 38, 3, 1)); - set_menu_format(menu, 5, 1); - - set_menu_mark(menu, " * "); + set_menu_sub(menu, derwin(window, max_y - 2, 60, 0, 0)); + set_menu_format(menu, max_y - 2, 1); + set_menu_mark(menu, ""); - box(window, 0, 0); + /* box(window, 0, 0); UI_print_in_middle(window, 1, 0, 40, "viv", COLOR_PAIR(1)); mvwaddch(window, 2, 0, ACS_LTEE); mvwhline(window, 2, 1, ACS_HLINE, 38); - mvwaddch(window, 2, 39, ACS_RTEE); + mvwaddch(window, 2, 39, ACS_RTEE); */ post_menu(menu); wrefresh(window); attron(COLOR_PAIR(2)); - mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scroll down or up a page of items"); - mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); + mvprintw(LINES - 2, 0, "exit: q"); + mvprintw(LINES - 1, 0, "movement: hjkl"); attroff(COLOR_PAIR(2)); refresh(); - while ((character = wgetch(window)) != KEY_F(1)) { + while ((character = wgetch(window)) != 'q') { switch (character) { - case KEY_UP: { + case 'k': { menu_driver(menu, REQ_UP_ITEM); } break; - case KEY_DOWN: { + case 'j': { menu_driver(menu, REQ_DOWN_ITEM); } break; - case KEY_RIGHT: { + case 'l': { menu_driver(menu, REQ_SCR_DPAGE); } break; - case KEY_LEFT: { + case 'h': { menu_driver(menu, REQ_SCR_UPAGE); } break; + /* case KEY_RESIZE | 'r': { + + } break; */ default: { /* ignore */ } break; @@ -118,7 +81,7 @@ void UI_initialise(void) { unpost_menu(menu); free_menu(menu); - for (int i = 0; i < choices_size; ++i) { + for (int i = 0; i < content_length; ++i) { free_item(window_items[i]); } delwin(window); @@ -126,7 +89,7 @@ void UI_initialise(void) { refresh(); } -void UI_print_in_middle( +/* void UI_print_in_middle( WINDOW *window, int start_y, int start_x, @@ -141,7 +104,7 @@ void UI_print_in_middle( getyx(window, y, x); - /* if (start_x != 0) { x = start_x; } */ + */ /* if (start_x != 0) { x = start_x; } */ /* if (start_y != 0) { y = start_y; } if (width != 0) { width = 80; } @@ -152,4 +115,4 @@ void UI_print_in_middle( mvwprintw(window, y, x, "%s", string); wattroff(window, colour); refresh(); -} +} */ @@ -10,6 +10,7 @@ #include <string.h> #include <unistd.h> +#include "viv/dynamic_array.h" #include "viv/cli.h" #include "viv/gemini.h" #include "viv/log.h" @@ -141,6 +142,30 @@ int main(int argc, char *argv[]) { gemini_ctx.content ) + VIV_DYNAMIC_ARRAY_dynamic_array *array = VIV_DYNAMIC_ARRAY_create(0); + + VIV_split(gemini_ctx.content, '\n', VIV_add_to_dynamic_array, array); + + char *lines[VIV_DYNAMIC_ARRAY_get_count(array)]; + + for (int i = 0; i < (int)VIV_DYNAMIC_ARRAY_get_count(array); ++i) { + lines[i] = VIV_DYNAMIC_ARRAY_get(array, i); + + if (strlen(lines[i]) > 1) { + lines[i][strlen(lines[i]) - 1] = 0; + if (lines[i][strlen(lines[i]) - 1] == '\r') { + lines[i][strlen(lines[i]) - 1] = ' '; + } + } else if (strlen(lines[i]) == 1) { + lines[i] = " "; + } + } + + UI_initialise(lines, (int)(sizeof(lines) / sizeof(*lines))); + + VIV_DYNAMIC_ARRAY_for_each(array, free); + VIV_DYNAMIC_ARRAY_delete(array); + close(connection_context.socket); SSL_CTX_free(ssl_context); ERR_free_strings(); @@ -148,9 +173,6 @@ int main(int argc, char *argv[]) { free(gemini_ctx.header.header); - /* ui.c testing */ - /* UI_initialise(); */ - return EXIT_SUCCESS; } @@ -200,3 +222,12 @@ char *strsep(char **__restrict stringp, const char *__restrict delim) { return rv; } + +void VIV_add_to_dynamic_array(const char *string, int length, void *data) { + VIV_DYNAMIC_ARRAY_dynamic_array *array = data; + char *token = calloc(length + 1, 1); + + memcpy(token, string, length); + + VIV_DYNAMIC_ARRAY_add_tail(array, token); +} |