Netdev List
 help / color / mirror / Atom feed
From: Peter Junos <petoju@gmail.com>
To: netdev@vger.kernel.org
Cc: petoju@gmail.com
Subject: [PATCH] ss: use compact output for undetected screen width
Date: Mon, 23 Dec 2019 13:47:16 +0100	[thread overview]
Message-ID: <20191223124716.GA25816@peto-laptopnovy> (raw)

This change fixes calculation of width in case user pipes the output.

SS output output works correctly when stdout is a terminal. When one
pipes the output, it tries to use 80 or 160 columns. That adds a
line-break if user has terminal width of 100 chars and output is of
the similar width.

To reproduce the issue, call
ss | less
and see every other line empty if your screen is between 80 and 160
columns wide.

Signed-off-by: Peter Junos <petoju@gmail.com>
---
 misc/ss.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index 95f1d37a..dff1a618 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -1135,10 +1135,10 @@ static void buf_free_all(void)
 	buffer.chunks = 0;
 }
 
-/* Get current screen width, default to 80 columns if TIOCGWINSZ fails */
+/* Get current screen width, returns -1 if TIOCGWINSZ fails */
 static int render_screen_width(void)
 {
-	int width = 80;
+	int width = -1;
 
 	if (isatty(STDOUT_FILENO)) {
 		struct winsize w;
@@ -1159,7 +1159,13 @@ static int render_screen_width(void)
  */
 static void render_calc_width(void)
 {
+	bool compact_output = false;
 	int screen_width = render_screen_width();
+	if (screen_width == -1) {
+		screen_width = 80;
+		compact_output = true;
+	}
+
 	struct column *c, *eol = columns - 1;
 	int first, len = 0, linecols = 0;
 
@@ -1183,6 +1189,11 @@ static void render_calc_width(void)
 			first = 0;
 	}
 
+	if (compact_output) {
+		/* Compact output, skip extending columns. */
+		return;
+	}
+
 	/* Second pass: find out newlines and distribute available spacing */
 	for (c = columns; c - columns < COL_MAX; c++) {
 		int pad, spacing, rem, last;
-- 
2.24.0


             reply	other threads:[~2019-12-23 12:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-23 12:47 Peter Junos [this message]
2019-12-25 20:36 ` [PATCH] ss: use compact output for undetected screen width Stephen Hemminger
2019-12-26 13:06   ` Peter Junos
2019-12-26 13:07   ` [PATCH net-next] " Peter Junos
2020-01-02 18:42     ` David Ahern
  -- strict thread matches above, loose matches on Subject: below --
2019-12-23 10:45 [PATCH] " Peter Junos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191223124716.GA25816@peto-laptopnovy \
    --to=petoju@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox