Table format creation code cleanup
Better scoping of variables for the most part, and ensure we are using string_length() and not strlen() as appropriate. Also refactor the longest cell code to call string_length() a lot less; by simply using an array of max sizes we don't have to recompute values nearly as much. Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
1b8bb7c1cd
commit
bcc6a5d72d
|
@ -459,7 +459,7 @@ static size_t string_length(const char *s)
|
||||||
int len;
|
int len;
|
||||||
wchar_t *wcstr;
|
wchar_t *wcstr;
|
||||||
|
|
||||||
if(!s) {
|
if(!s || s[0] == '\0') {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/* len goes from # bytes -> # chars -> # cols */
|
/* len goes from # bytes -> # chars -> # cols */
|
||||||
|
@ -506,47 +506,53 @@ static void table_print_line(const alpm_list_t *line,
|
||||||
static alpm_list_t *table_create_format(const alpm_list_t *header,
|
static alpm_list_t *table_create_format(const alpm_list_t *header,
|
||||||
const alpm_list_t *rows)
|
const alpm_list_t *rows)
|
||||||
{
|
{
|
||||||
alpm_list_t *longest_str, *longest_strs = NULL;
|
|
||||||
alpm_list_t *formats = NULL;
|
alpm_list_t *formats = NULL;
|
||||||
const alpm_list_t *i, *row, *cell;
|
const alpm_list_t *i;
|
||||||
char *str, *formatstr;
|
|
||||||
const unsigned short padding = 2;
|
const unsigned short padding = 2;
|
||||||
size_t colwidth, totalwidth = 0;
|
size_t curcol, totalcols, totalwidth = 0;
|
||||||
size_t curcol = 0;
|
size_t *colwidths;
|
||||||
|
|
||||||
|
totalcols = alpm_list_count(header);
|
||||||
|
colwidths = malloc(totalcols * sizeof(size_t));
|
||||||
|
if(!colwidths) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
/* header determines column count and initial values of longest_strs */
|
/* header determines column count and initial values of longest_strs */
|
||||||
for(i = header; i; i = alpm_list_next(i)) {
|
for(i = header, curcol = 0; i; i = alpm_list_next(i), curcol++) {
|
||||||
longest_strs = alpm_list_add(longest_strs, alpm_list_getdata(i));
|
colwidths[curcol] = string_length(alpm_list_getdata(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now find the longest string in each column */
|
/* now find the longest string in each column */
|
||||||
for(longest_str = longest_strs; longest_str;
|
for(i = rows; i; i = alpm_list_next(i)) {
|
||||||
longest_str = alpm_list_next(longest_str), curcol++) {
|
/* grab first column of each row and iterate through columns */
|
||||||
for(i = rows; i; i = alpm_list_next(i)) {
|
const alpm_list_t *j = alpm_list_getdata(i);
|
||||||
row = alpm_list_getdata(i);
|
for(curcol = 0; j; j = alpm_list_next(j), curcol++) {
|
||||||
cell = alpm_list_nth(row, curcol);
|
char *str = alpm_list_getdata(j);
|
||||||
str = alpm_list_getdata(cell);
|
size_t str_len = string_length(str);
|
||||||
|
|
||||||
if(strlen(str) > strlen(alpm_list_getdata(longest_str))) {
|
if(str_len > colwidths[curcol]) {
|
||||||
longest_str->data = str;
|
fprintf(stderr, "replace width: %zd (%zd -> %zd) %s\n",
|
||||||
|
curcol, colwidths[curcol], str_len, str);
|
||||||
|
colwidths[curcol] = str_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now use the column width info to generate format strings */
|
/* now use the column width info to generate format strings */
|
||||||
for(i = longest_strs; i; i = alpm_list_next(i)) {
|
for(curcol = 0; curcol < totalcols; curcol++) {
|
||||||
const char *display;
|
const char *display;
|
||||||
colwidth = string_length(alpm_list_getdata(i)) + padding;
|
char *formatstr;
|
||||||
|
size_t colwidth = colwidths[curcol] + padding;
|
||||||
totalwidth += colwidth;
|
totalwidth += colwidth;
|
||||||
|
|
||||||
/* right align the last column for a cleaner table display */
|
/* right align the last column for a cleaner table display */
|
||||||
display = (alpm_list_next(i) != NULL) ? "%%-%ds" : "%%%ds";
|
display = (curcol + 1 < totalcols) ? "%%-%ds" : "%%%ds";
|
||||||
pm_asprintf(&formatstr, display, colwidth);
|
pm_asprintf(&formatstr, display, colwidth);
|
||||||
|
|
||||||
formats = alpm_list_add(formats, formatstr);
|
formats = alpm_list_add(formats, formatstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
alpm_list_free(longest_strs);
|
free(colwidths);
|
||||||
|
|
||||||
/* return NULL if terminal is not wide enough */
|
/* return NULL if terminal is not wide enough */
|
||||||
if(totalwidth > getcols()) {
|
if(totalwidth > getcols()) {
|
||||||
|
|
Loading…
Reference in New Issue