commit ae16d0d30128d141b6bc2d0219a8839aaa3b6c5f
Author:     Jan Klemkow <[email protected]>
AuthorDate: Sun Apr 5 22:15:34 2020 +0200
Commit:     Jan Klemkow <[email protected]>
CommitDate: Sun Apr 5 22:17:12 2020 +0200

    more flexible config

diff --git a/config.def.h b/config.def.h
index 4a2a385..7542eea 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,9 +1,15 @@
 /*
  * Define ESC seqences to use for scroll events.
  * Use "cat -v" to figure out favorit key combination.
+ *
+ * lines is the number of lines scrolled up or down.  If lines is negaive,
+ * its the: -1 is 1/1.
  */
-#define KB_SCROLL_UP   "\033[5;2~"     /* [Shift] + [PageUP] */
-#define KB_SCROLL_DOWN "\033[6;2~"     /* [Shift] + [PageDown] */
 
-#define MS_SCROLL_UP   "\031"  /* mouse wheel up */
-#define MS_SCROLL_DOWN "\005"  /* mouse wheel Down */
+struct rule rules[] = {
+       /* sequence     event        lines */
+       {"\033[5;2~",   SCROLL_UP,   -1},       /* [Shift] + [PageUP] */
+       {"\033[6;2~",   SCROLL_DOWN, -1},       /* [Shift] + [PageDown] */
+       {"\031",        SCROLL_UP,    1},       /* mouse wheel up */
+       {"\005",        SCROLL_DOWN,  1}        /* mouse wheel Down */
+};
diff --git a/scroll.c b/scroll.c
index 7300231..79c6357 100644
--- a/scroll.c
+++ b/scroll.c
@@ -43,7 +43,7 @@
  #include <libutil.h>
 #endif
 
-#include "config.h"
+#define LENGTH(X)      (sizeof (X) / sizeof ((X)[0]))
 
 TAILQ_HEAD(tailhead, line) head;
 
@@ -60,6 +60,14 @@ struct termios dfl;
 struct winsize ws;
 static bool altscreen = false; /* is alternative screen active */
 
+struct rule {
+       const char *seq;
+       enum {SCROLL_UP, SCROLL_DOWN} event;
+       short lines;
+};
+
+#include "config.h"
+
 void
 die(const char *fmt, ...)
 {
@@ -254,6 +262,9 @@ scrollup(int n)
        int rows = 2;
        struct line *scrollend = bottom;
 
+       if (n < 0)
+               n = ws.ws_row / (-n) > 0 ? ws.ws_row / (-n) : 1;
+
        /* wind back scrollend pointer by one page plus n */
        for (; scrollend != NULL && TAILQ_NEXT(scrollend, entries) != NULL &&
            rows < ws.ws_row + n; rows++)
@@ -291,6 +302,10 @@ scrolldown(char *buf, size_t size, int n)
 {
        if (bottom == NULL || bottom == TAILQ_FIRST(&head))
                return;
+
+       if (n < 0)
+               n = ws.ws_row / (-n) > 0 ? ws.ws_row / (-n) : 1;
+
        bottom = TAILQ_PREV(bottom, tailhead, entries);
        /* print n lines */
        for (; n > 0 && bottom != NULL && bottom != TAILQ_FIRST(&head); n--) {
@@ -394,17 +409,24 @@ main(int argc, char *argv[])
                                die("read:");
 
                        input[n] = '\0';
-                       if (!altscreen && strcmp(KB_SCROLL_UP, input) == 0)
-                               scrollup(ws.ws_row);
-                       else if (!altscreen && strcmp(MS_SCROLL_UP, input) == 0)
-                               scrollup(1);
-                       else if (!altscreen && strcmp(KB_SCROLL_DOWN, input) == 
0)
-                               scrolldown(buf, pos, ws.ws_row);
-                       else if (!altscreen && strcmp(MS_SCROLL_DOWN, input) == 
0)
-                               scrolldown(buf, pos, 1);
-                       else if (write(mfd, input, n) == -1)
+
+                       if (altscreen)
+                               goto noevent;
+
+                       for (size_t i = 0; i < LENGTH(rules); i++) {
+                               if (strcmp(rules[i].seq, input) == 0) {
+                                       if (rules[i].event == SCROLL_UP)
+                                               scrollup(rules[i].lines);
+                                       if (rules[i].event == SCROLL_DOWN)
+                                               scrolldown(buf, pos, 
rules[i].lines);
+                                       continue;
+                               }
+                       }
+ noevent:
+                       if (write(mfd, input, n) == -1)
                                die("write:");
-                       else if (bottom != TAILQ_FIRST(&head))
+
+                       if (bottom != TAILQ_FIRST(&head))
                                jumpdown(buf, pos);
                }
                if (pfd[1].revents & POLLIN) {

Reply via email to