[hackers] [scroll] redraw upon sigwinch || Jochen Sprickerhof

From: <git_AT_suckless.org>
Date: Fri, 10 Apr 2020 00:00:24 +0200 (CEST)

commit 6a8eeb702a0cf24de2314d9be9ebf4b6f17c71cc
Author: Jochen Sprickerhof <git_AT_jochen.sprickerhof.de>
AuthorDate: Thu Apr 9 23:50:39 2020 +0200
Commit: Jochen Sprickerhof <git_AT_jochen.sprickerhof.de>
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
_AT_@ -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;
_AT_@ -110,6 +111,7 @@ sigwinch(int sig)
         if (ioctl(mfd, TIOCSWINSZ, &ws) == -1)
                 die("ioctl:");
         kill(-child, SIGWINCH);
+ doredraw = true;
 }
 
 void
_AT_@ -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)
 {
_AT_@ -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);
 
Received on Fri Apr 10 2020 - 00:00:24 CEST

This archive was generated by hypermail 2.3.0 : Fri Apr 10 2020 - 00:12:36 CEST