Implement word wrapping

master
Curtis McEnroe 2018-08-06 15:12:13 -04:00
parent d6fb797b11
commit 3f3fa34d8a
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
2 changed files with 21 additions and 2 deletions

2
pls.c
View File

@ -26,7 +26,7 @@ wchar_t *wcssep(wchar_t **stringp, const wchar_t *delim) {
size_t i = wcscspn(orig, delim); size_t i = wcscspn(orig, delim);
*stringp = NULL; *stringp = NULL;
if (orig[i]) { if (orig[i]) {
orig[i] = '\0'; orig[i] = L'\0';
*stringp = &orig[i + 1]; *stringp = &orig[i + 1];
} }
return orig; return orig;

21
ui.c
View File

@ -198,17 +198,36 @@ static const wchar_t *parseColor(short *pair, const wchar_t *str) {
return str; return str;
} }
static void wordWrap(WINDOW *win, const wchar_t *str) {
size_t len = wcscspn(str, L" ");
size_t width = 1;
for (size_t i = 0; i < len; ++i) {
if (iswprint(str[i])) width += wcwidth(str[i]);
}
int _, x, xMax;
getyx(win, _, x);
getmaxyx(win, _, xMax);
if (width >= (size_t)(xMax - x)) {
waddch(win, '\n');
} else {
waddch(win, ' ');
}
}
static void addIRC(WINDOW *win, const wchar_t *str) { static void addIRC(WINDOW *win, const wchar_t *str) {
attr_t attr = A_NORMAL; attr_t attr = A_NORMAL;
short pair = -1; short pair = -1;
for (;;) { for (;;) {
size_t cc = wcscspn(str, L"\2\3\35\37"); size_t cc = wcscspn(str, L" \2\3\35\37");
wattr_set(win, attr | attr8(pair), 1 + pair8(pair), NULL); wattr_set(win, attr | attr8(pair), 1 + pair8(pair), NULL);
waddnwstr(win, str, cc); waddnwstr(win, str, cc);
if (!str[cc]) break; if (!str[cc]) break;
str = &str[cc]; str = &str[cc];
switch (*str++) { switch (*str++) {
break; case L' ': wordWrap(win, str);
break; case L'\2': attr ^= A_BOLD; break; case L'\2': attr ^= A_BOLD;
break; case L'\3': str = parseColor(&pair, str); break; case L'\3': str = parseColor(&pair, str);
break; case L'\35': attr ^= A_ITALIC; break; case L'\35': attr ^= A_ITALIC;