From 8ae019ffc34b98bcd8546598de6037addaa29a4f Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sat, 10 Apr 2021 13:40:51 +0200 Subject: vis-menu: try to preserve valid Unicode points Before we were not taking non-ascii characters into account properly. With this patch we still mix byte counts and "grapheme cluster" (i.e. complete glyphs that are rendered in a terminal cell) counts but the code should be less broken in the more common case now. --- vis-menu.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'vis-menu.c') diff --git a/vis-menu.c b/vis-menu.c index 444a26a..66e23e3 100644 --- a/vis-menu.c +++ b/vis-menu.c @@ -84,12 +84,46 @@ appenditem(Item *item, Item **list, Item **last) { static size_t textwn(const char *s, int l) { - int b, c; /* bytes and UTF-8 characters */ + int c; - for(b=c=0; s && s[b] && (l<0 || b0 && c + utfcharbytes >= l)) { + break; + } + + c += utfcharbytes; + } + + return c; +} + static size_t textw(const char *s) { return textwn(s, -1); @@ -149,7 +183,7 @@ drawtext(const char *t, size_t w, Color col) { buf[tw] = '\0'; memcpy(buf, t, MIN(strlen(t), tw)); if (textw(t) > w) /* Remember textw returns the width WITH padding */ - for (i = MAX((tw-4), 0); i < tw; i++) buf[i] = '.'; + for (i = MAX(textvalidn(t, w-4), 0); i < tw; i++) buf[i] = '.'; fprintf(stderr, "%s %s %s", prestr, buf, poststr); free(buf); -- cgit v1.2.3