/** Displays the list in table format * * @param title the tables title * @param header the column headers. column count is determined by the nr * of headers * @param rows the rows to display as a list of lists of strings. the outer * list represents the rows, the inner list the cells (= columns) * @param cols the number of columns available in the terminal * @return -1 if not enough terminal cols available, else 0 */ static int table_display(const alpm_list_t *header, const alpm_list_t *rows, unsigned short cols) { const unsigned short padding = 2; const alpm_list_t *i, *first; size_t *widths = NULL, totalcols, totalwidth; int *has_data = NULL; if(rows == NULL) { return 0; } /* we want the first row. if no headers are provided, use the first * entry of the rows array. */ first = header ? header : rows->data; totalcols = alpm_list_count(first); totalwidth = table_calc_widths(first, rows, padding, totalcols, &widths, &has_data); /* return -1 if terminal is not wide enough */ if(totalwidth > cols) { pm_printf(ALPM_LOG_WARNING, _("insufficient columns available for table display\n")); return -1; } if(!totalwidth || !widths || !has_data) { return -1; } if (header) { table_print_line(header, padding, totalcols, widths, has_data); printf("\n"); } for(i = rows; i; i = alpm_list_next(i)) { table_print_line(i->data, padding, totalcols, widths, has_data); } free(widths); free(has_data); return 0; }
/** Displays the list in table format * * @param title the tables title * @param header the column headers. column count is determined by the nr * of headers * @param rows the rows to display as a list of lists of strings. the outer * list represents the rows, the inner list the cells (= columns) * @param cols the number of columns available in the terminal * @return -1 if not enough terminal cols available, else 0 */ static int table_display(const char *title, const alpm_list_t *header, const alpm_list_t *rows, unsigned short cols) { const unsigned short padding = 2; const alpm_list_t *i; size_t *widths = NULL, totalcols, totalwidth; int *has_data = NULL; if(rows == NULL || header == NULL) { return 0; } totalcols = alpm_list_count(header); totalwidth = table_calc_widths(header, rows, padding, totalcols, &widths, &has_data); /* return -1 if terminal is not wide enough */ if(totalwidth > cols) { pm_printf(ALPM_LOG_WARNING, _("insufficient columns available for table display\n")); return -1; } if(!totalwidth || !widths || !has_data) { return -1; } if(title != NULL) { printf("%s\n\n", title); } table_print_line(header, padding, totalcols, widths, has_data); printf("\n"); for(i = rows; i; i = alpm_list_next(i)) { table_print_line(i->data, padding, totalcols, widths, has_data); } free(widths); free(has_data); return 0; }