From ba300c58f70ff729e306d89373650cf094f7c1c6 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 22 Dec 2015 15:03:04 +0900 Subject: [PATCH] Make "cluster show" output dynamic Calculate the width of the "Name" and "Upstream" columns dynamically. Based on pull request #135 by sengaya, edited and modified by myself to include a psql-like separator line. --- repmgr.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/repmgr.c b/repmgr.c index 596351e6..1a004ffb 100644 --- a/repmgr.c +++ b/repmgr.c @@ -673,6 +673,11 @@ do_cluster_show(void) char sqlquery[QUERY_STR_LEN]; char node_role[MAXLEN]; int i; + char name_header[MAXLEN]; + char upstream_header[MAXLEN]; + int name_length, + upstream_length, + conninfo_length = 0; /* We need to connect to check configuration */ log_info(_("connecting to database\n")); @@ -697,7 +702,51 @@ do_cluster_show(void) } PQfinish(conn); - printf("Role | Name | Upstream | Connection String\n"); + /* Format header nicely */ + + strncpy(name_header, _("Name"), MAXLEN); + strncpy(upstream_header, _("Upstream"), MAXLEN); + + /* + * XXX if repmgr is ever localized into non-ASCII locales, + * use pg_wcssize() or similar to establish printed column length + */ + name_length = strlen(name_header); + upstream_length = strlen(upstream_header); + + for (i = 0; i < PQntuples(res); i++) + { + int conninfo_length_cur, name_length_cur, upstream_length_cur; + + conninfo_length_cur = strlen(PQgetvalue(res, i, 0)); + if (conninfo_length_cur > conninfo_length) + conninfo_length = conninfo_length_cur; + + name_length_cur = strlen(PQgetvalue(res, i, 2)); + if (name_length_cur > name_length) + name_length = name_length_cur; + + upstream_length_cur = strlen(PQgetvalue(res, i, 3)); + if (upstream_length_cur > upstream_length) + upstream_length = upstream_length_cur; + } + + printf("Role | %-*s | %-*s | Connection String\n", name_length, name_header, upstream_length, upstream_header); + printf("----------+-"); + + for (i = 0; i < name_length; i++) + printf("-"); + + printf("-|-"); + for (i = 0; i < upstream_length; i++) + printf("-"); + + printf("-|-"); + for (i = 0; i < conninfo_length; i++) + printf("-"); + + printf("\n"); + for (i = 0; i < PQntuples(res); i++) { conn = establish_db_connection(PQgetvalue(res, i, 0), false); @@ -711,8 +760,8 @@ do_cluster_show(void) strcpy(node_role, "* master"); printf("%-10s", node_role); - printf("| %-14s", PQgetvalue(res, i, 2)); - printf("| %-14s", PQgetvalue(res, i, 3)); + printf("| %-*s ", name_length, PQgetvalue(res, i, 2)); + printf("| %-*s ", upstream_length, PQgetvalue(res, i, 3)); printf("| %s\n", PQgetvalue(res, i, 0)); PQfinish(conn);