aboutsummaryrefslogtreecommitdiff
path: root/libui/libui.ha
diff options
context:
space:
mode:
Diffstat (limited to 'libui/libui.ha')
-rw-r--r--libui/libui.ha17
1 files changed, 13 insertions, 4 deletions
diff --git a/libui/libui.ha b/libui/libui.ha
index 0546d24..6ad33d1 100644
--- a/libui/libui.ha
+++ b/libui/libui.ha
@@ -17,6 +17,7 @@ export type ttyui = struct {
listeners: []listener,
};
+// Initializes the UI and returns a ttyui.
export fn init() ttyui = {
let f = match (tty::open()) {
case let f: io::file =>
@@ -43,28 +44,36 @@ export fn init() ttyui = {
};
};
+// 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.
export fn notify(ui: *ttyui, r: rune) bool = {
for (let i = 0z; i < len(ui.listeners); i += 1) {
if (ui.listeners[i](ui, r)) {
@@ -94,17 +103,17 @@ fn loop(ui: *ttyui) void = {
};
};
+// 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::fprintf(ui.f, "{}\r\n", 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")!;
};
-
-//export @symbol("wcwidth") fn wcwidth(r: rune) int;