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
m_len; \
})
+extern int min(int a, int b);
+
+extern char* wrap_text(char* str, int max_width, int* lines);
+
#endif
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;
/* init curses */
init_tscurses();
- getch();
MenuItem** item_list = malloc(4*sizeof(MenuItem*));
for (int i = 0; i < 3; i++) {
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;
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+
#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;
+
+}
+