Move printoff and curline to struct dir - sacc - sacc(omys), simple console gopher client
git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/
Log
Files
Refs
Tags
LICENSE
---
commit 9a618bec82d956de8d6a2d67539eb19f6790944e
parent 5f125c4f75c25a0bd4aeecc9625307c52f6a8c41
Author: Quentin Rameau 
Date:   Thu, 13 Jul 2017 00:24:53 +0200

Move printoff and curline to struct dir

Diffstat:
  M common.h                            |       6 +++---
  M sacc.c                              |      10 +++-------
  M ui_ti.c                             |      64 ++++++++++++++++---------------
  M ui_txt.c                            |      31 ++++++++++++++++---------------

4 files changed, 55 insertions(+), 56 deletions(-)
---
diff --git a/common.h b/common.h
@@ -10,15 +10,15 @@ struct item {
         char *host;
         char *port;
         char *raw;
-        size_t printoff;
-        size_t curline;
-        Item *entry;
         void *dat;
+        Item *entry;
 };
 
 struct dir {
         Item **items;
         size_t nitems;
+        size_t printoff;
+        size_t curline;
 };
 
 void die(const char *fmt, ...);
diff --git a/sacc.c b/sacc.c
@@ -263,6 +263,7 @@ molddiritem(char *raw)
 
         dir->items = items;
         dir->nitems = nitems;
+        dir->printoff = dir->curline = 0;
 
         return dir;
 }
@@ -485,9 +486,7 @@ delve(Item *hole)
                         break;
                 case '7':
                         if (selector = searchselector(hole)) {
-                                free(hole->raw);
-                                hole->raw = NULL;
-                                hole->printoff = 0;
+                                clear(hole->raw);
                                 if (dig(entry, hole) && hole->dat)
                                         entry = hole;
                                 free(hole->selector);
@@ -568,10 +567,7 @@ moldentry(char *url)
         entry->host = host;
         entry->port = port;
         entry->entry = entry;
-        entry->printoff = 0;
-        entry->curline = 0;
-        entry->raw = NULL;
-        entry->dat = NULL;
+        entry->raw = entry->dat = NULL;
 
         return entry;
 }
