From: Daniel Liu Date: Sun, 24 Jan 2021 21:49:07 +0000 (-0500) Subject: text wrap X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=303071d6e0488d7da4dc810897948b8ed97354d2;p=taskasaur.git text wrap --- diff --git a/headers/menu.h b/headers/menu.h index 120ddad..ce0883e 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -15,6 +15,8 @@ extern int set_menu_items(Menu* menu, MenuItem** item_list); extern int set_menu_win(Menu* menu, WINDOW* win); extern MenuItem* get_menu_items(Menu* menu); +extern int render_menu(Menu* menu); + extern int free_menu(Menu* menu); #endif diff --git a/headers/utils.h b/headers/utils.h index 0d23060..3dab4a9 100644 --- a/headers/utils.h +++ b/headers/utils.h @@ -18,4 +18,8 @@ array_length(type, array) \ m_len; \ }) +extern int min(int a, int b); + +extern char* wrap_text(char* str, int max_width, int* lines); + #endif diff --git a/menu.c b/menu.c index 5d5a0eb..2c1975f 100644 --- a/menu.c +++ b/menu.c @@ -72,13 +72,19 @@ get_menu_items(Menu* menu) int render_menu(Menu* menu) { + int cur_line; + + cur_line = 0; for (int i = 0; i < menu->menu_length; i++) { /* wrap text by inserting newlines */ + mvwprintw(menu->menu_win, cur_line, 0, menu->menu_items[i]->contents); /* color selected item */ + cur_line += 1; + } return 0; diff --git a/taskasaur.c b/taskasaur.c index 15dcb9a..af7b3d3 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -14,7 +14,6 @@ main(int argc, char** argv) /* init curses */ init_tscurses(); - getch(); MenuItem** item_list = malloc(4*sizeof(MenuItem*)); for (int i = 0; i < 3; i++) { @@ -24,7 +23,16 @@ main(int argc, char** argv) Menu* menu = create_menu(item_list); + render_menu(menu); + + getch(); + exit_tscurses(); + + /* int out; */ + /* printf("%s", wrap_text("ayylmaooooxdxdxdxdxd", 3, &out)); */ + /* printf("%d\n", out); */ + /* wrap_text("ayylmaooooxdxdxdxdxd", 3, &out); */ return 0; diff --git a/utils.c b/utils.c index 2f5c53b..ecf4b70 100644 --- a/utils.c +++ b/utils.c @@ -1,4 +1,51 @@ +#include +#include +#include +#include + #include "headers/utils.h" +int +min(int a, int b) +{ + return (a < b) ? a : b; +} + +char* +wrap_text(char* str, int max_width, int* lines) +{ + char* wrapped_str; + char* str_read; + int totlen; + int line_count; + + wrapped_str = malloc(sizeof(char)); + wrapped_str[0] = 0; + str_read = str; + totlen = 0; + line_count = 0; + + for (int i = 0; i < floor(strlen(str)/max_width)+1; i++) { + + int curlen; + + curlen = min(strlen(str_read), max_width); + totlen += (curlen+1); // account for new line + + wrapped_str = realloc(wrapped_str, sizeof(char)*totlen+1); // account for null + strncat(wrapped_str, str_read, curlen); + strcat(wrapped_str, "\n"); + + str_read += max_width; + line_count += 1; + + } + + *lines = line_count; + + return wrapped_str; + +} +