commit f7052595cdec83861c5f28bc5e579c6238b5aced
Author:     Tom Schwindl <schwi...@posteo.de>
AuthorDate: Thu Oct 13 16:23:21 2022 +0200
Commit:     Jan Klemkow <j.klem...@wemelug.de>
CommitDate: Thu Oct 13 22:58:40 2022 +0200

    slackline: implement sl_move() to handle cursor movement

diff --git a/slackline.c b/slackline.c
index b43b71c..d7047a5 100644
--- a/slackline.c
+++ b/slackline.c
@@ -24,6 +24,8 @@
 
 #include "slackline.h"
 
+enum direction {LEFT, RIGHT, HOME, END};
+
 struct slackline *
 sl_init(void)
 {
@@ -112,6 +114,35 @@ sl_backspace(struct slackline *sl)
        sl->ptr = ncur;
 }
 
+static void
+sl_move(struct slackline *sl, enum direction dir)
+{
+       switch (dir) {
+       case HOME:
+               sl->bcur = sl->rcur = 0;
+               sl->ptr = sl->buf;
+               return;
+       case END:
+               sl->rcur = sl->rlen;
+               sl->bcur = sl_postobyte(sl, sl->rcur);
+               sl->ptr = sl->buf + sl->bcur;
+               return;
+       case RIGHT:
+               if (sl->rcur < sl->rlen)
+                       sl->rcur++;
+               break;
+       case LEFT:
+               if (sl->rcur > 0) {
+                       sl->rcur--;
+                       sl->bcur = sl_postobyte(sl, sl->rcur);
+               }
+               break;
+       }
+
+       sl->bcur = sl_postobyte(sl, sl->rcur);
+       sl->ptr = sl->buf + sl->bcur;
+}
+
 int
 sl_keystroke(struct slackline *sl, int key)
 {
@@ -133,39 +164,22 @@ sl_keystroke(struct slackline *sl, int key)
                case 'B':       /* down  */
                        break;
                case 'C':       /* right */
-                       if (sl->rcur < sl->rlen)
-                               sl->rcur++;
-                       sl->bcur = sl_postobyte(sl, sl->rcur);
-                       sl->ptr = sl->buf + sl->bcur;
+                       sl_move(sl, RIGHT);
                        break;
                case 'D':       /* left */
-                       if (sl->rcur > 0)
-                               sl->rcur--;
-                       sl->bcur = sl_postobyte(sl, sl->rcur);
-                       sl->ptr = sl->buf + sl->bcur;
+                       sl_move(sl, LEFT);
                        break;
                case 'H':       /* Home  */
-                       sl->bcur = sl->rcur = 0;
-                       sl->ptr = sl->buf;
+                       sl_move(sl, HOME);
                        break;
                case 'F':       /* End   */
-                       sl->rcur = sl->rlen;
-                       sl->bcur = sl_postobyte(sl, sl->rcur);
-                       sl->ptr = sl->buf + sl->bcur;
+                       sl_move(sl, END);
                        break;
                case 'P':       /* delete */
                        if (sl->rcur == sl->rlen)
                                break;
-
-                       char *ncur = sl_postoptr(sl, sl->rcur + 1);
-
-                       memmove(sl->ptr, ncur, sl->last - ncur);
-
-                       sl->rlen--;
-                       sl->blen = sl_postobyte(sl, sl->rlen);
-
-                       sl->last -= ncur - sl->ptr;
-                       *sl->last = '\0';
+                       sl_move(sl, RIGHT);
+                       sl_backspace(sl);
                        break;
                case '0':
                case '1':
@@ -188,13 +202,10 @@ sl_keystroke(struct slackline *sl, int key)
                case '~':
                        switch(sl->nummod) {
                        case '7':
-                               sl->bcur = sl->rcur = 0;
-                               sl->ptr = sl->buf;
+                               sl_move(sl, HOME);
                                break;
                        case '8':
-                               sl->rcur = sl->rlen;
-                               sl->bcur = sl_postobyte(sl, sl->rcur);
-                               sl->ptr = sl->buf + sl->bcur;
+                               sl_move(sl, END);
                                break;
                        }
                        sl->esc = ESC_NONE;

Reply via email to