summaryrefslogtreecommitdiffstats
path: root/listnav.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2021-12-16 13:48:13 +0100
committerLouis Burda <quent.burda@gmail.com>2021-12-20 15:31:29 +0100
commit15a8fe2cf2b16af8739a7ec2b64b5c5f184161b8 (patch)
tree62a7b404ac2fda90d0df628922f182afacd0d510 /listnav.c
parent1bd07952245e3fc8ed95af0c1eff45938098b40b (diff)
downloadtmus-15a8fe2cf2b16af8739a7ec2b64b5c5f184161b8.tar.gz
tmus-15a8fe2cf2b16af8739a7ec2b64b5c5f184161b8.zip
Implemented list navigation and other fixes
Diffstat (limited to 'listnav.c')
-rw-r--r--listnav.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/listnav.c b/listnav.c
new file mode 100644
index 0000000..707a973
--- /dev/null
+++ b/listnav.c
@@ -0,0 +1,43 @@
+#include "listnav.h"
+#include "util.h"
+
+#include <string.h>
+
+void
+listnav_init(struct listnav *nav)
+{
+ memset(nav, 0, sizeof(struct listnav));
+}
+
+void
+listnav_update_bounds(struct listnav *nav, int min, int max)
+{
+ nav->min = min;
+ nav->max = max;
+ nav->wmin = MAX(nav->wmin, nav->min);
+ nav->wmax = MIN(nav->wmin + nav->wlen, nav->max);
+ nav->sel = MIN(MAX(nav->sel, nav->wmin), nav->wmax - 1);
+}
+
+void
+listnav_update_wlen(struct listnav *nav, int wlen)
+{
+ nav->wlen = wlen;
+ nav->wmax = MIN(nav->wmin + nav->wlen, nav->max);
+ nav->sel = MIN(MAX(nav->sel, nav->wmin), nav->wmax - 1);
+}
+
+void
+listnav_update_sel(struct listnav *nav, int sel)
+{
+ nav->sel = MAX(MIN(sel, nav->max - 1), nav->min);
+
+ if (nav->sel >= nav->wmax) {
+ nav->wmax = nav->sel + 1;
+ nav->wmin = MAX(nav->min, nav->wmax - nav->wlen);
+ } else if (nav->sel < nav->wmin) {
+ nav->wmin = nav->sel;
+ nav->wmax = MIN(nav->wmin + nav->wlen, nav->max);
+ }
+}
+