diff --git a/ui_ti.c b/ui_ti.c
@@ -94,15 +94,17 @@ help(void)
 static void
 displaystatus(Item *item)
 {
-        size_t nitems = item->dat ? ((Dir*)item->dat)->nitems : 0;
+        Dir *dir = item->dat;
+        size_t nitems = dir ? dir->nitems : 0;
+        unsigned long long printoff = dir ? dir->printoff : 0;
 
         putp(tparm(save_cursor));
 
         putp(tparm(cursor_address, lines-1, 0));
         putp(tparm(enter_standout_mode));
         printf("%3lld%%| %s:%s%s",
-               (item->printoff + lines-1 >= nitems) ? 100 :
-               ((unsigned long long)item->printoff + lines-1) * 100 / nitems,
+               (printoff + lines-1 >= nitems) ? 100 :
+               (printoff + lines-1) * 100 / nitems,
                item->host, item->port, item->selector);
         putp(tparm(exit_standout_mode));
 
@@ -130,8 +132,8 @@ display(Item *entry)
 
         items = dir->items;
         nitems = dir->nitems;
-        printoff = entry->printoff;
-        curln = entry->curline;
+        printoff = dir->printoff;
+        curln = dir->curline;
         lastln = printoff + lines-1; /* one off for status bar */
 
         for (i = printoff; i < nitems && i < lastln; ++i) {
@@ -162,17 +164,17 @@ movecurline(Item *item, int l)
         if (dir == NULL)
                 return;
 
-        curline = item->curline + l;
+        curline = dir->curline + l;
         nitems = dir->nitems;
         if (curline < 0 || curline >= nitems)
                 return;
 
-        printitem(dir->items[item->curline]);
-        item->curline = curline;
+        printitem(dir->items[dir->curline]);
+        dir->curline = curline;
 
         if (l > 0) {
-                offline = item->printoff + lines-1;
-                if (curline - item->printoff >= plines / 2 && offline < nitems) {
+                offline = dir->printoff + lines-1;
+                if (curline - dir->printoff >= plines / 2 && offline < nitems) {
                         putp(tparm(save_cursor));
 
                         putp(tparm(cursor_address, plines, 0));
@@ -180,10 +182,10 @@ movecurline(Item *item, int l)
                         printitem(dir->items[offline]);
 
                         putp(tparm(restore_cursor));
-                        item->printoff += l;
+                        dir->printoff += l;
                 }
         } else {
-                offline = item->printoff + l;
+                offline = dir->printoff + l;
                 if (curline - offline <= plines / 2 && offline >= 0) {
                         putp(tparm(save_cursor));
 
@@ -193,11 +195,11 @@ movecurline(Item *item, int l)
                         putchar('\n');
 
                         putp(tparm(restore_cursor));
-                        item->printoff += l;
+                        dir->printoff += l;
                 }
         }
         
-        putp(tparm(cursor_address, curline - item->printoff, 0));
+        putp(tparm(cursor_address, curline - dir->printoff, 0));
         putp(tparm(enter_standout_mode));
         printitem(dir->items[curline]);
         putp(tparm(exit_standout_mode));
@@ -218,27 +220,27 @@ jumptoline(Item *entry, ssize_t offset)
         nitems = dir->nitems;
 
         if (offset <= 0) {
-                if (!entry->curline)
+                if (!dir->curline)
                         return;
-                entry->printoff = 0;
-                entry->curline = 0;
+                dir->printoff = 0;
+                dir->curline = 0;
         } else if (offset + plines < nitems) {
-                entry->printoff = offset;
-                entry->curline = offset;
-        } else if (entry->curline == nitems-1) {
+                dir->printoff = offset;
+                dir->curline = offset;
+        } else if (dir->curline == nitems-1) {
                 return;
         } else if (nitems < plines) {
-                entry->curline = nitems-1;
+                dir->curline = nitems-1;
         } else if (offset == nitems) {
-                entry->printoff = nitems-1 - plines;
-                entry->curline = nitems-1;
+                dir->printoff = nitems-1 - plines;
+                dir->curline = nitems-1;
         } else {
                 offset = nitems-1 - plines;
-                if (entry->printoff == offset)
-                        entry->curline = nitems-1;
-                else if (entry->curline < offset)
-                        entry->curline = offset;
-                entry->printoff = offset;
+                if (dir->printoff == offset)
+                        dir->curline = nitems-1;
+                else if (dir->curline < offset)
+                        dir->curline = offset;
+                dir->printoff = offset;
         }
 
         display(entry);
@@ -296,7 +298,7 @@ selectitem(Item *entry)
                 case '\n':
                 pgnext:
                         if (dir)
-                                return dir->items[entry->curline];
+                                return dir->items[dir->curline];
                         continue;
                 case _key_lndown:
                 lndown:
@@ -304,7 +306,7 @@ selectitem(Item *entry)
                         continue;
                 case _key_pgdown:
                 pgdown:
-                        jumptoline(entry, entry->printoff + plines);
+                        jumptoline(entry, dir->printoff + plines);
                         continue;
                 case _key_end:
                 end:
@@ -316,7 +318,7 @@ selectitem(Item *entry)
                         continue;
                 case _key_pgup:
                 pgup:
-                        jumptoline(entry, entry->printoff - plines);
+                        jumptoline(entry, dir->printoff - plines);
                         continue;
                 case _key_home:
                 home:
diff --git a/ui_txt.c b/ui_txt.c
@@ -58,12 +58,13 @@ ndigits(size_t n)
 static void
 printstatus(Item *item, char c)
 {
-        size_t nitems = item->dat ? ((Dir*)item->dat)->nitems : 0;
+        Dir *dir = item->dat;
+        size_t nitems = dir ? dir->nitems : 0;
+        unsigned long long printoff = dir ? dir->printoff : 0;
 
         printf("%3lld%%%*c %s:%s%s [%c]: ",
-               (item->printoff + lines >= nitems) ? 100 :
-               ((unsigned long long)item->printoff + lines) * 100 / nitems,
-               ndigits(nitems)+2, '|',
+               (printoff + lines >= nitems) ? 100 :
+               (printoff + lines) * 100 / nitems, ndigits(nitems)+2, '|',
                item->host, item->port, item->selector, c);
 }
 
@@ -96,10 +97,10 @@ display(Item *entry)
 
         items = dir->items;
         nitems = dir->nitems;
-        lines = entry->printoff + termlines();
+        lines = dir->printoff + termlines();
         nd = ndigits(nitems);
 
-        for (i = entry->printoff; i < nitems && i < lines; ++i) {
+        for (i = dir->printoff; i < nitems && i < lines; ++i) {
                 printf("%*zu %s %s\n",
                        nd, i+1, typedisplay(items[i]->type), items[i]->username);
         }
@@ -140,26 +141,26 @@ selectitem(Item *entry)
                         return NULL;
                 case 'n':
                         lines = termlines();
-                        if (lines < nitems - entry->printoff &&
-                            lines < (size_t)-1 - entry->printoff)
-                                entry->printoff += lines;
+                        if (lines < nitems - dir->printoff &&
+                            lines < (size_t)-1 - dir->printoff)
+                                dir->printoff += lines;
                         return entry;
                 case 'p':
                         lines = termlines();
-                        if (lines <= entry->printoff)
-                                entry->printoff -= lines;
+                        if (lines <= dir->printoff)
+                                dir->printoff -= lines;
                         else
-                                entry->printoff = 0;
+                                dir->printoff = 0;
                         return entry;
                 case 'b':
                         lines = termlines();
                         if (nitems > lines)
-                                entry->printoff = nitems - lines;
+                                dir->printoff = nitems - lines;
                         else
-                                entry->printoff = 0;
+                                dir->printoff = 0;
                         return entry;
                 case 't':
-                        entry->printoff = 0;
+                        dir->printoff = 0;
                         return entry;
                 case '!':
                         if (entry->raw)