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;
65365831eb91141&follow=1'>Move C file description commentsJune McEnroe Comments are ugly this is why I don't put them in my code ;) 2017-09-06Fix bin/Makefile FreeBSD compatibilityJune McEnroe Have to use the same generic rule as sys.mk so it replaces it. Otherwise it only uses the one in sys.mk. 2017-09-06Remove C code from link scriptJune McEnroe 2017-09-06Move C code to bin, Makefile, AGPLJune McEnroe 2017-09-05Pass final NULL to execlpJune McEnroe 2017-09-05Add watch.cJune McEnroe 2017-09-02Remove Scala highlightingJune McEnroe 2017-08-31Fix Linux console Tarmak 3June McEnroe Wow oops. 2017-08-31Use Tarmak 3 on Linux consoleJune McEnroe 2017-08-25Install ddateJune McEnroe 2017-08-19Move nethack options to envJune McEnroe 2017-08-17Remove scala syntax fileJune McEnroe 2017-08-06Add wakeJune McEnroe That payload can appear anywhere within an ethernet frame. Wake-on-LAN is funny. 2017-08-04Use $() in install.shJune McEnroe Something gave me the impression that sh did not like this, but it's specified by POSIX. 2017-08-02Create Code Tarmak 3 layoutJune McEnroe 2017-07-31Add tupJune McEnroe 2017-07-31Use designated initializer for hnel tableJune McEnroe I did not know this syntax worked! 2017-07-30Add juneJune McEnroe 2017-07-30Play nethack as ValkyrieJune McEnroe 2017-07-28Add toggle to hnelJune McEnroe 2017-07-28Install slJune McEnroe 2017-07-25Add up, supJune McEnroe 2017-07-24Autopickup ringsJune McEnroe 2017-07-24Name dogJune McEnroe 2017-07-23Add nethackrcJune McEnroe 2017-07-23Remove useless setuid in briJune McEnroe Don't you think it would be better if the setuid bit only gave you permission to do it and didn't do it for you? 2017-07-23Clean up hnel a tiny bitJune McEnroe 2017-07-21Set window size in hnelJune McEnroe 2017-07-21Add hnelJune McEnroe 2017-07-19chmod 600 in dtchJune McEnroe