1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 08:18:51 -05:00

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>
This commit is contained in:
Dan McGee 2008-02-22 23:47:03 -06:00
parent a6470956bc
commit 4c14dcc580
2 changed files with 55 additions and 22 deletions

View File

@ -24,6 +24,7 @@
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <wchar.h>
#include <alpm.h> #include <alpm.h>
#include <alpm_list.h> #include <alpm_list.h>
@ -48,6 +49,8 @@ void dump_pkg_full(pmpkg_t *pkg, int level)
char bdatestr[50] = "", idatestr[50] = ""; char bdatestr[50] = "", idatestr[50] = "";
const alpm_list_t *i; const alpm_list_t *i;
alpm_list_t *requiredby = NULL, *depstrings = NULL; alpm_list_t *requiredby = NULL, *depstrings = NULL;
wchar_t *wcstr;
int len;
if(pkg == NULL) { if(pkg == NULL) {
return; return;
@ -86,8 +89,6 @@ void dump_pkg_full(pmpkg_t *pkg, int level)
requiredby = alpm_pkg_compute_requiredby(pkg); requiredby = alpm_pkg_compute_requiredby(pkg);
} }
descheader = _("Description : ");
/* actual output */ /* actual output */
if(level == 0) { if(level == 0) {
string_display(_("Filename :"), alpm_pkg_get_filename(pkg)); string_display(_("Filename :"), alpm_pkg_get_filename(pkg));
@ -136,8 +137,16 @@ void dump_pkg_full(pmpkg_t *pkg, int level)
} }
/* printed using a variable to make i18n safe */ /* printed using a variable to make i18n safe */
descheader = _("Description : ");
/* len goes from # bytes -> # chars -> # cols */
len = strlen(descheader) + 1;
wcstr = calloc(len, sizeof(wchar_t));
len = mbstowcs(wcstr, descheader, len);
len = wcswidth(wcstr, len);
free(wcstr);
/* we can finally print the darn thing */
printf("%s", descheader); printf("%s", descheader);
indentprint(alpm_pkg_get_desc(pkg), mbstowcs(NULL, descheader, 0)); indentprint(alpm_pkg_get_desc(pkg), len);
printf("\n\n"); printf("\n\n");
/* Print additional package info if info flag passed more than once */ /* Print additional package info if info flag passed more than once */

View File

@ -34,6 +34,7 @@
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
#include <wchar.h>
#include <alpm.h> #include <alpm.h>
#include <alpm_list.h> #include <alpm_list.h>
@ -219,37 +220,46 @@ char *mdirname(const char *path)
*/ */
void indentprint(const char *str, int indent) void indentprint(const char *str, int indent)
{ {
const char *p = str; wchar_t *wcstr;
int cidx = indent; 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) { while(*p) {
if(*p == ' ') { if(*p == L' ') {
const char *next = NULL; const wchar_t *q, *next;
int len;
p++; p++;
if(p == NULL || *p == ' ') continue; if(p == NULL || *p == L' ') continue;
next = strchr(p, ' '); next = wcschr(p, L' ');
if(next == NULL) { 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)) { if(len > (getcols() - cidx - 1)) {
/* newline */ /* wrap to a newline and reindent */
int i; fprintf(stdout, "\n%-*s", indent, "");
fprintf(stdout, "\n");
for(i = 0; i < indent; i++) {
fprintf(stdout, " ");
}
cidx = indent; cidx = indent;
} else { } else {
printf(" "); printf(" ");
cidx++; cidx++;
} }
continue;
} }
fprintf(stdout, "%c", *p); fprintf(stdout, "%lc", (wint_t)*p);
cidx += wcwidth(*p);
p++; p++;
cidx++;
} }
free(wcstr);
} }
/* Convert a string to uppercase /* 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; const alpm_list_t *i;
int cols, len; 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); printf("%s ", title);
if(list) { if(list) {
for(i = list, cols = len; i; i = alpm_list_next(i)) { for(i = list, cols = len; i; i = alpm_list_next(i)) {
char *str = alpm_list_getdata(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(); int maxcols = getcols();
if(s + cols >= maxcols) { if(s + cols >= maxcols) {
int i; int i;