use tui; 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 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 else sz.rows; case void => yield tsz.rows; }; if (end > len(items)) { end = len(items): u16; }; return list { widget = widget::widget { state = state, print = &printlist, resize = &resizelist, pos = pos, sz = sz, style = style, ... }, 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)!; memio::concat(&st, list.items[i])!; if (i != list.frame.end - 1) { memio::concat(&st, "\n")!; }; }; list.widget.buf = memio::string(&st)!; widget::print(list); }; export fn resizelist(widget: *widget::widget, ttysize: tty::ttysize) void = { return; };