2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-04 16:25:17 +00:00

table: Add --max-column-width option.

This can make it easier to read tables that contain wide data in some
columns.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Justin Pettit <jpettit@ovn.org>
This commit is contained in:
Ben Pfaff
2017-12-13 15:04:48 -08:00
parent bc7bcc408e
commit 80f66ee041
5 changed files with 40 additions and 14 deletions

View File

@@ -255,19 +255,31 @@ table_print_table__(const struct table *table, const struct table_style *style)
puts(table->caption);
}
widths = xmalloc(table->n_columns * sizeof *widths);
widths = xzalloc(table->n_columns * sizeof *widths);
for (x = 0; x < table->n_columns; x++) {
const struct column *column = &table->columns[x];
widths[x] = strlen(column->heading);
int w = 0;
for (y = 0; y < table->n_rows; y++) {
const char *text = cell_to_text(table_cell__(table, y, x), style);
size_t length = strlen(text);
if (length > widths[x]) {
widths[x] = length;
if (length > w) {
w = length;
}
}
int max = style->max_column_width;
if (max > 0 && w > max) {
w = max;
}
if (style->headings) {
int min = strlen(column->heading);
if (w < min) {
w = min;
}
}
widths[x] = w;
}
if (style->headings) {
@@ -295,7 +307,7 @@ table_print_table__(const struct table *table, const struct table_style *style)
if (x) {
ds_put_char(&line, ' ');
}
ds_put_format(&line, "%-*s", widths[x], text);
ds_put_format(&line, "%-*.*s", widths[x], widths[x], text);
}
table_print_table_line__(&line);
}