diff options
author | Hiltjo Posthuma <hiltjo@codemadness.org> | 2017-06-29 19:03:20 +0200 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2017-06-29 19:03:20 +0200 |
commit | cfd9f1f73aaadd5a6eeac7b542419a25a98a8e99 (patch) | |
tree | 618708dbc0440cbcd280aefe03f074c3bc06b6c9 | |
parent | 4d4ae4d724ad54773d28b486575ee950aff6ca22 (diff) |
improve printutf8pad for sfeed_plain
- use a UTF-8 ellipses (1 column width) for "...".
- do proper truncation at the specified length.
-rw-r--r-- | util.c | 28 |
1 files changed, 17 insertions, 11 deletions
@@ -245,20 +245,26 @@ void printutf8pad(FILE *fp, const char *s, size_t len, int pad) { wchar_t w; - size_t n = 0, i; - int r; + size_t col = 0, i, slen; + int rl, wc; - for (i = 0; *s && n < len; i++, s++) { - if (ISUTF8(*s)) { - if ((r = mbtowc(&w, s, 4)) == -1) - break; - if ((r = wcwidth(w)) == -1) - r = 1; - n += (size_t)r; + if (!len) + return; + + slen = strlen(s); + for (i = 0; i < slen && col < len + 1; i += rl) { + if ((rl = mbtowc(&w, &s[i], slen - i < 4 ? slen - i : 4)) <= 0) + break; + if ((wc = wcwidth(w)) == -1) + wc = 1; + col += (size_t)wc; + if (col >= len && s[i + rl]) { + fputs("\xe2\x80\xa6", fp); + break; } - putc(*s, fp); + fwrite(&s[i], 1, rl, fp); } - for (; n < len; n++) + for (; col < len; col++) putc(pad, fp); } |