summaryrefslogtreecommitdiff
path: root/tui
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2025-03-22 18:39:17 +0100
committerJulian Hurst <ark@mansus.space>2025-03-22 18:39:17 +0100
commit4c3c071e7b8f34d3b77a0232ef907350a992a49e (patch)
treedae02bea0265101493e5ad5df26713a317c5d2a3 /tui
parente55266b83db116e1610b562bf186dbda94c549b4 (diff)
downloadhare-tui-4c3c071e7b8f34d3b77a0232ef907350a992a49e.tar.gz
Refactor styles to be widget specific (except border)
Diffstat (limited to 'tui')
-rw-r--r--tui/widget/list/scrolllist.ha60
-rw-r--r--tui/widget/text/text.ha25
-rw-r--r--tui/widget/widget.ha35
3 files changed, 93 insertions, 27 deletions
diff --git a/tui/widget/list/scrolllist.ha b/tui/widget/list/scrolllist.ha
index ab1e7bc..95fa4e0 100644
--- a/tui/widget/list/scrolllist.ha
+++ b/tui/widget/list/scrolllist.ha
@@ -5,6 +5,13 @@ use unix::tty;
use memio;
use strings;
use fmt;
+use strconv;
+
+export def DEFAULTSTYLE = liststyle {
+ style = void,
+ normal = widget::color::DEFAULTFG,
+ marked = widget::color::BLUEBG,
+};
export type scrolllist = struct {
widget: widget::widget,
@@ -12,13 +19,22 @@ export type scrolllist = struct {
frame: frame,
cursor: int,
marked: []int,
+ style: *liststyle,
+};
+
+export type liststyle = struct {
+ style: (void | *widget::style),
+ normal: widget::color,
+ marked: widget::color,
+ //normal: (void | widget::color),
+ //marked: (void | widget::color),
};
// 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 newscrolllist(state: *tui::tui, pos: widget::coords, sz: widget::widgetsize,
-style: (*widget::style | void), items: str...) (scrolllist | tty::error) = {
+style: *liststyle, items: str...) (scrolllist | tty::error) = {
const tsz = tty::winsize(state.out)?;
let end = match (sz) {
@@ -42,7 +58,7 @@ style: (*widget::style | void), items: str...) (scrolllist | tty::error) = {
finish = &finishscrolllist,
pos = pos,
sz = sz,
- style = style,
+ style = style.style,
damage = widget::damageall,
...
},
@@ -53,6 +69,7 @@ style: (*widget::style | void), items: str...) (scrolllist | tty::error) = {
},
cursor = 0,
marked = [],
+ style = style,
};
};
@@ -153,22 +170,47 @@ fn stylesscrolllist(widget: *widget::widget, txt: str, idx: size) str = {
const idx = idx: int + list.frame.start;
let st = memio::dynamic();
defer io::close(&st)!;
+
+ const normalst = colorordefault(list.style.normal, widget::color::DEFAULTFG);
+ const markst = colorordefault(list.style.marked, widget::color::BLUEBG);
+
+ //memio::concat(&st, widget::color_to_str(normalst))!;
+ const normalsts = widget::color_to_str(normalst);
+ defer free(normalsts);
+ memio::concat(&st, normalsts)!;
if (idx == list.cursor) {
memio::concat(&st, "\x1B[7m")!;
};
if (ismarked(*list, idx) is size) {
- memio::concat(&st, "\x1B[44m")!;
+ const s = widget::color_to_str(markst);
+ defer free(s);
+ memio::concat(&st, s)!;
};
memio::concat(&st, txt)!;
- if (idx == list.cursor) {
- memio::concat(&st, "\x1B[27m")!;
- };
- if (ismarked(*list, idx) is size) {
- memio::concat(&st, "\x1B[49m")!;
- };
+ memio::concat(&st, "\x1B[0m")!;
+ //if (idx == list.cursor) {
+ // memio::concat(&st, "\x1B[27m")!;
+ //} else if (ismarked(*list, idx) is size) {
+ //};
+ //if (idx == list.cursor) {
+ // memio::concat(&st, "\x1B[27m")!;
+ //};
+ //if (ismarked(*list, idx) is size) {
+ // memio::concat(&st, "\x1B[49m")!;
+ //};
return strings::dup(memio::string(&st)!);
};
+fn colorordefault(col: (void | widget::color), d: widget::color) widget::color = {
+ const c = match (col) {
+ case let c: widget::color =>
+ yield c;
+ case void =>
+ yield d;
+ };
+ return c;
+};
+
fn ismarked(li: scrolllist, j: int) (size | void) = {
for (let i = 0z; i < len(li.marked); i += 1) {
const idx = li.marked[i];
diff --git a/tui/widget/text/text.ha b/tui/widget/text/text.ha
index 40b3f58..0055d96 100644
--- a/tui/widget/text/text.ha
+++ b/tui/widget/text/text.ha
@@ -2,13 +2,25 @@ use io;
use unix::tty;
use tui;
use tui::widget;
+use strings;
export type text = struct {
widget: widget::widget,
txt: str,
+ style: *style,
};
-export fn newtext(state: *tui::tui, txt: str, pos: widget::coords, style: (*widget::style | void)) text = {
+export def DEFAULTSTYLE = style {
+ style = void,
+ normal = widget::color::DEFAULTFG,
+};
+
+export type style = struct {
+ style: (void | *widget::style),
+ normal: widget::color,
+};
+
+export fn newtext(state: *tui::tui, txt: str, pos: widget::coords, style: *style) text = {
return text {
widget = widget::widget {
state = state,
@@ -17,11 +29,12 @@ export fn newtext(state: *tui::tui, txt: str, pos: widget::coords, style: (*widg
finish = &finishtext,
pos = pos,
sz = void,
- style = style,
+ style = style.style,
damage = widget::damageall,
...
},
txt = txt,
+ style = style,
};
};
@@ -29,6 +42,7 @@ fn printtext(widget: *widget::widget) void = {
const widget = widget: *text;
widget.widget.buf = widget::linesbuf {
lines = [widget.txt],
+ styles = &styles,
...
};
widget::print(widget);
@@ -45,3 +59,10 @@ export fn settext(text: *text, txt: str) void = {
fn finishtext(widget: *widget::widget) void = {
widget::finish(widget);
};
+
+fn styles(widget: *widget::widget, txt: str, idx: size) str = {
+ const txtw = widget: *text;
+ const s = widget::color_to_str(txtw.style.normal);
+ defer free(s);
+ return strings::concat(s, txt, "\x1B[0m");
+};
diff --git a/tui/widget/widget.ha b/tui/widget/widget.ha
index 4212eae..93f3a3e 100644
--- a/tui/widget/widget.ha
+++ b/tui/widget/widget.ha
@@ -72,14 +72,10 @@ export type color = enum uint {
export type style = struct {
border: bool,
- colorfg: color,
- colorbg: color,
};
export def DEFAULT_STYLE: style = style {
border = false,
- colorfg = color::DEFAULTFG,
- colorbg = color::DEFAULTBG,
};
export def NEWLINE: str = "\r\n";
@@ -133,7 +129,14 @@ fn minrows(out: io::file, x: u16, y: widgetsize) (u16 | tty::error) = {
};
// Must free return string
-fn color_to_str(color: color) str = fmt::asprintf("\x1B[{}m", strconv::utos(color));
+export fn color_to_str(color: color) str = fmt::asprintf("\x1B[{}m", strconv::utos(color));
+//{
+// const cu = match (color) {
+// case let c: colorfg => yield c: uint;
+// case let c: colorbg => yield c: uint;
+// };
+// return
+//};
fn clearrow(row: uint) str = fmt::asprintf("\x1B[{}d{}", row, CLEARROW);
@@ -208,17 +211,17 @@ fn applystyles(st: (*style | void), s: []str) []str = {
yield strings::dupall(s);
};
//defer strings::freeall(sborder);
- const scolor = color_to_str(st.colorfg);
- defer free(scolor);
- const defcolor = color_to_str(color::DEFAULTFG);
- defer free(defcolor);
- const sb = strings::concat(scolor, sborder[0]);
- free(sborder[0]);
- sborder[0] = sb;
- const endidx = len(sborder) - 1;
- const sb = strings::concat(sborder[endidx], defcolor);
- free(sborder[endidx]);
- sborder[endidx] = sb;
+ //const scolor = color_to_str(st.colorfg);
+ //defer free(scolor);
+ //const defcolor = color_to_str(colorfg::DEFAULTFG);
+ //defer free(defcolor);
+ //const sb = strings::concat(scolor, sborder[0]);
+ //free(sborder[0]);
+ //sborder[0] = sb;
+ //const endidx = len(sborder) - 1;
+ //const sb = strings::concat(sborder[endidx], defcolor);
+ //free(sborder[endidx]);
+ //sborder[endidx] = sb;
yield sborder;
case void =>
yield strings::dupall(s);