commit 6a8eeb702a0cf24de2314d9be9ebf4b6f17c71cc
Author:     Jochen Sprickerhof <[email protected]>
AuthorDate: Thu Apr 9 23:50:39 2020 +0200
Commit:     Jochen Sprickerhof <[email protected]>
CommitDate: Thu Apr 9 23:50:39 2020 +0200

    redraw upon sigwinch

diff --git a/scroll.c b/scroll.c
index 89bbd1d..bea53a9 100644
--- a/scroll.c
+++ b/scroll.c
@@ -60,6 +60,7 @@ int mfd;
 struct termios dfl;
 struct winsize ws;
 static bool altscreen = false; /* is alternative screen active */
+static bool doredraw = false;  /* redraw upon sigwinch */
 
 struct rule {
        const char *seq;
@@ -110,6 +111,7 @@ sigwinch(int sig)
        if (ioctl(mfd, TIOCSWINSZ, &ws) == -1)
                die("ioctl:");
        kill(-child, SIGWINCH);
+       doredraw = true;
 }
 
 void
@@ -257,6 +259,37 @@ addline(char *buf, size_t size)
        TAILQ_INSERT_HEAD(&head, line, entries);
 }
 
+void
+redraw()
+{
+       int rows = 0;
+
+       /* wind back bottom pointer by one page */
+       for (; bottom != NULL && TAILQ_NEXT(bottom, entries) != NULL &&
+           rows < ws.ws_row; rows++)
+               bottom = TAILQ_NEXT(bottom, entries);
+
+       if (rows <= 0) {
+               return;
+       }
+
+       /* clear screen */
+       dprintf(STDOUT_FILENO, "\033[2J");
+       /* set cursor position to upper left corner */
+       write(STDOUT_FILENO, "\033[0;0H", 6);
+
+       /* remove newline of first line as we are at 0,0 already */
+       if (bottom->size > 0 && bottom->buf[0] == '\n')
+               write(STDOUT_FILENO, bottom->buf + 1, bottom->size - 1);
+       else
+               write(STDOUT_FILENO, bottom->buf, bottom->size);
+
+       for (; rows > 0; rows--) {
+               bottom = TAILQ_PREV(bottom, tailhead, entries);
+               write(STDOUT_FILENO, bottom->buf, bottom->size);
+       }
+}
+
 void
 scrollup(int n)
 {
@@ -437,6 +470,12 @@ main(int argc, char *argv[])
                if (poll(pfd, 2, -1) == -1 && errno != EINTR)
                        die("poll:");
 
+               if (doredraw) {
+                       redraw();
+                       doredraw = false;
+                       continue;
+               }
+
                if (pfd[0].revents & POLLIN) {
                        ssize_t n = read(STDIN_FILENO, input, sizeof(input)-1);
 

Reply via email to