summaryrefslogtreecommitdiff
path: root/ui.ha
blob: 31a1865b08faeba6f3df3efa11a12b01ae7992d3 (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
use fmt;
use io;
use os;
use unix::tty;

fn clear() void = {
	fmt::fprint(os::stdout_file, "\x1b[H\x1b[2J\x1b[3J")!;
};

fn put_at(x: int, y: int, s: str) void = {
	// 0 indexed!
	fmt::fprintf(os::stdout_file, "\x1b[{};{}H{}", y+1,x+1, s)!;
};

fn get_key() u8 = {
	// bad
	let b = [0u8];
	io::read(os::stdin_file, b)!;
	return b[0];
};

fn setup_term() tty::termios = {
	const term = tty::termios_query(os::stdin_file)!;
	tty::makeraw(&term)!;

	// alternate screen, hide cursor
	fmt::fprint(os::stdout_file, "\x1b[?1049h\x1b[?25l")!;

	clear();

	return term;
};

fn finish_term(term: *tty::termios) void = {
	// normal screen, show cursor
	fmt::fprint(os::stdout_file, "\x1b[?1049l\x1b[?25h")!;

	tty::termios_restore(term);
};