summaryrefslogtreecommitdiff
path: root/tui/widget/text/text.ha
blob: 0055d96ac90abcb2b068bab335a2fdae458f4ee5 (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
use io;
use unix::tty;
use tui;
use tui::widget;
use strings;

export type text = struct {
	widget: widget::widget,
	txt: str,
	style: *style,
};

export def DEFAULTSTYLE = style {
	style = void,
	normal = widget::color::DEFAULTFG,
};

export type style = struct {
	style: (void | *widget::style),
	normal: widget::color,
};

export fn newtext(state: *tui::tui, txt: str, pos: widget::coords, style: *style) text = {
	return text {
		widget = widget::widget {
			state = state,
			print = &printtext,
			resize = &resizetext,
			finish = &finishtext,
			pos = pos,
			sz = void,
			style = style.style,
			damage = widget::damageall,
			...
		},
		txt = txt,
		style = style,
	};
};

fn printtext(widget: *widget::widget) void = {
	const widget = widget: *text;
	widget.widget.buf = widget::linesbuf {
		lines = [widget.txt],
		styles = &styles,
		...
	};
	widget::print(widget);
};

fn resizetext(widget: *widget::widget, ttysize: tty::ttysize) void = {
	return;
};

export fn settext(text: *text, txt: str) void = {
	text.txt = txt;
};

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

fn styles(widget: *widget::widget, txt: str, idx: size) str = {
	const txtw = widget: *text;
	const s = widget::color_to_str(txtw.style.normal);
	defer free(s);
	return strings::concat(s, txt, "\x1B[0m");
};