about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-02 01:54:51 -0500
committerJune McEnroe <june@causal.agency>2020-02-02 01:54:51 -0500
commitc799310d67b825f2aacf7b573f23991654d1e6c4 (patch)
tree47cc4c2d37fdf8ca1f65c7bdf4ffaaf2dfcf8860
parentImplement word wrap (diff)
downloadcatgirl-c799310d67b825f2aacf7b573f23991654d1e6c4.tar.gz
catgirl-c799310d67b825f2aacf7b573f23991654d1e6c4.zip
Implement wordWidth
Diffstat (limited to '')
-rw-r--r--ui.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/ui.c b/ui.c
index 7ce0257..7b1e339 100644
--- a/ui.c
+++ b/ui.c
@@ -25,6 +25,8 @@
 #include <string.h>
 #include <sysexits.h>
 #include <time.h>
+#include <wchar.h>
+#include <wctype.h>
 
 #include "chat.h"
 
@@ -112,7 +114,6 @@ static struct Window *windowFor(size_t id) {
 	if (!window) err(EX_OSERR, "malloc");
 	window->id = id;
 	window->pad = newpad(PadLines, COLS);
-	wsetscrreg(window->pad, 0, PadLines - 1);
 	scrollok(window->pad, true);
 	wmove(window->pad, PadLines - 1, 0);
 	window->heat = Cold;
@@ -211,20 +212,28 @@ static void styleParse(struct Style *style, const char **str, size_t *len) {
 
 static int wordWidth(const char *str) {
 	size_t len = strcspn(str, " ");
-	// TODO: wcswidth.
-	return len;
+	int width = 0;
+	while (len) {
+		wchar_t wc;
+		int n = mbtowc(&wc, str, len);
+		if (n < 1) return width + len;
+		width += (iswprint(wc) ? wcwidth(wc) : 0);
+		str += n;
+		len -= n;
+	}
+	return width;
 }
 
 static void styleAdd(WINDOW *win, const char *str) {
-	int _, x, width;
-	getmaxyx(win, _, width);
+	int y, x, width;
+	getmaxyx(win, y, width);
 
 	size_t len;
 	struct Style style = Reset;
 	while (*str) {
 		if (*str == ' ') {
+			getyx(win, y, x);
 			const char *word = &str[strspn(str, " ")];
-			getyx(win, _, x);
 			if (width - x - 1 < wordWidth(word)) {
 				waddch(win, '\n');
 				str = word;