aboutsummaryrefslogtreecommitdiff
path: root/libui/libui.ha
blob: 7e40a9d4e2d7176a8bb8894eaa67532120dc7bab (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use fmt;
use os;
use io;
use unix::tty;
use errors;
use bufio;
use encoding::utf8;
use strings;


// A listener on a rune input that returns if the ui needs to terminate or not
export type listener = *fn(ui: *ttyui, r: rune) bool;

export type ttyui = struct {
	term: tty::termios,
	f: io::file,
	listeners: []listener,
};

// Initializes the UI and returns a ttyui.
export fn init() ttyui = {
	let f = match (tty::open()) {
	case let f: io::file =>
		yield f;
	case let e: tty::error =>
		fmt::fatal(tty::strerror(e));
	};
	if (!tty::isatty(f)) {
		fmt::fatal("stream is not a tty");
	};
	let term = match (tty::termios_query(f)) {
	case let t: tty::termios =>
		yield t;
	case let e: errors::error =>
		fmt::fatal(errors::strerror(e));
	};
	tty::makeraw(&term)!;
	tty::noecho(&term)!;

	return ttyui {
		term = term,
		f = f,
		listeners = [],
	};
};

// Returns the window size for the given ttyui.
export fn getwinsize(ui: ttyui) (tty::ttysize | tty::error) = {
	return tty::winsize(ui.f);
};

// Suspend the UI. To restore it, use [[resume]].
export fn suspend(ui: *ttyui) void = {
	tty::termios_restore(&ui.term);
};

// Resumes the UI after a [[suspend]].
export fn resume(ui: *ttyui) void = {
	tty::makeraw(&ui.term)!;
	tty::noecho(&ui.term)!;
};

// Restores the UI state and closes and frees the resources associated with the
// given ttyui.
export fn finish(ui: *ttyui) void = {
	tty::termios_restore(&ui.term);
	io::close(ui.f)!;
	free(ui.listeners);
};

// Scans a rune. A convenience function for [[bufio::scanrune]].
export fn scan(ui: ttyui) (rune | utf8::invalid | io::EOF | io::error) = {
	return bufio::scanrune(ui.f);
};

// Notify (call) the ttyui's listeners with the ttyui and r as a parameter.
// Returns true if a listener returned true, false otherwise.
export fn notify(ui: *ttyui, r: rune) bool = {
	for (let i = 0z; i < len(ui.listeners); i += 1) {
		if (ui.listeners[i](ui, r)) {
			return true;
		};
	};
	return false;
};

fn loop(ui: *ttyui) void = {
	for (true) {
		let r = match (bufio::scanrune(ui.f)) {
		case let r: rune =>
			yield r;
		case utf8::invalid =>
			fmt::fatal("Invalid utf8 sequence found");
		case io::EOF =>
			fmt::fatal("EOF");
		case let e: io::error =>
			fmt::fatal(io::strerror(e));
		};
		for (let i = 0z; i < len(ui.listeners); i += 1) {
			if (ui.listeners[i](ui, r)) {
				return;
			};
		};
	};
};

// Add a listener to the given ttyui.
export fn addlistener(ui: *ttyui, l: listener) void = {
	append(ui.listeners, l);
};

// Print a string or rune to the ttyui.
export fn print(ui: ttyui, arg: (str | rune)) void = {
	fmt::fprint(ui.f, arg)!;
	//fmt::fprintf(ui.f, "{}\r", arg)!;
};

// Clear the ttyui.
export fn clear(ui: ttyui) void = {
	fmt::fprintf(ui.f, "\x1B[2J\x1B[1;1H\r")!;
};