aboutsummaryrefslogtreecommitdiff
path: root/libtui/layout
diff options
context:
space:
mode:
Diffstat (limited to 'libtui/layout')
-rw-r--r--libtui/layout/layout.ha55
-rw-r--r--libtui/layout/vlayout/vlayout.ha44
2 files changed, 99 insertions, 0 deletions
diff --git a/libtui/layout/layout.ha b/libtui/layout/layout.ha
new file mode 100644
index 0000000..8391c2f
--- /dev/null
+++ b/libtui/layout/layout.ha
@@ -0,0 +1,55 @@
+use libtui;
+use libtui::widget;
+use io;
+use unix::tty;
+use fmt;
+use os;
+
+export type layout = struct {
+ widgets: []*widget::widget,
+};
+
+// Create and return a new layout from a list of widgets. [[finishall]] must be
+// called to properly free the widget's nad layout's resources.
+export fn newlayout(widgets: *widget::widget...) layout = {
+ return layout {
+ widgets = widgets,
+ };
+};
+
+// Display all the widgets contained in the given layout.
+export fn print(layout: layout) (void | widget::error) = {
+ libtui::clear(layout.widgets[0].ui);
+ for (let i = 0z; i < len(layout.widgets); i += 1) {
+ match (layout.widgets[i].print) {
+ case null =>
+ return;
+ case let f: *widget::print =>
+ f(layout.widgets[i])?;
+ };
+ };
+};
+
+// Finish and free the widgets in the given layout.
+export fn finishall(layout: *layout) void = {
+ for (let i = 0z; i < len(layout.widgets); i += 1) {
+ match (layout.widgets[i].finish) {
+ case null =>
+ return;
+ case let f: *widget::finish =>
+ f(layout.widgets[i]);
+ };
+ };
+ free(layout.widgets);
+};
+
+// Notify all the widgets contained in the given layout. Returns true as soon as
+// one of the widget's listeners returns true, false otherwise.
+export fn notifyall(layout: layout, k: libtui::key) bool = {
+ for (let i = 0z; i < len(layout.widgets); i += 1) {
+ if (widget::notify(layout.widgets[i], k)) {
+ return true;
+ };
+ };
+ return false;
+};
diff --git a/libtui/layout/vlayout/vlayout.ha b/libtui/layout/vlayout/vlayout.ha
new file mode 100644
index 0000000..579625c
--- /dev/null
+++ b/libtui/layout/vlayout/vlayout.ha
@@ -0,0 +1,44 @@
+use libtui;
+use libtui::widget;
+use libtui::layout;
+use unix::tty;
+
+export type vlayout = struct {
+ layout: layout::layout,
+};
+
+// Create and return a new vlayout from a list of widgets. [[finishall]] must be
+// called to properly free the widget's and layout's resources.
+export fn newvlayout(widgets: *widget::widget...) vlayout = {
+ let sz = libtui::getwinsize(u.list.ui)!;
+ let cols = sz.columns / len(widgets);
+ for (let i = 0z; i < len(widgets); i += 1) {
+ const s = tty::ttysize {
+ columns = cols,
+ rows = sz.rows,
+ };
+ widget::setsize(widgets[i], s);
+ };
+ return vlayout {
+ layout = layout {
+ widgets = widgets,
+ },
+ };
+};
+
+// Display all the widgets contained in the given layout.
+export fn print(layout: layout::layout) (void | widget::error) = {
+ const layout = layout: vlayout;
+ for (let i = 0z; i < len(layout.widgets); i += 1) {
+ match (layout.widgets[i].print) {
+ case null =>
+ return;
+ case let f: *widget::print =>
+ f(layout.widgets[i])?;
+ };
+ libtui::cursorhome(widgets[i].ui);
+ for (let j = 0z; j < widgets[i].sz.columns; j += 1) {
+ libtui::cursorright(widgets[i].ui);
+ };
+ };
+};