From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefano Brivio Subject: [PATCH iproute2 net-next 3/3] ss: Beautify output when arbitrary columns are hidden Date: Tue, 30 Oct 2018 16:05:20 +0100 Message-ID: <0ef0caaaaed2ae437151efcf3f4853212813fdf8.1540910943.git.sbrivio@redhat.com> References: Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Cc: "Yoann P." , Stephen Hemminger , netdev@vger.kernel.org To: David Ahern Return-path: Received: from mx1.redhat.com ([209.132.183.28]:52856 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727675AbeJ3X72 (ORCPT ); Tue, 30 Oct 2018 19:59:28 -0400 In-Reply-To: Sender: netdev-owner@vger.kernel.org List-ID: Define a secondary alignment for columns in case the next column is hidden, this avoids awkward outputs if e.g. the local address is shown, but not the local port. Omit embedded delimiter in socket specifiers if the port or service field is hidden. Signed-off-by: Stefano Brivio --- misc/ss.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index 91be3c6db151..d489233681e9 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -131,7 +131,8 @@ enum col_align { }; struct column { - const enum col_align align; + enum col_align align; + const enum col_align align_without_next; const char *optname; const char *header; const char *ldelim; @@ -141,15 +142,15 @@ struct column { }; static struct column columns[] = { - { ALIGN_LEFT, "netid", "Netid", "", 0, 0, 0 }, - { ALIGN_LEFT, "state", "State", " ", 0, 0, 0 }, - { ALIGN_LEFT, "recvq", "Recv-Q", " ", 0, 0, 0 }, - { ALIGN_LEFT, "sendq", "Send-Q", " ", 0, 0, 0 }, - { ALIGN_RIGHT, "local", "Local Address:", " ", 0, 0, 0 }, - { ALIGN_LEFT, "lport", "Port", "", 0, 0, 0 }, - { ALIGN_RIGHT, "peer", "Peer Address:", " ", 0, 0, 0 }, - { ALIGN_LEFT, "pport", "Port", "", 0, 0, 0 }, - { ALIGN_LEFT, "ext", "", "", 0, 0, 0 }, + { ALIGN_LEFT, ALIGN_LEFT, "netid", "Netid", "", 0, 0, 0 }, + { ALIGN_LEFT, ALIGN_LEFT, "state", "State", " ", 0, 0, 0 }, + { ALIGN_LEFT, ALIGN_LEFT, "recvq", "Recv-Q", " ", 0, 0, 0 }, + { ALIGN_LEFT, ALIGN_LEFT, "sendq", "Send-Q", " ", 0, 0, 0 }, + { ALIGN_RIGHT, ALIGN_LEFT, "local", "Local Address:", " ", 0, 0, 0 }, + { ALIGN_LEFT, ALIGN_LEFT, "lport", "Port", "", 0, 0, 0 }, + { ALIGN_RIGHT, ALIGN_LEFT, "peer", "Peer Address:", " ", 0, 0, 0 }, + { ALIGN_LEFT, ALIGN_LEFT, "pport", "Port", "", 0, 0, 0 }, + { ALIGN_LEFT, ALIGN_LEFT, "ext", "", "", 0, 0, 0 }, }; static struct column *current_field = columns; @@ -1374,6 +1375,9 @@ static void sock_details_print(struct sockstat *s) static void sock_addr_print(const char *addr, char *delim, const char *port, const char *ifname) { + if ((current_field + 1)->disabled) + delim = ""; + if (ifname) out("%s" "%%" "%s%s", addr, ifname, delim); else @@ -5006,6 +5010,12 @@ int main(int argc, char *argv[]) } p = p1 + 1; } while (p1); + + for (f = columns; field_is_valid(f + 1); f++) { + if ((f + 1)->disabled) + f->align = f->align_without_next; + } + break; } case 'h': -- 2.19.1