From patchwork Tue Oct 30 15:05:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Brivio X-Patchwork-Id: 990909 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42kvsx00Gbz9s9h for ; Wed, 31 Oct 2018 02:05:40 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727684AbeJ3X73 (ORCPT ); Tue, 30 Oct 2018 19:59:29 -0400 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 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 326B93154849; Tue, 30 Oct 2018 15:05:38 +0000 (UTC) Received: from epycfail.redhat.com (ovpn-200-20.brq.redhat.com [10.40.200.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id C71A71055014; Tue, 30 Oct 2018 15:05:36 +0000 (UTC) From: Stefano Brivio To: David Ahern Cc: "Yoann P." , Stephen Hemminger , netdev@vger.kernel.org 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> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Tue, 30 Oct 2018 15:05:38 +0000 (UTC) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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':