From nobody Mon Feb 9 02:11:40 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 1537280533334781.0974100055284; Tue, 18 Sep 2018 07:22:13 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 61F0F30014D1; Tue, 18 Sep 2018 14:22:11 +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 8E6A17A41D; Tue, 18 Sep 2018 14:22:10 +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 2E17E181A55B; Tue, 18 Sep 2018 14:22:10 +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 w8IEM7iJ000414 for ; Tue, 18 Sep 2018 10:22:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 249A27A422; Tue, 18 Sep 2018 14:22:07 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id 71730176A0; Tue, 18 Sep 2018 14:22:06 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Tue, 18 Sep 2018 16:21:36 +0200 Message-Id: <20180918142137.16258-13-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 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Tue, 18 Sep 2018 14:22:12 +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 | 127 +++++++++---------------------------------- 1 file changed, 27 insertions(+), 100 deletions(-) diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index 42d11701ec..071882c628 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) - goto cleanup; - /* 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"); + table =3D vshTableNew("Name", "Path", "Type", "Capacity", "Allocation"= , NULL); + if (!table) + goto cleanup; =20 /* Display the volume info rows */ 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