]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: cmdNodeMemStats: Rework to vshTable
authorPeter Krempa <pkrempa@redhat.com>
Fri, 17 Apr 2026 12:43:44 +0000 (14:43 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 24 Apr 2026 09:29:01 +0000 (11:29 +0200)
After recent addition of 'available' field the hardcoded alignments no
longer match:

  $ virsh nodememstats
  total  :             63393452 KiB
  free   :              4046756 KiB
  available:             35747628 KiB
  buffers:              2291748 KiB
  cached :             24086464 KiB

To address the issue switch to use dynamically aligned columns via
vshTable infrastructure:

  $ virsh nodememstats
  total    :   63393452 KiB
  free     :    3888776 KiB
  available:   35640268 KiB
  buffers  :    2291768 KiB
  cached   :   24089916 KiB

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

index dd98917fa88c1570c752697ee21a9b6e85c73c6b..ef91e22fed3601f24ec651132d644d8f8c77fdcc 100644 (file)
@@ -33,6 +33,7 @@
 #include "virfile.h"
 #include "virenum.h"
 #include "virsh-util.h"
+#include "vsh-table.h"
 
 /*
  * "capabilities" command
@@ -889,6 +890,8 @@ cmdNodeMemStats(vshControl *ctl, const vshCmd *cmd)
     int cellNum = VIR_NODE_MEMORY_STATS_ALL_CELLS;
     g_autofree virNodeMemoryStatsPtr params = NULL;
     virshControl *priv = ctl->privData;
+    g_autoptr(vshTable) table = vshTableNew("field", ":", "value", NULL);
+    g_autofree char *tblstr = NULL;
 
     if (vshCommandOptInt(ctl, cmd, "cell", &cellNum) < 0)
         return false;
@@ -912,8 +915,18 @@ cmdNodeMemStats(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
 
-    for (i = 0; i < nparams; i++)
-        vshPrint(ctl, "%-7s: %20llu KiB\n", params[i].field, params[i].value);
+    for (i = 0; i < nparams; i++) {
+        g_autofree char *val = g_strdup_printf("%llu KiB", params[i].value);
+
+        vshTableRowAppendFlags(table,
+                               VSH_TABLE_CELL_SKIP_LEADING | VSH_TABLE_CELL_SKIP_TRAILING, params[i].field,
+                               VSH_TABLE_CELL_SKIP_LEADING, ":",
+                               VSH_TABLE_CELL_ALIGN_RIGHT, val,
+                               0, NULL);
+    }
+
+    tblstr = vshTablePrintToString(table, false);
+    vshPrint(ctl, "%s", tblstr);
 
     return true;
 }