[PATCH] examples: Improved shell output of showDomains
From: Daniel Hora <dhora@redhat.com> Improved formatting of the shell output of the function showDomains based on length of the longest domain. Signed-off-by: Daniel Hora <dhora@redhat.com> --- examples/c/misc/hellolibvirt.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/examples/c/misc/hellolibvirt.c b/examples/c/misc/hellolibvirt.c index 39cefe934c..77098d7be3 100644 --- a/examples/c/misc/hellolibvirt.c +++ b/examples/c/misc/hellolibvirt.c @@ -4,6 +4,7 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <libvirt/libvirt.h> #include <libvirt/virterror.h> @@ -44,11 +45,34 @@ showHypervisorInfo(virConnectPtr conn) return 0; } +static int +longestDomain(virDomainPtr *nameList, int numNames) +{ + int longestDomainLength; + size_t i; + + longestDomainLength = 0; + + for (i = 0; i < numNames; i++) { + const char *currentDomain = virDomainGetName(nameList[i]); + + if (currentDomain != NULL) { + int currentDomainLength = strlen(currentDomain); + + if (currentDomainLength > longestDomainLength) { + longestDomainLength = currentDomainLength; + } + } + } + + return longestDomainLength; + +} static int showDomains(virConnectPtr conn) { - int numNames, numInactiveDomains, numActiveDomains; + int numNames, numInactiveDomains, numActiveDomains, longestDomainLength; ssize_t i; int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE | VIR_CONNECT_LIST_DOMAINS_INACTIVE; @@ -88,9 +112,12 @@ showDomains(virConnectPtr conn) return 1; } + longestDomainLength = longestDomain(nameList, numNames); + for (i = 0; i < numNames; i++) { int active = virDomainIsActive(nameList[i]); - printf(" %8s (%s)\n", + printf(" %-*s (%s)\n", + longestDomainLength, virDomainGetName(nameList[i]), (active == 1 ? "active" : "non-active")); /* must free the returned named per the API documentation */ -- 2.54.0
On 5/28/26 15:28, Daniel Hora via Devel wrote:
From: Daniel Hora <dhora@redhat.com>
Improved formatting of the shell output of the function showDomains based on length of the longest domain.
Signed-off-by: Daniel Hora <dhora@redhat.com> --- examples/c/misc/hellolibvirt.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/examples/c/misc/hellolibvirt.c b/examples/c/misc/hellolibvirt.c index 39cefe934c..77098d7be3 100644 --- a/examples/c/misc/hellolibvirt.c +++ b/examples/c/misc/hellolibvirt.c @@ -4,6 +4,7 @@
#include <stdio.h> #include <stdlib.h> +#include <string.h> #include <libvirt/libvirt.h> #include <libvirt/virterror.h>
@@ -44,11 +45,34 @@ showHypervisorInfo(virConnectPtr conn) return 0; }
+static int +longestDomain(virDomainPtr *nameList, int numNames) +{ + int longestDomainLength; + size_t i; + + longestDomainLength = 0;
This can be done when declaring the variable.
+ + for (i = 0; i < numNames; i++) { + const char *currentDomain = virDomainGetName(nameList[i]); + + if (currentDomain != NULL) { + int currentDomainLength = strlen(currentDomain); + + if (currentDomainLength > longestDomainLength) { + longestDomainLength = currentDomainLength; + } + } + } + + return longestDomainLength; + +}
static int showDomains(virConnectPtr conn) { - int numNames, numInactiveDomains, numActiveDomains; + int numNames, numInactiveDomains, numActiveDomains, longestDomainLength; ssize_t i; int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE | VIR_CONNECT_LIST_DOMAINS_INACTIVE; @@ -88,9 +112,12 @@ showDomains(virConnectPtr conn) return 1; }
+ longestDomainLength = longestDomain(nameList, numNames); + for (i = 0; i < numNames; i++) { int active = virDomainIsActive(nameList[i]); - printf(" %8s (%s)\n", + printf(" %-*s (%s)\n", + longestDomainLength,
Ooops, misaligned.
virDomainGetName(nameList[i]), (active == 1 ? "active" : "non-active")); /* must free the returned named per the API documentation */
I'll fix those trivial nits before pushing (we're in a freeze, so after the release). Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Michal
participants (2)
-
Daniel Hora -
Michal Prívozník