0010-Truncate-long-status-lines-on-small-terminals.patch 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. From: =?utf-8?q?Martin_Stegh=C3=B6fer?= <martin@steghoefer.eu>
  2. Date: Mon, 8 Dec 2014 19:18:37 +0100
  3. Subject: Truncate long status lines on small terminals
  4. If the status line is longer than the number of columns of the terminal
  5. that stderr prints to, line-wrapping occurs and the mechanisms to
  6. overwrite the current line don't work any longer, thus filling the
  7. terminal with status lines. This can be avoided by checking the
  8. available size and truncating the line, if necessary. The truncation
  9. is indicated to the user via suspension points.
  10. Bug-Debian: https://bugs.debian.org/239073
  11. Forwarded: https://trac.xiph.org/ticket/1677#comment:1
  12. ---
  13. ogg123/status.c | 23 +++++++++++++++++++++++
  14. 1 file changed, 23 insertions(+)
  15. diff --git a/ogg123/status.c b/ogg123/status.c
  16. index ccec389..8eb073a 100644
  17. --- a/ogg123/status.c
  18. +++ b/ogg123/status.c
  19. @@ -24,6 +24,7 @@
  20. #include <pthread.h>
  21. #ifdef HAVE_UNISTD_H
  22. +#include <sys/ioctl.h>
  23. #include <unistd.h>
  24. #endif
  25. @@ -166,6 +167,28 @@ int print_statistics_line (stat_format_t stats[])
  26. stats++;
  27. }
  28. +
  29. +#ifdef HAVE_UNISTD_H
  30. + /* If the line would break in the console, truncate it to avoid the break,
  31. + and indicate the truncation by adding points of ellipsis */
  32. + struct winsize max;
  33. + int ioctlError = ioctl(STDERR_FILENO, TIOCGWINSZ, &max);
  34. + if (!ioctlError) {
  35. + const int limit = max.ws_col - 1;
  36. + if (len > limit) {
  37. + int pointsStart = limit - 3;
  38. + if (pointsStart < 0) {
  39. + pointsStart = 0;
  40. + }
  41. + int position;
  42. + for (position = pointsStart; position < limit; position++) {
  43. + str[position] = '.';
  44. + }
  45. + str[position] = 0;
  46. + len = position;
  47. + }
  48. + }
  49. +#endif
  50. len += sprintf(str+len, "\r");