From 68429e4f41e0691acb37e73f35e42a5d12f5302c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 29 Apr 2022 03:11:35 -0700 Subject: Use clear-to-end-of-line for flicker-free updates. --- pw.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'pw.c') diff --git a/pw.c b/pw.c index dd0160e..e533a5e 100644 --- a/pw.c +++ b/pw.c @@ -227,6 +227,18 @@ static void usage(const char *name) exit(EXIT_FAILURE); } +static void clrline() +{ + printf("\r\033[J"); +} + +static void clreol(int nl) +{ + printf("\033[K"); + if (nl) + putchar('\n'); +} + static void drawline(const char *line, int hpos, int columns) { size_t len = dslen(line); @@ -238,22 +250,19 @@ static void drawline(const char *line, int hpos, int columns) putchar('>'); } if (len < (size_t) columns - 1) { - puts(line); + fputs(line, stdout); + clreol(1); } else { for (int i = 0; i < columns - 2; i++) putchar(line[i]); puts("<"); } } else { - puts(">"); + putchar('>'); + clreol(1); } } -static void clrline() -{ - printf("\r\033[J"); -} - static void drawstatus(int columns, unsigned stat, char *cmd) { char status[cmdsize] = "", *ptr = status; @@ -292,6 +301,7 @@ static void drawstatus(int columns, unsigned stat, char *cmd) } fputs(status, stdout); + clreol(0); fflush(stdout); } @@ -307,13 +317,13 @@ static void redraw(char **circbuf, int nlines, int hpos, dsdrop(snapshot[i]); } snaplines = nlines; - printf("\r\033[%dA\033[J", nlines); + printf("\r\033[%dA", nlines); for (int i = 0; i < nlines; i++) { drawline(circbuf[i], hpos, columns); snapshot[i] = dsref(circbuf[i]); } } else { - printf("\r\033[%dA\033[J", snaplines); + printf("\r\033[%dA", snaplines); for (int i = 0; i < snaplines; i++) drawline(snapshot[i], hpos, columns); } -- cgit v1.2.3