aboutsummaryrefslogtreecommitdiff
path: root/libtui/libtui.ha
diff options
context:
space:
mode:
Diffstat (limited to 'libtui/libtui.ha')
-rw-r--r--libtui/libtui.ha34
1 files changed, 25 insertions, 9 deletions
diff --git a/libtui/libtui.ha b/libtui/libtui.ha
index 3d33680..5f862b0 100644
--- a/libtui/libtui.ha
+++ b/libtui/libtui.ha
@@ -19,8 +19,11 @@ export type ttyui = struct {
term: tty::termios,
f: io::file,
listeners: []listener,
+ doclear: bool,
};
+const CLEARESC: str = "\x1B[2J\x1B[1;1H\r";
+
// Initializes the UI and returns a ttyui.
export fn init() ttyui = {
let f = match (tty::open()) {
@@ -45,16 +48,17 @@ export fn init() ttyui = {
term = term,
f = f,
listeners = [],
+ doclear = false,
};
- hidecursor(ui);
+ hidecursor(&ui);
return ui;
};
-export fn hidecursor(ui: ttyui) void = {
+export fn hidecursor(ui: *ttyui) void = {
print(ui, "\x1B[?25l");
};
-export fn showcursor(ui: ttyui) void = {
+export fn showcursor(ui: *ttyui) void = {
print(ui, "\x1B[?25h");
};
@@ -65,7 +69,7 @@ export fn getwinsize(ui: ttyui) (tty::ttysize | tty::error) = {
// Suspend the UI. To restore it, use [[resume]].
export fn suspend(ui: *ttyui) void = {
- showcursor(*ui);
+ showcursor(ui);
tty::termios_restore(&ui.term);
};
@@ -73,13 +77,13 @@ export fn suspend(ui: *ttyui) void = {
export fn resume(ui: *ttyui) void = {
tty::makeraw(&ui.term)!;
tty::noecho(&ui.term)!;
- hidecursor(*ui);
+ hidecursor(ui);
};
// Restores the UI state and closes and frees the resources associated with the
// given ttyui.
export fn finish(ui: *ttyui) void = {
- showcursor(*ui);
+ showcursor(ui);
tty::termios_restore(&ui.term);
io::close(ui.f)!;
free(ui.listeners);
@@ -134,12 +138,24 @@ export fn addlistener(ui: *ttyui, l: listener) void = {
};
// Print a string or rune to the ttyui.
-export fn print(ui: ttyui, arg: (str | rune)) void = {
- fmt::fprint(ui.f, arg)!;
+export fn print(ui: *ttyui, arg: str) void = {
+ if (ui.doclear) {
+ fmt::fprint(ui.f, strings::concat(CLEARESC, arg))!;
+ ui.doclear = false;
+ } else {
+ 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")!;
+ fmt::fprintf(ui.f, CLEARESC)!;
+};
+
+// Schedule a clear on the next print to the ttyui. Used to optimize printing
+// and avoid intermittent blanking/flickering by grouping the clear and print in
+// the same write syscall.
+export fn doclear(ui: *ttyui) void = {
+ ui.doclear = true;
};