From nobody Mon Feb 9 00:20:16 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1537280547239413.9473607273709; Tue, 18 Sep 2018 07:22:27 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 21B5C32B669; Tue, 18 Sep 2018 14:22:25 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C8E0E7B015; Tue, 18 Sep 2018 14:22:24 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 773F8181A13A; Tue, 18 Sep 2018 14:22:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8IEM6FR000405 for ; Tue, 18 Sep 2018 10:22:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 21949176A0; Tue, 18 Sep 2018 14:22:06 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C8437A422; Tue, 18 Sep 2018 14:22:05 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Tue, 18 Sep 2018 16:21:35 +0200 Message-Id: <20180918142137.16258-12-skobyda@redhat.com> In-Reply-To: <20180918142137.16258-1-skobyda@redhat.com> References: <20180918142137.16258-1-skobyda@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Simon Kobyda Subject: [libvirt] [PATCH 11/13] virsh: Implement vshTable API to pool-list X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 18 Sep 2018 14:22:26 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Local lengthy unicode-unreliable table formatting was replaced by new API. Great example of how new API saves space and time. Removed a lot of string lenght canculation used by the local table. Signed-off-by: Simon Kobyda --- tools/virsh-pool.c | 160 +++++++++------------------------------------ 1 file changed, 30 insertions(+), 130 deletions(-) diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c index 89206a48f5..724864a935 100644 --- a/tools/virsh-pool.c +++ b/tools/virsh-pool.c @@ -33,6 +33,7 @@ #include "conf/storage_conf.h" #include "virstring.h" #include "virtime.h" +#include "vsh-table.h" =20 #define VIRSH_COMMON_OPT_POOL_FULL(cflags) \ VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"), cflags) @@ -1113,10 +1114,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRI= BUTE_UNUSED) virStoragePoolInfo info; size_t i; bool ret =3D false; - size_t stringLength =3D 0, nameStrLength =3D 0; - size_t autostartStrLength =3D 0, persistStrLength =3D 0; - size_t stateStrLength =3D 0, capStrLength =3D 0; - size_t allocStrLength =3D 0, availStrLength =3D 0; struct poolInfoText { char *state; char *autostart; @@ -1133,7 +1130,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIB= UTE_UNUSED) bool inactive, all; bool uuid =3D false; bool name =3D false; - char *outputStr =3D NULL; + vshTablePtr table =3D NULL; =20 inactive =3D vshCommandOptBool(cmd, "inactive"); all =3D vshCommandOptBool(cmd, "all"); @@ -1260,11 +1257,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRI= BUTE_UNUSED) else poolInfoTexts[i].persistent =3D vshStrdup(ctl, persistent ? _("yes") : _("no"= )); - - /* Keep the length of persistent string if longest so far */ - stringLength =3D strlen(poolInfoTexts[i].persistent); - if (stringLength > persistStrLength) - persistStrLength =3D stringLength; } =20 /* Collect further extended information about the pool */ @@ -1310,21 +1302,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRI= BUTE_UNUSED) poolInfoTexts[i].allocation =3D vshStrdup(ctl, _("-")); poolInfoTexts[i].available =3D vshStrdup(ctl, _("-")); } - - /* Keep the length of capacity string if longest so far */ - stringLength =3D strlen(poolInfoTexts[i].capacity); - if (stringLength > capStrLength) - capStrLength =3D stringLength; - - /* Keep the length of allocation string if longest so far = */ - stringLength =3D strlen(poolInfoTexts[i].allocation); - if (stringLength > allocStrLength) - allocStrLength =3D stringLength; - - /* Keep the length of available string if longest so far */ - stringLength =3D strlen(poolInfoTexts[i].available); - if (stringLength > availStrLength) - availStrLength =3D stringLength; } else { /* --details option was not specified, only active/inactive * state strings are used */ @@ -1334,21 +1311,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRI= BUTE_UNUSED) poolInfoTexts[i].state =3D vshStrdup(ctl, _("inactive"= )); } } - - /* Keep the length of name string if longest so far */ - stringLength =3D strlen(virStoragePoolGetName(list->pools[i])); - if (stringLength > nameStrLength) - nameStrLength =3D stringLength; - - /* Keep the length of state string if longest so far */ - stringLength =3D strlen(poolInfoTexts[i].state); - if (stringLength > stateStrLength) - stateStrLength =3D stringLength; - - /* Keep the length of autostart string if longest so far */ - stringLength =3D strlen(poolInfoTexts[i].autostart); - if (stringLength > autostartStrLength) - autostartStrLength =3D stringLength; } =20 /* If the --details option wasn't selected, we output the pool @@ -1376,19 +1338,23 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTR= IBUTE_UNUSED) } =20 /* Output old style header */ - vshPrintExtra(ctl, " %-20s %-10s %-10s\n", _("Name"), _("State"), - _("Autostart")); - vshPrintExtra(ctl, "-------------------------------------------\n"= ); + table =3D vshTableNew("Name", "State", "Autostart", NULL); + if (!table) + goto cleanup; =20 /* Output old style pool info */ for (i =3D 0; i < list->npools; i++) { const char *name_str =3D virStoragePoolGetName(list->pools[i]); - vshPrint(ctl, " %-20s %-10s %-10s\n", - name_str, - poolInfoTexts[i].state, - poolInfoTexts[i].autostart); + if (vshTableRowAppend(table, + name_str, + poolInfoTexts[i].state, + poolInfoTexts[i].autostart, + NULL) < 0) + goto cleanup; } =20 + vshTablePrintToStdout(table, ctl); + /* Cleanup and return */ ret =3D true; goto cleanup; @@ -1396,99 +1362,33 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTR= IBUTE_UNUSED) =20 /* We only get here if the --details option was selected. */ =20 - /* Use the length of name header string if it's longest */ - stringLength =3D strlen(_("Name")); - if (stringLength > nameStrLength) - nameStrLength =3D stringLength; - - /* Use the length of state header string if it's longest */ - stringLength =3D strlen(_("State")); - if (stringLength > stateStrLength) - stateStrLength =3D stringLength; - - /* Use the length of autostart header string if it's longest */ - stringLength =3D strlen(_("Autostart")); - if (stringLength > autostartStrLength) - autostartStrLength =3D stringLength; - - /* Use the length of persistent header string if it's longest */ - stringLength =3D strlen(_("Persistent")); - if (stringLength > persistStrLength) - persistStrLength =3D stringLength; - - /* Use the length of capacity header string if it's longest */ - stringLength =3D strlen(_("Capacity")); - if (stringLength > capStrLength) - capStrLength =3D stringLength; - - /* Use the length of allocation header string if it's longest */ - stringLength =3D strlen(_("Allocation")); - if (stringLength > allocStrLength) - allocStrLength =3D stringLength; - - /* Use the length of available header string if it's longest */ - stringLength =3D strlen(_("Available")); - if (stringLength > availStrLength) - availStrLength =3D stringLength; - - /* Display the string lengths for debugging. */ - vshDebug(ctl, VSH_ERR_DEBUG, "Longest name string =3D %lu chars\n", - (unsigned long) nameStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, "Longest state string =3D %lu chars\n", - (unsigned long) stateStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, "Longest autostart string =3D %lu chars\n= ", - (unsigned long) autostartStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, "Longest persistent string =3D %lu chars\= n", - (unsigned long) persistStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, "Longest capacity string =3D %lu chars\n", - (unsigned long) capStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, "Longest allocation string =3D %lu chars\= n", - (unsigned long) allocStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, "Longest available string =3D %lu chars\n= ", - (unsigned long) availStrLength); - - /* Create the output template. Each column is sized according to - * the longest string. - */ - if (virAsprintf(&outputStr, - " %%-%lus %%-%lus %%-%lus %%-%lus %%%lus %%%lus = %%%lus\n", - (unsigned long) nameStrLength, - (unsigned long) stateStrLength, - (unsigned long) autostartStrLength, - (unsigned long) persistStrLength, - (unsigned long) capStrLength, - (unsigned long) allocStrLength, - (unsigned long) availStrLength) < 0) - goto cleanup; - /* Display the header */ - vshPrintExtra(ctl, outputStr, _("Name"), _("State"), _("Autostart"), - _("Persistent"), _("Capacity"), _("Allocation"), - _("Available")); - for (i =3D nameStrLength + stateStrLength + autostartStrLength - + persistStrLength + capStrLength - + allocStrLength + availStrLength - + 14; i > 0; i--) - vshPrintExtra(ctl, "-"); - vshPrintExtra(ctl, "\n"); + table =3D vshTableNew("Name", "State", "Autostart", "Persistent", + "Capacity", "Allocation", "Available", NULL); + if (!table) + goto cleanup; =20 /* Display the pool info rows */ for (i =3D 0; i < list->npools; i++) { - vshPrint(ctl, outputStr, - virStoragePoolGetName(list->pools[i]), - poolInfoTexts[i].state, - poolInfoTexts[i].autostart, - poolInfoTexts[i].persistent, - poolInfoTexts[i].capacity, - poolInfoTexts[i].allocation, - poolInfoTexts[i].available); + if (vshTableRowAppend(table, + virStoragePoolGetName(list->pools[i]), + poolInfoTexts[i].state, + poolInfoTexts[i].autostart, + poolInfoTexts[i].persistent, + poolInfoTexts[i].capacity, + poolInfoTexts[i].allocation, + poolInfoTexts[i].available, + NULL) < 0) + goto cleanup; } =20 + vshTablePrintToStdout(table, ctl); + /* Cleanup and return */ ret =3D true; =20 cleanup: - VIR_FREE(outputStr); + vshTableFree(table); if (list && list->npools) { for (i =3D 0; i < list->npools; i++) { VIR_FREE(poolInfoTexts[i].state); --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list