summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--cmd/list.ha16
-rw-r--r--cmd/text.ha17
-rw-r--r--tui/layout/layout.ha14
-rw-r--r--tui/layout/vlayout.ha26
-rw-r--r--tui/tui.ha14
-rw-r--r--tui/widget/widget.ha26
7 files changed, 115 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..52e4b20
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+text
+list
diff --git a/cmd/list.ha b/cmd/list.ha
new file mode 100644
index 0000000..8c93e1a
--- /dev/null
+++ b/cmd/list.ha
@@ -0,0 +1,16 @@
+use tui;
+use tui::layout;
+use tui::widget::list;
+use unix::tty;
+use io;
+use fmt;
+use time;
+
+export fn main() void = {
+ const out = tui::init()!;
+ defer io::close(out)!;
+ const sz = tty::winsize(out)!;
+ let li = list::newlist(out, (1, 1), sz, "hello", "world")!;
+ let l = layout::newvlayout(&li);
+ l.layout.print(&l);
+};
diff --git a/cmd/text.ha b/cmd/text.ha
new file mode 100644
index 0000000..6e6e102
--- /dev/null
+++ b/cmd/text.ha
@@ -0,0 +1,17 @@
+use tui;
+use tui::layout;
+use tui::widget::text;
+use io;
+use fmt;
+use time;
+
+export fn main() void = {
+ const out = tui::init()!;
+ defer io::close(out)!;
+ let txt = text::newtext(out, "hello world", (50, 20));
+ let l = layout::newvlayout(&txt);
+ l.layout.print(&l);
+ time::sleep(5 * time::SECOND);
+ text::settext(&txt, "bye world");
+ l.layout.print(&l);
+};
diff --git a/tui/layout/layout.ha b/tui/layout/layout.ha
new file mode 100644
index 0000000..d425891
--- /dev/null
+++ b/tui/layout/layout.ha
@@ -0,0 +1,14 @@
+use tui::widget;
+
+export type printfn = fn(l: *layout) void;
+export type finishfn = fn(l: *layout) void;
+
+export type layout = struct {
+ widgets: []*widget::widget,
+ print: *printfn,
+ finish: *finishfn,
+};
+
+fn finish(l: *layout) void = {
+ free(l.widgets);
+};
diff --git a/tui/layout/vlayout.ha b/tui/layout/vlayout.ha
new file mode 100644
index 0000000..0381a9f
--- /dev/null
+++ b/tui/layout/vlayout.ha
@@ -0,0 +1,26 @@
+use tui::widget;
+
+export type vlayout = struct {
+ layout: layout,
+};
+
+export fn newvlayout(widgets: *widget::widget...) vlayout = {
+ return vlayout {
+ layout = layout {
+ widgets = widgets,
+ print = &printvlayout,
+ finish = &finishvlayout,
+ },
+ };
+};
+
+fn printvlayout(l: *layout) void = {
+ for (let widget .. l.widgets) {
+ widget.print(widget);
+ };
+};
+
+fn finishvlayout(l: *layout) void = {
+ let vl = l: *vlayout;
+ finish(&vl.layout);
+};
diff --git a/tui/tui.ha b/tui/tui.ha
new file mode 100644
index 0000000..871f847
--- /dev/null
+++ b/tui/tui.ha
@@ -0,0 +1,14 @@
+use fmt;
+use io;
+use unix::tty;
+
+export fn init() (io::file | tty::error) = {
+ const f = tty::open()?;
+ clear(f);
+ return f;
+
+};
+
+export fn clear(out: io::file) void = {
+ fmt::fprint(out, "\x1B[2J\x1B[1;1H")!;
+};
diff --git a/tui/widget/widget.ha b/tui/widget/widget.ha
new file mode 100644
index 0000000..cb202be
--- /dev/null
+++ b/tui/widget/widget.ha
@@ -0,0 +1,26 @@
+use fmt;
+use unix::tty;
+use io;
+
+export type coords = (u16, u16);
+
+export type printfn = fn(w: *widget) void;
+export type resizefn = fn(w: *widget, ttysize: tty::ttysize) void;
+
+export type nosize = void;
+export type widgetsize = (tty::ttysize | nosize);
+
+export type widget = struct {
+ out: io::file,
+ print: *printfn,
+ resize: *resizefn,
+ pos: coords,
+ sz: widgetsize,
+};
+
+def gotoroot: str = "\x1B[1;1f";
+
+export fn print(out: io::file, s: str, pos: coords) void = {
+ fmt::fprintf(out, "\x1B[{};{}H", pos.0, pos.1)!;
+ fmt::fprint(out, s)!;
+};