aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-09-23 20:55:00 +0200
committerThomas Voss <mail@thomasvoss.com> 2022-09-23 20:55:00 +0200
commit68f7fb971322ffb03c8b5deba60ee25e31c77bfd (patch)
treeb393c74fbc4a893028674d714f68c1272bfe618f
parent003fe73b8902048b6190554e10a68e4b1c3b18c6 (diff)
Fix text centering of multibyte characters
-rw-r--r--center.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/center.c b/center.c
index 37a643e..be6d9ee 100644
--- a/center.c
+++ b/center.c
@@ -113,8 +113,10 @@ center(FILE *fp)
tabs++;
}
- len = lenfunc(line) + tabs * 8 - tabs + 1;
- printf("%*s", ((int) width - len) / 2 + len, line);
+ len = lenfunc(line) + tabs * 8 - tabs;
+ for (int i = (width - len) / 2; i; i--)
+ putchar(' ');
+ fputs(line, stdout);
}
if (ferror(fp)) {
warn("getline");
@@ -145,6 +147,9 @@ utf8len(const char *s)
while (*s)
l += (*s++ & 0xC0) != 0x80;
+ if (l > 0 && s[-1] == '\n')
+ l--;
+
return l;
}