summaryrefslogtreecommitdiff
path: root/tui/widget
diff options
context:
space:
mode:
Diffstat (limited to 'tui/widget')
-rw-r--r--tui/widget/list/list.ha7
-rw-r--r--tui/widget/text/text.ha5
-rw-r--r--tui/widget/widget.ha32
3 files changed, 26 insertions, 18 deletions
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 =>