[PATCHv2 3/4] nvme: add common APIs for printing tabular format output
Hannes Reinecke
hare at suse.de
Mon Aug 18 00:27:16 PDT 2025
On 8/12/25 14:56, Nilay Shroff wrote:
> Some nvme-cli commands, such as nvme list and nvme list -v, support output
> in tabular format. Currently, the table output is not well aligned because
> column widths are fixed at print time, regardless of the length of the data
> in each column. This often results in uneven and hard-to-read output.For
> any new CLI command that requires tabular output, developers must manually
> specify the column width and row value width, which is both error-prone and
> inconsistent.
>
> This patch introduces a set of common table APIs that:
> - Automatically calculate column widths based on the content
> - Maintain proper alignment regardless of value length
> - Simplify adding tabular output support to new and existing commands
>
> The new APIs are:
> 1. table_init() — Allocate a table instance.
> 2. table_add_columns() — Add column definitions (struct table_column),
> including name and alignment (LEFT, RIGHT, CENTERED).
> 3. table_get_row_id() — Reserve a row index for inserting values.
> 4. table_add_row() — Add a row to the table.
> 5. table_print() — Print the table with auto-calculated widths.
> 6. table_free() — Free resources allocated for the table.
>
> For adding values, the following setter APIs are provided, each
> supporting alignment types (LEFT, RIGHT, or CENTERED):
> - table_set_value_str()
> - table_set_value_int()
> - table_set_value_unsigned()
> - table_set_value_long()
> - table_set_value_unsigned_long()
>
> Usage steps:
> 1. Call table_init() to create a table handle.
> 2. Define an array of struct table_column specifying column names and
> alignment, then call table_add_columns().
> 3. Obtain a row ID using table_get_row_id() and set values using the
> appropriate setter table APIs : table_set_value_*() function.
> 4. Add the completed row using table_add_row().
> 5. Repeat steps 3–4 for each additional row.
> 5. Call table_print() to display the table.
> 6. Call table_free() to release table resources.
>
> With these APIs, developers no longer need to pre-calculate column or row
> widths. The output is consistently aligned and easy to read.
>
> Suggested-by: Daniel Wagner <dwagner at suse.de>
> Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
> ---
> util/meson.build | 3 +-
> util/table.c | 278 +++++++++++++++++++++++++++++++++++++++++++++++
> util/table.h | 117 ++++++++++++++++++++
> 3 files changed, 397 insertions(+), 1 deletion(-)
> create mode 100644 util/table.c
> create mode 100644 util/table.h
>
Sheesh. Can o'worms.
Displaying a table on an ASCII terminal is black magic (check
manpage for ncurses ...), and I'm pretty certain that printing
spaces for alignment is _not_ the way to go.
If you really want to implement this then at the very least use
tabs. Ideally check with the ncurses manpage how one can display
a table with the correct escape characters.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare at suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
More information about the Linux-nvme
mailing list