summaryrefslogtreecommitdiff
path: root/tui
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2025-03-21 18:00:02 +0100
committerJulian Hurst <ark@mansus.space>2025-03-21 18:00:02 +0100
commit6cb9d6f8756e690e6cd2360b8b1ab531da45c405 (patch)
treede0ba6160942e1663cd3a298b878f1857598c124 /tui
parent1c3531d4da2e2f56bf09c38a756a8c5f7c615b80 (diff)
downloadhare-tui-6cb9d6f8756e690e6cd2360b8b1ab531da45c405.tar.gz
Simply and fix reframe and introduce bounds checks for setcursor
Diffstat (limited to 'tui')
-rw-r--r--tui/widget/list/scrolllist.ha26
1 files changed, 12 insertions, 14 deletions
diff --git a/tui/widget/list/scrolllist.ha b/tui/widget/list/scrolllist.ha
index 2f18ef0..0079363 100644
--- a/tui/widget/list/scrolllist.ha
+++ b/tui/widget/list/scrolllist.ha
@@ -120,27 +120,25 @@ export fn bottom(li: *scrolllist) void = {
};
export fn setcursor(li: *scrolllist, newpos: int) void = {
- li.cursor = newpos;
+ if (newpos < 0) {
+ li.cursor = 0;
+ } else if (newpos > len(li.items): int) {
+ li.cursor = len(li.items): int - 1;
+ } else {
+ li.cursor = newpos;
+ };
reframe(li);
};
fn reframe(li: *scrolllist) void = {
- if (li.cursor > li.frame.end) {
+ if (li.cursor >= li.frame.end) {
const diff = li.frame.end - li.frame.start;
- li.frame.start = if (li.cursor <= len(li.items): int - diff) li.cursor else len(li.items): int - diff;
- li.frame.end = if (li.frame.start + diff > len(li.items): int) {
- yield len(li.items): int;
- } else {
- yield li.frame.start + diff;
- };
+ li.frame.end = li.cursor + 1;
+ li.frame.start = li.frame.end - diff;
} else if (li.cursor < li.frame.start) {
const diff = li.frame.end - li.frame.start;
- li.frame.end = if (li.cursor > diff) li.cursor else diff;
- li.frame.start = if (li.frame.end - diff < 0) {
- yield 0;
- } else {
- yield li.frame.end - diff;
- };
+ li.frame.start = li.cursor;
+ li.frame.end = li.frame.start + diff;
};
assert(li.frame.start >= 0);
assert(li.frame.end <= len(li.items): int);