From 922d4af37ff2e5aafe6f35886aed9d8c20a69d29 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 24 Feb 2023 12:35:02 -0800 Subject: [PATCH] ps, vi: fix flicker. My earlier trick to ensure that we buffer whole screens full works fine on glibc, but both bionic and musl have a tiny 1024-byte BUFSIZ that makes it unsuitable for this kind of use, even on laptop screens. Explicitly say 8192, since 4096 is slightly too small for my larger laptop's screen (and I don't use a particularly small font). At some point we should probably move this into tty.c, dynamically allocate based on screen size (plus space for escape sequences/non-ASCII characters), and track SIGWINCH in case the window grows. But this stops top and vi flickering today, which is good enough for now. (Amusingly, I hit the vi problem -- which is actually much worse, for dense strace output -- while debugging the top problem, while debugging the ps problem, while debugging the thing I was actually supposed to be doing _yesterday_. So definitely time to back out of a few rat holes!) --- toys/pending/vi.c | 4 ++-- toys/posix/ps.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/toys/pending/vi.c b/toys/pending/vi.c index e5a8e058..9e743dc3 100644 --- a/toys/pending/vi.c +++ b/toys/pending/vi.c @@ -1520,7 +1520,7 @@ static void draw_page() void vi_main(void) { - char stdout_buf[BUFSIZ]; + char stdout_buf[8192]; char keybuf[16] = {0}; char vi_buf[16] = {0}; char utf8_code[8] = {0}; @@ -1544,7 +1544,7 @@ void vi_main(void) TT.screen_height -= 1; // Avoid flicker. - setbuf(stdout, stdout_buf); + setbuffer(stdout, stdout_buf, sizeof(stdout_buf)); xsignal(SIGWINCH, generic_signal); set_terminal(0, 1, 0, 0); diff --git a/toys/posix/ps.c b/toys/posix/ps.c index d43379c3..4f3807a8 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -1521,13 +1521,13 @@ static void top_common( "iow", "irq", "sirq", "host"}; unsigned tock = 0; int i, lines, topoff = 0, done = 0; - char stdout_buf[BUFSIZ]; + char stdout_buf[8192]; if (!TT.fields) perror_exit("no -o"); // Avoid flicker and hide the cursor in interactive mode. if (!FLAG(b)) { - setbuf(stdout, stdout_buf); + setbuffer(stdout, stdout_buf, sizeof(stdout_buf)); sigatexit(top_cursor_cleanup); xputsn("\e[?25l"); } -- 2.39.2