diff options
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | lib/table.c | 22 | ||||
-rw-r--r-- | lib/table.h | 13 | ||||
-rw-r--r-- | lib/table.man | 5 | ||||
-rw-r--r-- | tests/ovsdb-client.at | 12 |
5 files changed, 40 insertions, 14 deletions
@@ -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". diff --git a/lib/table.c b/lib/table.c index c8e9f4153..98599d67c 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 fee36897f..f1221716a 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 2b3874110..bec8292c6 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. diff --git a/tests/ovsdb-client.at b/tests/ovsdb-client.at index ea4755e8a..467175f1a 100644 --- a/tests/ovsdb-client.at +++ b/tests/ovsdb-client.at @@ -64,12 +64,12 @@ AT_CHECK( ]]) AT_CHECK([ovsdb-client --no-headings dump ordinals | sort -k 3 | uuidfilt], [0], [dnl ordinals table -<0> "" 0 -<1> "" 1 -<2> "" 2 -<3> "" 3 -<4> "" 4 -<5> "" 5 +<0> "" 0 +<1> "" 1 +<2> "" 2 +<3> "" 3 +<4> "" 4 +<5> "" 5 ]) AT_CHECK([ovsdb-client restore < backup]) AT_CHECK([ovsdb-client dump | tr -s ' ' | sort -k 3 | uuidfilt], [0], [dnl |