blob: f6014a6fb76f1a9fae6037e1802883ec9764ac7b (
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
|
use fmt;
use io;
use unix::tty;
use errors;
use encoding::utf8;
use bufio;
export type tui = struct {
out: io::file,
clear: bool,
tq: tty::termios,
};
export type skey = enum {
BS,
};
export type key = (skey | rune);
export fn init() (tui | tty::error | errors::error) = {
const f = tty::open()?;
const tq = tty::termios_query(f)?;
tty::noncanonical(&tq)?;
doclear(f);
return tui {
out = f,
clear = false,
tq = tq,
};
};
export fn read(state: *tui) (key | utf8::invalid | io::error | io::EOF) = {
match (bufio::read_rune(state.out)?) {
case let r: rune =>
return r;
case io::EOF =>
return io::EOF;
};
};
fn doclear(out: io::file) void = {
fmt::fprint(out, "\x1B[2J\x1B[1;1H")!;
};
export fn clear(state: *tui) void = {
state.clear = true;
//fmt::fprint(out, "\x1B[2J\x1B[1;1H")!;
};
export fn finish(state: *tui) void = {
tty::termios_restore(&state.tq);
io::close(state.out)!;
};
|