From 6bab265109546396730d84a4189610eca094c62a Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Wed, 12 Mar 2025 15:20:19 +0100 Subject: Refactor and docs --- tui/widget/widget.ha | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tui/widget/widget.ha b/tui/widget/widget.ha index dbbfa11..f2510c6 100644 --- a/tui/widget/widget.ha +++ b/tui/widget/widget.ha @@ -100,10 +100,18 @@ 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); - let sstyle = match (w.style) { + let sstyle = applystyles(w.style, s); + defer free(sstyle); + + fmt::fprint(w.out, sstyle)!; +}; + +// Applies styling (style) to the given string +fn applystyles(st: (*style | void), s: str) str = { + return match (st) { case let st: *style => - defer free(s); let sborder = if (st.border) { yield border(s); } else { @@ -116,12 +124,8 @@ export fn print(w: *widget) void = { defer free(defcolor); yield strings::concat(scolor, sborder, defcolor); case void => - yield s; + yield strings::dup(s); }; - - defer free(sstyle); - - fmt::fprint(w.out, sstyle)!; }; // Truncates the text of the widget to the widget's size (rows and columns) and @@ -148,6 +152,7 @@ fn truncate_to_size(w: *widget) str = { return strings::dup(memio::string(&st)!); }; +// Add a border around a string fn border(s: str) str = { let st = memio::dynamic(); defer io::close(&st)!; -- cgit v1.2.3