Skip to contentSkip to conversation

ss: use compact output for undetected screen width

PATCH New Peter Junos 1 participant 1 message Dec 23, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ss: use compact output for undetected screen width

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.

---
 misc/ss.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
misc/ss.c +13 -2
index 95f1d37a..56d44f6b 100644
@@ -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) {
+		/* Terminal width couldn't be guessed, don't extend the output */
+		return;
+	}
+
 	/* Second pass: find out newlines and distribute available spacing */
 	for (c = columns; c - columns < COL_MAX; c++) {
 		int pad, spacing, rem, last;
Signature · 1 line
2.24.0