use tui::widget; use io; use unix::tty; use memio; use strings; export type frame = struct { start: u16, end: u16, }; export type list = struct { widget: widget::widget, items: []str, frame: frame, }; // Return an instance of list. out is the tty file, pos the starting position, // sz is the size of the widget (if [[widget::nosize]] is used, the maximum possible // size is used), items is the slice of items of the list. export fn newlist(out: io::file, pos: widget::coords, sz: widget::widgetsize, items: str...) (list | tty::error) = { const tsz = tty::winsize(out)?; let end = match (sz) { case let sz: tty::ttysize => yield if (tsz.rows < sz.rows) tsz.rows else sz.rows; case widget::nosize => yield tsz.rows; }; if (end > len(items)) { end = len(items): u16; }; return list { widget = widget::widget { out = out, print = &printlist, resize = &resizelist, pos = pos, sz = sz, }, items = items, frame = frame { start = 0, end = end, }, }; }; export fn printlist(widget: *widget::widget) void = { const list = widget: *list; let st = memio::dynamic(); defer io::close(&st)!; for (let i = list.frame.start; i < list.frame.end; i += 1) { let item = match (list.widget.sz) { case let sz: tty::ttysize => yield strings::sub(list.items[i], 0z, sz.columns); case widget::nosize => yield list.items[i]; }; memio::concat(&st, item)!; if (i != list.frame.end - 1) { memio::concat(&st, "\n")!; }; }; widget::print(list.widget.out, memio::string(&st)!, (1, 1)); }; export fn resizelist(widget: *widget::widget, ttysize: tty::ttysize) void = { return; };