From: =?utf-8?q?Martin_Stegh=C3=B6fer?= <martin@steghoefer.eu>
Date: Mon, 8 Dec 2014 19:18:37 +0100
Subject: Truncate long status lines on small terminals

If the status line is longer than the number of columns of the terminal
that stderr prints to, line-wrapping occurs and the mechanisms to
overwrite the current line don't work any longer, thus filling the
terminal with status lines. This can be avoided by checking the
available size and truncating the line, if necessary. The truncation
is indicated to the user via suspension points.

Bug-Debian: https://bugs.debian.org/239073
Forwarded: https://trac.xiph.org/ticket/1677#comment:1
---
 ogg123/status.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/ogg123/status.c b/ogg123/status.c
index ccec389..8eb073a 100644
--- a/ogg123/status.c
+++ b/ogg123/status.c
@@ -24,6 +24,7 @@
 #include <pthread.h>
 
 #ifdef HAVE_UNISTD_H
+#include <sys/ioctl.h>
 #include <unistd.h>
 #endif
 
@@ -166,6 +167,28 @@ int print_statistics_line (stat_format_t stats[])
 
     stats++;
   }
+  
+#ifdef HAVE_UNISTD_H
+  /* If the line would break in the console, truncate it to avoid the break,
+     and indicate the truncation by adding points of ellipsis */
+  struct winsize max;
+  int ioctlError = ioctl(STDERR_FILENO, TIOCGWINSZ, &max);
+  if (!ioctlError) {
+    const int limit = max.ws_col - 1;
+    if (len > limit) {
+      int pointsStart = limit - 3;
+      if (pointsStart < 0) {
+        pointsStart = 0;
+      }
+      int position;
+      for (position = pointsStart; position < limit; position++) {
+        str[position] = '.';
+      }
+      str[position] = 0;
+      len = position;
+    }
+  }
+#endif
 
   len += sprintf(str+len, "\r");
 
