Commit f594ac04 authored by Martin Steghöfer's avatar Martin Steghöfer Committed by Thomas Daede

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.

parent f3bf5570
......@@ -24,6 +24,7 @@
#include <pthread.h>
#include <sys/ioctl.h>
#include <unistd.h>
......@@ -166,6 +167,28 @@ int print_statistics_line (stat_format_t stats[])
/* 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;
len += sprintf(str+len, "\r");
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment