summaryrefslogtreecommitdiff
path: root/tui/widget/list/list.ha
blob: cbf83faaf235b463e3f224aa4b12c767108bd69a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
};