diff options
| author | Julian Hurst <ark@mansus.space> | 2025-03-12 22:36:13 +0100 |
|---|---|---|
| committer | Julian Hurst <ark@mansus.space> | 2025-03-12 22:36:13 +0100 |
| commit | e074c936bb99ccfce311445f4dbf42ba964b44a4 (patch) | |
| tree | a2ca68130a4a6a532df3f7362d3cad0574d50259 /tui | |
| parent | 6bab265109546396730d84a4189610eca094c62a (diff) | |
| download | hare-tui-e074c936bb99ccfce311445f4dbf42ba964b44a4.tar.gz | |
Implement global state and clear scheduling
Diffstat (limited to 'tui')
| -rw-r--r-- | tui/tui.ha | 25 | ||||
| -rw-r--r-- | tui/widget/list/list.ha | 7 | ||||
| -rw-r--r-- | tui/widget/text/text.ha | 5 | ||||
| -rw-r--r-- | tui/widget/widget.ha | 32 |
4 files changed, 47 insertions, 22 deletions
@@ -2,13 +2,30 @@ use fmt; use io; use unix::tty; -export fn init() (io::file | tty::error) = { +export type tui = struct { + out: io::file, + clear: bool, +}; + +export fn init() (tui | tty::error) = { const f = tty::open()?; - clear(f); - return f; + doclear(f); + return tui { + out = f, + clear = false, + }; }; -export fn clear(out: io::file) void = { +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 = { + io::close(state.out)!; +}; diff --git a/tui/widget/list/list.ha b/tui/widget/list/list.ha index 50fb8df..ca7571b 100644 --- a/tui/widget/list/list.ha +++ b/tui/widget/list/list.ha @@ -1,3 +1,4 @@ +use tui; use tui::widget; use io; use unix::tty; @@ -18,9 +19,9 @@ export type list = struct { // Return an instance of list. out is the tty file, pos the starting position, // sz is the size of the widget (if void is used, the maximum possible // size is used), items is the slice of items of the list. -export fn newlist(out: io::file, pos: widget::coords, sz: widget::widgetsize, +export fn newlist(state: *tui::tui, pos: widget::coords, sz: widget::widgetsize, style: (*widget::style | void), items: str...) (list | tty::error) = { - const tsz = tty::winsize(out)?; + const tsz = tty::winsize(state.out)?; let end = match (sz) { case let sz: tty::ttysize => @@ -35,7 +36,7 @@ style: (*widget::style | void), items: str...) (list | tty::error) = { return list { widget = widget::widget { - out = out, + state = state, print = &printlist, resize = &resizelist, pos = pos, diff --git a/tui/widget/text/text.ha b/tui/widget/text/text.ha index ecb5c2c..d849661 100644 --- a/tui/widget/text/text.ha +++ b/tui/widget/text/text.ha @@ -1,5 +1,6 @@ use io; use unix::tty; +use tui; use tui::widget; use strings; @@ -8,10 +9,10 @@ export type text = struct { txt: str, }; -export fn newtext(out: io::file, txt: str, pos: widget::coords, style: (*widget::style | void)) text = { +export fn newtext(state: *tui::tui, txt: str, pos: widget::coords, style: (*widget::style | void)) text = { return text { widget = widget::widget { - out = out, + state = state, print = &printtext, resize = &resizetext, pos = pos, 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 => |
