use tui; use tui::widget; use io; use unix::tty; use memio; use strings; export type frame = struct { start: int, end: int, }; 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 void is used, the maximum possible // size is used), items is the slice of items of the list. export fn newlist(state: *tui::tui, pos: widget::coords, sz: widget::widgetsize, style: (*widget::style | void), items: str...) (list | tty::error) = { const tsz = tty::winsize(state.out)?; let end = match (sz) { case let sz: tty::ttysize => yield if (tsz.rows < sz.rows) tsz.rows - 1 else sz.rows; case void => yield tsz.rows - 1; }; let end = end: int; if (end > len(items): int) { end = len(items): int; }; return list { widget = widget::widget { state = state, print = &printlist, resize = &resizelist, finish = &finishlist, pos = pos, sz = sz, style = style, damage = widget::damageall, ... }, items = items, frame = frame { start = 0, end = end: int, }, }; }; export fn printlist(widget: *widget::widget) void = { const list = widget: *list; list.widget.buf = widget::linesbuf { lines = list.items[list.frame.start..list.frame.end], styles = null, }; widget::print(list); }; export fn resizelist(widget: *widget::widget, ttysize: tty::ttysize) void = { return; }; fn finishlist(widget: *widget::widget) void = { widget::finish(widget); };