This can make it easier to read tables that contain wide data in some columns.
Signed-off-by: Ben Pfaff <[email protected]> --- NEWS | 2 ++ lib/table.c | 22 +++++++++++++++++----- lib/table.h | 13 ++++++++++--- lib/table.man | 5 +++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 188a0757a797..a6db0dce78be 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ Post-v2.8.0 -------------------- - NSH implementation now conforms to latest draft (draft-ietf-sfc-nsh-28). + - ovs-vsctl and other commands that display data in tables now support a + --max-column-width option to limit column width. - OVN: * The "requested-chassis" option for a logical switch port now accepts a chassis "hostname" in addition to a chassis "name". diff --git a/lib/table.c b/lib/table.c index c8e9f4153d3a..98599d67cfcf 100644 --- a/lib/table.c +++ b/lib/table.c @@ -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); } diff --git a/lib/table.h b/lib/table.h index fee36897f81f..f1221716a8f3 100644 --- a/lib/table.h +++ b/lib/table.h @@ -78,21 +78,24 @@ struct table_style { enum cell_format cell_format; /* CF_*. */ bool headings; /* Include headings? */ int json_flags; /* CF_JSON: Flags for json_to_string(). */ + int max_column_width; /* CF_STRING: Limit for column width. */ }; -#define TABLE_STYLE_DEFAULT { TF_LIST, CF_STRING, true, JSSF_SORT } +#define TABLE_STYLE_DEFAULT { TF_LIST, CF_STRING, true, JSSF_SORT, 0 } #define TABLE_OPTION_ENUMS \ OPT_NO_HEADINGS, \ OPT_PRETTY, \ - OPT_BARE + OPT_BARE, \ + OPT_MAX_COLUMN_WIDTH #define TABLE_LONG_OPTIONS \ {"format", required_argument, NULL, 'f'}, \ {"data", required_argument, NULL, 'd'}, \ {"no-headings", no_argument, NULL, OPT_NO_HEADINGS}, \ {"pretty", no_argument, NULL, OPT_PRETTY}, \ - {"bare", no_argument, NULL, OPT_BARE} + {"bare", no_argument, NULL, OPT_BARE}, \ + {"max-column-width", required_argument, NULL, OPT_MAX_COLUMN_WIDTH} #define TABLE_OPTION_HANDLERS(STYLE) \ case 'f': \ @@ -115,6 +118,10 @@ struct table_style { (STYLE)->format = TF_LIST; \ (STYLE)->cell_format = CF_BARE; \ (STYLE)->headings = false; \ + break; \ + \ + case OPT_MAX_COLUMN_WIDTH: \ + (STYLE)->max_column_width = atoi(optarg); \ break; void table_parse_format(struct table_style *, const char *format); diff --git a/lib/table.man b/lib/table.man index 2b38741105d5..bec8292c6862 100644 --- a/lib/table.man +++ b/lib/table.man @@ -69,3 +69,8 @@ This option does not affect JSON in tables, which is always printed compactly. .IP "\fB\-\-bare\fR" Equivalent to \fB\-\-format=list \-\-data=bare \-\-no\-headings\fR. +.IP "\fB\-\-max\-column-width=\fIn\fR" +For table output only, limits the width of any column in the output to +\fIn\fR columns. Longer cell data is truncated to fit, as necessary. +Columns are always wide enough to display the column names, if the +heading row is printed. -- 2.10.2 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
