diff options
author | Dan McGee <dan@archlinux.org> | 2008-02-22 23:47:03 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2008-02-22 23:47:03 -0600 |
commit | 4c14dcc5804aa6736616d35aac4db1ea3b29985e (patch) | |
tree | 75a0378a5c82eb6927ef9f635ac7a33ee6171d55 /src/pacman/util.c | |
parent | a6470956bc2db5acb821485e590822953966586a (diff) |
A few more wide character output fixes
Fix up the indentprint and list printing functions so they work properly.
This output can be seen in places such as -Ss, -Si, -Qs, and -Qi.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'src/pacman/util.c')
-rw-r--r-- | src/pacman/util.c | 62 |
1 files changed, 43 insertions, 19 deletions
diff --git a/src/pacman/util.c b/src/pacman/util.c index 20f4c072..42436e26 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -34,6 +34,7 @@ #include <dirent.h> #include <unistd.h> #include <limits.h> +#include <wchar.h> #include <alpm.h> #include <alpm_list.h> @@ -219,37 +220,46 @@ char *mdirname(const char *path) */ void indentprint(const char *str, int indent) { - const char *p = str; - int cidx = indent; + wchar_t *wcstr; + const wchar_t *p; + int len, cidx; + + len = strlen(str) + 1; + wcstr = calloc(len, sizeof(wchar_t)); + len = mbstowcs(wcstr, str, len); + p = wcstr; + cidx = indent; while(*p) { - if(*p == ' ') { - const char *next = NULL; - int len; + if(*p == L' ') { + const wchar_t *q, *next; p++; - if(p == NULL || *p == ' ') continue; - next = strchr(p, ' '); + if(p == NULL || *p == L' ') continue; + next = wcschr(p, L' '); if(next == NULL) { - next = p + mbstowcs(NULL, p, 0); + next = p + wcslen(p); + } + /* len captures # cols */ + len = 0; + q = p; + while(q < next) { + len += wcwidth(*q++); } - len = next - p; if(len > (getcols() - cidx - 1)) { - /* newline */ - int i; - fprintf(stdout, "\n"); - for(i = 0; i < indent; i++) { - fprintf(stdout, " "); - } + /* wrap to a newline and reindent */ + fprintf(stdout, "\n%-*s", indent, ""); cidx = indent; } else { printf(" "); cidx++; } + continue; } - fprintf(stdout, "%c", *p); + fprintf(stdout, "%lc", (wint_t)*p); + cidx += wcwidth(*p); p++; - cidx++; } + free(wcstr); } /* Convert a string to uppercase @@ -389,14 +399,28 @@ void list_display(const char *title, const alpm_list_t *list) { const alpm_list_t *i; int cols, len; + wchar_t *wcstr; + + /* len goes from # bytes -> # chars -> # cols */ + len = strlen(title) + 1; + wcstr = calloc(len, sizeof(wchar_t)); + len = mbstowcs(wcstr, title, len); + len = wcswidth(wcstr, len); + free(wcstr); - len = mbstowcs(NULL, title, 0); printf("%s ", title); if(list) { for(i = list, cols = len; i; i = alpm_list_next(i)) { char *str = alpm_list_getdata(i); - int s = mbstowcs(NULL, str, 0) + 2; + /* s goes from # bytes -> # chars -> # cols */ + int s = strlen(str) + 1; + wcstr = calloc(s, sizeof(wchar_t)); + s = mbstowcs(wcstr, str, s); + s = wcswidth(wcstr, s); + free(wcstr); + /* two additional spaces are added to the length */ + s += 2; int maxcols = getcols(); if(s + cols >= maxcols) { int i; |