summaryrefslogtreecommitdiff
path: root/tui/widget/widget.ha
diff options
context:
space:
mode:
Diffstat (limited to 'tui/widget/widget.ha')
-rw-r--r--tui/widget/widget.ha32
1 files changed, 19 insertions, 13 deletions
diff --git a/tui/widget/widget.ha b/tui/widget/widget.ha
index f2510c6..a56203a 100644
--- a/tui/widget/widget.ha
+++ b/tui/widget/widget.ha
@@ -1,3 +1,4 @@
+use tui;
use fmt;
use unix::tty;
use io;
@@ -13,6 +14,16 @@ export type resizefn = fn(w: *widget, ttysize: tty::ttysize) void;
export type widgetsize = (tty::ttysize | void);
+export type widget = struct {
+ state: *tui::tui,
+ print: *printfn,
+ resize: *resizefn,
+ buf: str,
+ pos: coords,
+ sz: widgetsize,
+ style: (*style | void),
+};
+
export type color = enum uint {
BLACKFG = 30,
REDFG = 31,
@@ -47,16 +58,6 @@ export def DEFAULT_STYLE: style = style {
colorbg = color::DEFAULTBG,
};
-export type widget = struct {
- out: io::file,
- print: *printfn,
- resize: *resizefn,
- buf: str,
- pos: coords,
- sz: widgetsize,
- style: (*style | void),
-};
-
def gotoroot: str = "\x1B[1;1H";
def UNDERLINE: str = "\x1B[4m";
@@ -97,7 +98,6 @@ fn minrows(out: io::file, x: u16, y: widgetsize) (u16 | tty::error) = {
fn color_to_str(color: color) str = fmt::asprintf("\x1B[{}m", strconv::utos(color));
export fn print(w: *widget) void = {
- fmt::fprintf(w.out, "\x1B[{};{}H", w.pos.0, w.pos.1)!;
let s = truncate_to_size(w);
defer free(s);
@@ -105,7 +105,13 @@ export fn print(w: *widget) void = {
let sstyle = applystyles(w.style, s);
defer free(sstyle);
- fmt::fprint(w.out, sstyle)!;
+ const clear = if (w.state.clear) "\x1B[2J" else "";
+ let seekpos = fmt::asprintf("{}\x1B[{};{}H", clear, w.pos.0, w.pos.1);
+ defer free(seekpos);
+ const sout = strings::concat(seekpos, sstyle);
+ defer free(sout);
+
+ fmt::fprint(w.state.out, sout)!;
};
// Applies styling (style) to the given string
@@ -134,7 +140,7 @@ fn truncate_to_size(w: *widget) str = {
let spl = strings::split(w.buf, "\n");
const st = memio::dynamic();
defer io::close(&st)!;
- for (let i = 0z; i < minrows(w.out, len(spl): u16, w.sz)!; i += 1) {
+ for (let i = 0z; i < minrows(w.state.out, len(spl): u16, w.sz)!; i += 1) {
const line = spl[i];
let item = match (w.sz) {
case let sz: tty::ttysize =>