From 6cb9d6f8756e690e6cd2360b8b1ab531da45c405 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Fri, 21 Mar 2025 18:00:02 +0100 Subject: Simply and fix reframe and introduce bounds checks for setcursor --- cmd/il.ha | 16 ++++++++++++++++ tui/widget/list/scrolllist.ha | 26 ++++++++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/cmd/il.ha b/cmd/il.ha index afc1ee5..b6d1efb 100644 --- a/cmd/il.ha +++ b/cmd/il.ha @@ -61,6 +61,22 @@ export fn main() void = { if (r == 'K') { list::frameup(&li); }; + if (r == 'd') { + const nextpage = li.cursor + li.frame.end - li.frame.start; + fmt::errorln(nextpage)!; + list::setcursor(&li, nextpage); + fmt::errorln(li.frame.start)!; + fmt::errorln(li.frame.end)!; + fmt::errorln()!; + }; + if (r == 'u') { + const prevpage = li.cursor - (li.frame.end - li.frame.start); + fmt::errorln(prevpage)!; + list::setcursor(&li, prevpage); + fmt::errorln(li.frame.start)!; + fmt::errorln(li.frame.end)!; + fmt::errorln()!; + }; if (r == 'g') { list::top(&li); }; 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); -- cgit v1.2.3