summaryrefslogtreecommitdiff
path: root/tui/widget/list/list.ha
blob: 23127ac8bfabacf9fe3f73283bd8a1d4e8966ef5 (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
74
75
76
77
78
79
80
81
82
83
84
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,
			finish = &finishlist,
			pos = pos,
			sz = sz,
			style = style,
			damage = widget::damageall,
			...
		},
		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;
};

fn finishlist(widget: *widget::widget) void = {
	widget::finish(widget);
};