1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-21 23:38:49 -05:00

dump_pkg_info: fix wide character title alignment

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@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Andrew Gregory 2016-02-01 12:14:47 -05:00 committed by Allan McRae
parent 252183b409
commit c1bb41a037

View File

@ -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]));
}
}