public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Ethtool: Beautify private flags print
@ 2013-05-23 17:55 Yuval Mintz
  2013-05-23 18:30 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Yuval Mintz @ 2013-05-23 17:55 UTC (permalink / raw)
  To: bhutchings, netdev; +Cc: eilong, Yuval Mintz

When printing the private flags of the device, align all strings
to have the same length.

Signed-off-by: Yuval Mintz <yuvalmin@broadcom.com>
---
 ethtool.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/ethtool.c b/ethtool.c
index 8cc10b5..1244c74 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -3445,6 +3445,7 @@ static int do_gprivflags(struct cmd_context *ctx)
 	struct ethtool_gstrings *strings;
 	struct ethtool_value flags;
 	unsigned int i;
+	int max_len = 0, cur_len;
 
 	if (ctx->argc != 0)
 		exit_bad_args();
@@ -3472,11 +3473,28 @@ static int do_gprivflags(struct cmd_context *ctx)
 		return 1;
 	}
 
+	/* Find longest string and align all strings accordingly */
+	for (i = 0; i < strings->len; i++) {
+		cur_len = strlen((const char*)strings->data +
+					      i * ETH_GSTRING_LEN);
+		if (cur_len > max_len)
+			max_len = cur_len;
+	}
+
 	printf("Private flags for %s:\n", ctx->devname);
-	for (i = 0; i < strings->len; i++)
-		printf("%s: %s\n",
-		       (const char *)strings->data + i * ETH_GSTRING_LEN,
-		       (flags.data & (1U << i)) ? "on" : "off");
+	for (i = 0; i < strings->len; i++) {
+		int j;
+
+		printf("%s", (const char *)strings->data +
+					   i * ETH_GSTRING_LEN);
+
+		cur_len = strlen((const char*)strings->data +
+					      i * ETH_GSTRING_LEN);
+		for (j = 0; j < max_len - cur_len; j++)
+			printf(" ");
+
+		printf(": %s\n", (flags.data & (1U << i)) ? "on" : "off");
+	}
 
 	return 0;
 }
-- 
1.8.1.227.g44fe835

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Ethtool: Beautify private flags print
  2013-05-23 17:55 [PATCH] Ethtool: Beautify private flags print Yuval Mintz
@ 2013-05-23 18:30 ` Joe Perches
  2013-05-24  3:50   ` Yuval Mintz
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2013-05-23 18:30 UTC (permalink / raw)
  To: Yuval Mintz; +Cc: bhutchings, netdev, eilong

On Thu, 2013-05-23 at 20:55 +0300, Yuval Mintz wrote:
> When printing the private flags of the device, align all strings
> to have the same length.
[]
> diff --git a/ethtool.c b/ethtool.c
[]
> +	for (i = 0; i < strings->len; i++) {
> +		int j;
> +
> +		printf("%s", (const char *)strings->data +
> +					   i * ETH_GSTRING_LEN);
> +
> +		cur_len = strlen((const char*)strings->data +
> +					      i * ETH_GSTRING_LEN);
> +		for (j = 0; j < max_len - cur_len; j++)
> +			printf(" ");

I think this isn't necessary, but this is simpler

"%-*s", max_len, strings->data + i * ETH_GSTRING_LEN

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH] Ethtool: Beautify private flags print
  2013-05-23 18:30 ` Joe Perches
@ 2013-05-24  3:50   ` Yuval Mintz
  0 siblings, 0 replies; 3+ messages in thread
From: Yuval Mintz @ 2013-05-24  3:50 UTC (permalink / raw)
  To: Joe Perches
  Cc: bhutchings@solarflare.com, netdev@vger.kernel.org,
	Eilon Greenstein

> On Thu, 2013-05-23 at 20:55 +0300, Yuval Mintz wrote:
> > When printing the private flags of the device, align all strings
> > to have the same length.
> []
> > diff --git a/ethtool.c b/ethtool.c
> []
> > +	for (i = 0; i < strings->len; i++) {
> > +		int j;
> > +
> > +		printf("%s", (const char *)strings->data +
> > +					   i * ETH_GSTRING_LEN);
> > +
> > +		cur_len = strlen((const char*)strings->data +
> > +					      i * ETH_GSTRING_LEN);
> > +		for (j = 0; j < max_len - cur_len; j++)
> > +			printf(" ");
> 
> I think this isn't necessary, but this is simpler
> 
> "%-*s", max_len, strings->data + i * ETH_GSTRING_LEN

Nice - I was unfamiliar with this second-order format string modifier.
I'll modify the patch into using this format.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-05-24  3:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-23 17:55 [PATCH] Ethtool: Beautify private flags print Yuval Mintz
2013-05-23 18:30 ` Joe Perches
2013-05-24  3:50   ` Yuval Mintz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox