]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
vshTableGetColumnsWidths: Include spacing in lenght calculation
authorPeter Krempa <pkrempa@redhat.com>
Fri, 17 Apr 2026 12:26:42 +0000 (14:26 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 24 Apr 2026 09:29:01 +0000 (11:29 +0200)
Modify the array holding lengths of individual columns in the table to
include the spacing. This will be used later when we'll allow to modify
the spacing.

To do this we'll include the 3 extra spaces as lengths as well as fix
the two loops using the value to use it directly.

Since the spacing is not included in the string the code in
'vshTableRowPrint' is modified to explicitly add the spacing instead of
adding a constant to the calculated length.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tools/vsh-table.c

index 0c96407f03c1af3fb953ecb36626428f55e399ce..a247585b507e906a87969ee9ac2c36d4f386dde0 100644 (file)
@@ -257,7 +257,8 @@ vshTableSafeEncode(const char *s, size_t *width)
  *
  * Fill passed @maxwidths and @widths arrays with maximum number
  * of characters for columns and number of character per each
- * table cell, respectively.
+ * table cell, respectively. Both lengths include the extra whitespace for
+ * separation of columns.
  * Handle unicode strings (user must have multibyte locale)
  *
  * Return 0 in case of success, -1 otherwise.
@@ -284,6 +285,9 @@ vshTableGetColumnsWidths(vshTable *table,
             if (!tmp)
                 return -1;
 
+            /* include the built-in whitespace in the calculated length */
+            size += 3;
+
             VIR_FREE(row->cells[j]);
             row->cells[j] = tmp;
             widths[i][j] = size;
@@ -317,8 +321,9 @@ vshTableRowPrint(vshTableRow *row,
         virBufferAsprintf(buf, " %s", row->cells[i]);
 
         if (i < (row->ncells - 1)) {
-            for (j = 0; j < maxwidths[i] - widths[i] + 2; j++)
+            for (j = 0; j < maxwidths[i] - widths[i]; j++)
                 virBufferAddChar(buf, ' ');
+            virBufferAddLit(buf, "  ");
         }
     }
     virBufferAddChar(buf, '\n');
@@ -365,7 +370,7 @@ vshTablePrint(vshTable *table, bool header)
 
         /* print dividing line  */
         for (i = 0; i < table->rows[0]->ncells; i++) {
-            for (j = 0; j < maxwidths[i] + 3; j++)
+            for (j = 0; j < maxwidths[i]; j++)
                 virBufferAddChar(&buf, '-');
         }
         virBufferAddChar(&buf, '\n');