[pacman-dev] [PATCH] dump_pkg_info: fix wide character title alignment

Andrew Gregory andrew.gregory.8 at gmail.com
Mon Feb 1 17:14:47 UTC 2016


The padding added to the end of the title was based on the return value
of mbstowcs which is the number of characters.  This caused alignment
issues for languages with characters that span multiple columns.
Instead, base the padding on the number of columns needed by the
translated string as returned by wcswidth.

Fixes #47980

Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
---

It might make more sense to eventually rewrite this to use string_length from
util.c and do all of the padding using the original char string, but I went the
more direct route for maint.

 src/pacman/package.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/src/pacman/package.c b/src/pacman/package.c
index ac47493..3ab9abc 100644
--- a/src/pacman/package.c
+++ b/src/pacman/package.c
@@ -89,10 +89,12 @@ static char titles[_T_MAX][TITLE_MAXLEN * sizeof(wchar_t)];
 static void make_aligned_titles(void)
 {
 	unsigned int i;
-	size_t max = 0;
+	size_t maxlen = 0;
+	int maxcol = 0;
 	static const wchar_t title_suffix[] = L" :";
 	wchar_t wbuf[ARRAYSIZE(titles)][TITLE_MAXLEN + ARRAYSIZE(title_suffix)];
 	size_t wlen[ARRAYSIZE(wbuf)];
+	int wcol[ARRAYSIZE(wbuf)];
 	char *buf[ARRAYSIZE(wbuf)];
 	buf[T_ARCHITECTURE] = _("Architecture");
 	buf[T_BACKUP_FILES] = _("Backup Files");
@@ -125,14 +127,19 @@ static void make_aligned_titles(void)
 
 	for(i = 0; i < ARRAYSIZE(wbuf); i++) {
 		wlen[i] = mbstowcs(wbuf[i], buf[i], strlen(buf[i]) + 1);
-		if(wlen[i] > max) {
-			max = wlen[i];
+		wcol[i] = wcswidth(wbuf[i], wlen[i]);
+		if(wcol[i] > maxcol) {
+			maxcol = wcol[i];
+		}
+		if(wlen[i] > maxlen) {
+			maxlen = wlen[i];
 		}
 	}
 
 	for(i = 0; i < ARRAYSIZE(wbuf); i++) {
-		wmemset(wbuf[i] + wlen[i], L' ', max - wlen[i]);
-		wmemcpy(wbuf[i] + max, title_suffix, ARRAYSIZE(title_suffix));
+		size_t padlen = maxcol - wcol[i];
+		wmemset(wbuf[i] + wlen[i], L' ', padlen);
+		wmemcpy(wbuf[i] + wlen[i] + padlen, title_suffix, ARRAYSIZE(title_suffix));
 		wcstombs(titles[i], wbuf[i], sizeof(wbuf[i]));
 	}
 }
-- 
2.7.0


More information about the pacman-dev mailing list