virsh: Implement vshTable API to pool-list

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 <skobyda@redhat.com>
This commit is contained in:
Simon Kobyda 2018-09-21 16:17:22 +02:00 committed by Michal Privoznik
parent 3072ded354
commit 79eec7992b
1 changed files with 31 additions and 131 deletions

View File

@ -33,6 +33,7 @@
#include "conf/storage_conf.h" #include "conf/storage_conf.h"
#include "virstring.h" #include "virstring.h"
#include "virtime.h" #include "virtime.h"
#include "vsh-table.h"
#define VIRSH_COMMON_OPT_POOL_FULL(cflags) \ #define VIRSH_COMMON_OPT_POOL_FULL(cflags) \
VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"), cflags) VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"), cflags)
@ -1113,10 +1114,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
virStoragePoolInfo info; virStoragePoolInfo info;
size_t i; size_t i;
bool ret = false; bool ret = false;
size_t stringLength = 0, nameStrLength = 0;
size_t autostartStrLength = 0, persistStrLength = 0;
size_t stateStrLength = 0, capStrLength = 0;
size_t allocStrLength = 0, availStrLength = 0;
struct poolInfoText { struct poolInfoText {
char *state; char *state;
char *autostart; char *autostart;
@ -1133,7 +1130,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
bool inactive, all; bool inactive, all;
bool uuid = false; bool uuid = false;
bool name = false; bool name = false;
char *outputStr = NULL; vshTablePtr table = NULL;
inactive = vshCommandOptBool(cmd, "inactive"); inactive = vshCommandOptBool(cmd, "inactive");
all = vshCommandOptBool(cmd, "all"); all = vshCommandOptBool(cmd, "all");
@ -1260,11 +1257,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
else else
poolInfoTexts[i].persistent = vshStrdup(ctl, persistent ? poolInfoTexts[i].persistent = vshStrdup(ctl, persistent ?
_("yes") : _("no")); _("yes") : _("no"));
/* Keep the length of persistent string if longest so far */
stringLength = strlen(poolInfoTexts[i].persistent);
if (stringLength > persistStrLength)
persistStrLength = stringLength;
} }
/* Collect further extended information about the pool */ /* Collect further extended information about the pool */
@ -1310,21 +1302,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
poolInfoTexts[i].allocation = vshStrdup(ctl, _("-")); poolInfoTexts[i].allocation = vshStrdup(ctl, _("-"));
poolInfoTexts[i].available = vshStrdup(ctl, _("-")); poolInfoTexts[i].available = vshStrdup(ctl, _("-"));
} }
/* Keep the length of capacity string if longest so far */
stringLength = strlen(poolInfoTexts[i].capacity);
if (stringLength > capStrLength)
capStrLength = stringLength;
/* Keep the length of allocation string if longest so far */
stringLength = strlen(poolInfoTexts[i].allocation);
if (stringLength > allocStrLength)
allocStrLength = stringLength;
/* Keep the length of available string if longest so far */
stringLength = strlen(poolInfoTexts[i].available);
if (stringLength > availStrLength)
availStrLength = stringLength;
} else { } else {
/* --details option was not specified, only active/inactive /* --details option was not specified, only active/inactive
* state strings are used */ * state strings are used */
@ -1334,21 +1311,6 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
poolInfoTexts[i].state = vshStrdup(ctl, _("inactive")); poolInfoTexts[i].state = vshStrdup(ctl, _("inactive"));
} }
} }
/* Keep the length of name string if longest so far */
stringLength = strlen(virStoragePoolGetName(list->pools[i]));
if (stringLength > nameStrLength)
nameStrLength = stringLength;
/* Keep the length of state string if longest so far */
stringLength = strlen(poolInfoTexts[i].state);
if (stringLength > stateStrLength)
stateStrLength = stringLength;
/* Keep the length of autostart string if longest so far */
stringLength = strlen(poolInfoTexts[i].autostart);
if (stringLength > autostartStrLength)
autostartStrLength = stringLength;
} }
/* If the --details option wasn't selected, we output the pool /* If the --details option wasn't selected, we output the pool
@ -1376,19 +1338,23 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
} }
/* Output old style header */ /* Output old style header */
vshPrintExtra(ctl, " %-20s %-10s %-10s\n", _("Name"), _("State"), table = vshTableNew(_("Name"), _("State"), _("Autostart"), NULL);
_("Autostart")); if (!table)
vshPrintExtra(ctl, "-------------------------------------------\n"); goto cleanup;
/* Output old style pool info */ /* Output old style pool info */
for (i = 0; i < list->npools; i++) { for (i = 0; i < list->npools; i++) {
const char *name_str = virStoragePoolGetName(list->pools[i]); const char *name_str = virStoragePoolGetName(list->pools[i]);
vshPrint(ctl, " %-20s %-10s %-10s\n", if (vshTableRowAppend(table,
name_str, name_str,
poolInfoTexts[i].state, poolInfoTexts[i].state,
poolInfoTexts[i].autostart); poolInfoTexts[i].autostart,
NULL) < 0)
goto cleanup;
} }
vshTablePrintToStdout(table, ctl);
/* Cleanup and return */ /* Cleanup and return */
ret = true; ret = true;
goto cleanup; goto cleanup;
@ -1396,99 +1362,33 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
/* We only get here if the --details option was selected. */ /* We only get here if the --details option was selected. */
/* Use the length of name header string if it's longest */ /* Insert the header into table */
stringLength = strlen(_("Name")); table = vshTableNew(_("Name"), _("State"), _("Autostart"), _("Persistent"),
if (stringLength > nameStrLength) _("Capacity"), _("Allocation"), _("Available"), NULL);
nameStrLength = stringLength; if (!table)
/* Use the length of state header string if it's longest */
stringLength = strlen(_("State"));
if (stringLength > stateStrLength)
stateStrLength = stringLength;
/* Use the length of autostart header string if it's longest */
stringLength = strlen(_("Autostart"));
if (stringLength > autostartStrLength)
autostartStrLength = stringLength;
/* Use the length of persistent header string if it's longest */
stringLength = strlen(_("Persistent"));
if (stringLength > persistStrLength)
persistStrLength = stringLength;
/* Use the length of capacity header string if it's longest */
stringLength = strlen(_("Capacity"));
if (stringLength > capStrLength)
capStrLength = stringLength;
/* Use the length of allocation header string if it's longest */
stringLength = strlen(_("Allocation"));
if (stringLength > allocStrLength)
allocStrLength = stringLength;
/* Use the length of available header string if it's longest */
stringLength = strlen(_("Available"));
if (stringLength > availStrLength)
availStrLength = stringLength;
/* Display the string lengths for debugging. */
vshDebug(ctl, VSH_ERR_DEBUG, "Longest name string = %lu chars\n",
(unsigned long) nameStrLength);
vshDebug(ctl, VSH_ERR_DEBUG, "Longest state string = %lu chars\n",
(unsigned long) stateStrLength);
vshDebug(ctl, VSH_ERR_DEBUG, "Longest autostart string = %lu chars\n",
(unsigned long) autostartStrLength);
vshDebug(ctl, VSH_ERR_DEBUG, "Longest persistent string = %lu chars\n",
(unsigned long) persistStrLength);
vshDebug(ctl, VSH_ERR_DEBUG, "Longest capacity string = %lu chars\n",
(unsigned long) capStrLength);
vshDebug(ctl, VSH_ERR_DEBUG, "Longest allocation string = %lu chars\n",
(unsigned long) allocStrLength);
vshDebug(ctl, VSH_ERR_DEBUG, "Longest available string = %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; goto cleanup;
/* Display the header */ /* Insert the pool info rows into table*/
vshPrintExtra(ctl, outputStr, _("Name"), _("State"), _("Autostart"),
_("Persistent"), _("Capacity"), _("Allocation"),
_("Available"));
for (i = nameStrLength + stateStrLength + autostartStrLength
+ persistStrLength + capStrLength
+ allocStrLength + availStrLength
+ 14; i > 0; i--)
vshPrintExtra(ctl, "-");
vshPrintExtra(ctl, "\n");
/* Display the pool info rows */
for (i = 0; i < list->npools; i++) { for (i = 0; i < list->npools; i++) {
vshPrint(ctl, outputStr, if (vshTableRowAppend(table,
virStoragePoolGetName(list->pools[i]), virStoragePoolGetName(list->pools[i]),
poolInfoTexts[i].state, poolInfoTexts[i].state,
poolInfoTexts[i].autostart, poolInfoTexts[i].autostart,
poolInfoTexts[i].persistent, poolInfoTexts[i].persistent,
poolInfoTexts[i].capacity, poolInfoTexts[i].capacity,
poolInfoTexts[i].allocation, poolInfoTexts[i].allocation,
poolInfoTexts[i].available); poolInfoTexts[i].available,
NULL) < 0)
goto cleanup;
} }
vshTablePrintToStdout(table, ctl);
/* Cleanup and return */ /* Cleanup and return */
ret = true; ret = true;
cleanup: cleanup:
VIR_FREE(outputStr); vshTableFree(table);
if (list && list->npools) { if (list && list->npools) {
for (i = 0; i < list->npools; i++) { for (i = 0; i < list->npools; i++) {
VIR_FREE(poolInfoTexts[i].state); VIR_FREE(poolInfoTexts[i].state);