From nobody Mon Feb 9 16:51:30 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 1537539478032387.53652034131585; Fri, 21 Sep 2018 07:17:58 -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 2DC27C03D47F; Fri, 21 Sep 2018 14:17:56 +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 ED79280F75; Fri, 21 Sep 2018 14:17:55 +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 9BF41181A530; Fri, 21 Sep 2018 14:17:55 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8LEHeaZ028326 for ; Fri, 21 Sep 2018 10:17:40 -0400 Received: by smtp.corp.redhat.com (Postfix) id 28C938209A; Fri, 21 Sep 2018 14:17:40 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76F5053C21; Fri, 21 Sep 2018 14:17:39 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Fri, 21 Sep 2018 16:17:23 +0200 Message-Id: <20180921141724.18879-13-skobyda@redhat.com> In-Reply-To: <20180921141724.18879-1-skobyda@redhat.com> References: <20180921141724.18879-1-skobyda@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Simon Kobyda Subject: [libvirt] [PATCH v2 12/13] virsh: Implement vshTable API to vol-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.31]); Fri, 21 Sep 2018 14:17:56 +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 calculation used by the local table. Signed-off-by: Simon Kobyda --- tools/virsh-volume.c | 129 ++++++++++--------------------------------- 1 file changed, 28 insertions(+), 101 deletions(-) diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index 42d11701ec..6cd989446e 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -42,6 +42,7 @@ #include "virsh-pool.h" #include "virxml.h" #include "virstring.h" +#include "vsh-table.h" =20 #define VIRSH_COMMON_OPT_POOL_FULL \ VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"), \ @@ -1382,16 +1383,11 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRI= BUTE_UNUSED) { virStorageVolInfo volumeInfo; virStoragePoolPtr pool; - char *outputStr =3D NULL; const char *unit; double val; bool details =3D vshCommandOptBool(cmd, "details"); size_t i; bool ret =3D false; - int stringLength =3D 0; - size_t allocStrLength =3D 0, capStrLength =3D 0; - size_t nameStrLength =3D 0, pathStrLength =3D 0; - size_t typeStrLength =3D 0; struct volInfoText { char *allocation; char *capacity; @@ -1400,6 +1396,7 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBU= TE_UNUSED) }; struct volInfoText *volInfoTexts =3D NULL; virshStorageVolListPtr list =3D NULL; + vshTablePtr table =3D NULL; =20 /* Look up the pool information given to us by the user */ if (!(pool =3D virshCommandOptPool(ctl, cmd, "pool", NULL))) @@ -1446,36 +1443,6 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIB= UTE_UNUSED) "%.2lf %s", val, unit) < 0) goto cleanup; } - - /* Remember the largest length for each output string. - * This lets us displaying header and volume information rows - * using a single, properly sized, printf style output string. - */ - - /* Keep the length of name string if longest so far */ - stringLength =3D strlen(virStorageVolGetName(list->vols[i])); - if (stringLength > nameStrLength) - nameStrLength =3D stringLength; - - /* Keep the length of path string if longest so far */ - stringLength =3D strlen(volInfoTexts[i].path); - if (stringLength > pathStrLength) - pathStrLength =3D stringLength; - - /* Keep the length of type string if longest so far */ - stringLength =3D strlen(volInfoTexts[i].type); - if (stringLength > typeStrLength) - typeStrLength =3D stringLength; - - /* Keep the length of capacity string if longest so far */ - stringLength =3D strlen(volInfoTexts[i].capacity); - if (stringLength > capStrLength) - capStrLength =3D stringLength; - - /* Keep the length of allocation string if longest so far */ - stringLength =3D strlen(volInfoTexts[i].allocation); - if (stringLength > allocStrLength) - allocStrLength =3D stringLength; } } =20 @@ -1487,14 +1454,20 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRI= BUTE_UNUSED) /* Output basic info then return if --details option not selected */ if (!details) { /* The old output format */ - vshPrintExtra(ctl, " %-20s %-40s\n", _("Name"), _("Path")); - vshPrintExtra(ctl, "---------------------------------------" - "---------------------------------------\n"); + table =3D vshTableNew(_("Name"), _("Path"), NULL); + if (!table) + goto cleanup; + for (i =3D 0; i < list->nvols; i++) { - vshPrint(ctl, " %-20s %-40s\n", virStorageVolGetName(list->vol= s[i]), - volInfoTexts[i].path); + if (vshTableRowAppend(table, + virStorageVolGetName(list->vols[i]), + volInfoTexts[i].path, + NULL) < 0) + goto cleanup; } =20 + vshTablePrintToStdout(table, ctl); + /* Cleanup and return */ ret =3D true; goto cleanup; @@ -1502,75 +1475,30 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRI= BUTE_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 path header string if it's longest */ - stringLength =3D strlen(_("Path")); - if (stringLength > pathStrLength) - pathStrLength =3D stringLength; - - /* Use the length of type header string if it's longest */ - stringLength =3D strlen(_("Type")); - if (stringLength > typeStrLength) - typeStrLength =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; - - /* Display the string lengths for debugging */ - vshDebug(ctl, VSH_ERR_DEBUG, - "Longest name string =3D %zu chars\n", nameStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, - "Longest path string =3D %zu chars\n", pathStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, - "Longest type string =3D %zu chars\n", typeStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, - "Longest capacity string =3D %zu chars\n", capStrLength); - vshDebug(ctl, VSH_ERR_DEBUG, - "Longest allocation string =3D %zu chars\n", allocStrLength); - - if (virAsprintf(&outputStr, - " %%-%lus %%-%lus %%-%lus %%%lus %%%lus\n", - (unsigned long) nameStrLength, - (unsigned long) pathStrLength, - (unsigned long) typeStrLength, - (unsigned long) capStrLength, - (unsigned long) allocStrLength) < 0) + /* Insert the header into table */ + table =3D vshTableNew(_("Name"), _("Path"), _("Type"), _("Capacity"), = _("Allocation"), NULL); + if (!table) goto cleanup; =20 - /* Display the header */ - vshPrintExtra(ctl, outputStr, _("Name"), _("Path"), _("Type"), - _("Capacity"), _("Allocation")); - for (i =3D nameStrLength + pathStrLength + typeStrLength - + capStrLength + allocStrLength - + 10; i > 0; i--) - vshPrintExtra(ctl, "-"); - vshPrintExtra(ctl, "\n"); - - /* Display the volume info rows */ + /* Insert the volume info rows into table */ for (i =3D 0; i < list->nvols; i++) { - vshPrint(ctl, outputStr, - virStorageVolGetName(list->vols[i]), - volInfoTexts[i].path, - volInfoTexts[i].type, - volInfoTexts[i].capacity, - volInfoTexts[i].allocation); + if (vshTableRowAppend(table, + virStorageVolGetName(list->vols[i]), + volInfoTexts[i].path, + volInfoTexts[i].type, + volInfoTexts[i].capacity, + volInfoTexts[i].allocation, + NULL) < 0) + goto cleanup; } =20 + vshTablePrintToStdout(table, ctl); + /* Cleanup and return */ ret =3D true; =20 cleanup: + vshTableFree(table); =20 /* Safely free the memory allocated in this function */ if (list && list->nvols) { @@ -1584,7 +1512,6 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBU= TE_UNUSED) } =20 /* Cleanup remaining memory */ - VIR_FREE(outputStr); VIR_FREE(volInfoTexts); virStoragePoolFree(pool); virshStorageVolListFree(list); --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list