summaryrefslogtreecommitdiff
path: root/tui/widget/list/scrolllist.ha
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2025-03-22 15:46:40 +0100
committerJulian Hurst <ark@mansus.space>2025-03-22 15:46:40 +0100
commit734992c356f597268b0529ec8402efcf49a57a76 (patch)
tree44c1b85fa4ed66c6fd01da17dbf3f50302f79aa0 /tui/widget/list/scrolllist.ha
parent8833e41e377aec25b57df6c30b70ebee1e78c8df (diff)
downloadhare-tui-734992c356f597268b0529ec8402efcf49a57a76.tar.gz
scrolllist: add support for marking items
Diffstat (limited to 'tui/widget/list/scrolllist.ha')
-rw-r--r--tui/widget/list/scrolllist.ha32
1 files changed, 32 insertions, 0 deletions
diff --git a/tui/widget/list/scrolllist.ha b/tui/widget/list/scrolllist.ha
index 0079363..ab1e7bc 100644
--- a/tui/widget/list/scrolllist.ha
+++ b/tui/widget/list/scrolllist.ha
@@ -11,6 +11,7 @@ export type scrolllist = struct {
items: []str,
frame: frame,
cursor: int,
+ marked: []int,
};
// Return an instance of list. out is the tty file, pos the starting position,
@@ -51,6 +52,7 @@ style: (*widget::style | void), items: str...) (scrolllist | tty::error) = {
end = end,
},
cursor = 0,
+ marked = [],
};
};
@@ -70,7 +72,9 @@ fn resizescrolllist(widget: *widget::widget, ttysize: tty::ttysize) void = {
};
fn finishscrolllist(widget: *widget::widget) void = {
+ const list = widget: *scrolllist;
widget::finish(widget);
+ free(list.marked);
};
export fn down(li: *scrolllist) void = {
@@ -152,9 +156,37 @@ fn stylesscrolllist(widget: *widget::widget, txt: str, idx: size) str = {
if (idx == list.cursor) {
memio::concat(&st, "\x1B[7m")!;
};
+ if (ismarked(*list, idx) is size) {
+ memio::concat(&st, "\x1B[44m")!;
+ };
memio::concat(&st, txt)!;
if (idx == list.cursor) {
memio::concat(&st, "\x1B[27m")!;
};
+ if (ismarked(*list, idx) is size) {
+ memio::concat(&st, "\x1B[49m")!;
+ };
return strings::dup(memio::string(&st)!);
};
+
+fn ismarked(li: scrolllist, j: int) (size | void) = {
+ for (let i = 0z; i < len(li.marked); i += 1) {
+ const idx = li.marked[i];
+ if (idx == j) {
+ return i;
+ };
+ };
+ return;
+};
+
+export fn mark(li: *scrolllist) bool = {
+ defer down(li);
+ match (ismarked(*li, li.cursor)) {
+ case let s: size =>
+ delete(li.marked[s]);
+ return false;
+ case void =>
+ append(li.marked, li.cursor);
+ return true;
+ };
+};