2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-22 09:58:01 +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

2
NEWS
View File

@ -8,6 +8,8 @@ Post-v2.8.0
* ovsdb-client: New "get-schema-cksum" and "query" commands.
* ovsdb-client: New "backup" and "restore" commands.
* ovsdb-tool: New "db-name" and "schema-name" commands.
- 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".

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);
}

View File

@ -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);

View File

@ -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.