aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <julian.hurst92@gmail.com>2022-06-15 10:46:03 +0200
committerJulian Hurst <julian.hurst92@gmail.com>2022-06-15 10:46:03 +0200
commita95fcb445bbc9299eb1d974ce34db8cf24622c10 (patch)
treec88fee737c02c8f8894f2e08c35d85fa9a45dfd7
parentaa288b25a00385e8b034735838f3d5cc512df93f (diff)
downloadilhare-a95fcb445bbc9299eb1d974ce34db8cf24622c10.tar.gz
Externalize modules and add .gitignore
-rw-r--r--.gitignore2
-rw-r--r--handlers.ha51
-rw-r--r--libui/README1
-rw-r--r--libui/layout/layout.ha44
-rw-r--r--libui/libui.ha134
-rw-r--r--libui/widget/README2
-rw-r--r--libui/widget/list/README1
-rw-r--r--libui/widget/list/list.ha351
-rw-r--r--libui/widget/widget.ha65
-rw-r--r--main.ha32
-rw-r--r--set/set.ha85
11 files changed, 49 insertions, 719 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..79d3f79
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+il
+strset
diff --git a/handlers.ha b/handlers.ha
index 68d7556..c50e454 100644
--- a/handlers.ha
+++ b/handlers.ha
@@ -1,6 +1,6 @@
-use libui;
-use libui::widget;
-use libui::widget::list;
+use libtui;
+use libtui::widget;
+use libtui::widget::list;
use sort;
use slices;
use encoding::utf8;
@@ -14,15 +14,26 @@ use unix::tty;
let itemscopy: []str = [];
-fn globalrunehandler(ui: *libui::ttyui, r: rune) bool = {
- if (r == 'q') {
- return true;
+fn globalrunehandler(ui: *libtui::ttyui, r: libtui::key) bool = {
+ match (r) {
+ case let r: rune =>
+ if (r == 'q') {
+ return true;
+ };
+ case let r: libtui::specialkey =>
+ yield;
};
return false;
};
-fn runehandler(l: *widget::widget, r: rune) bool = {
+fn runehandler(l: *widget::widget, r: libtui::key) bool = {
const l = l: *list::listwidget;
+ const r = match (r) {
+ case let r: rune =>
+ yield r;
+ case let r: libtui::specialkey =>
+ return false;
+ };
switch (r) {
case 'j' =>
list::down(l);
@@ -34,7 +45,7 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
list::pageup(l);
case 'l' =>
// to print properly suspend the ui, print, then resume
- libui::suspend(&l.widget.ui);
+ libtui::suspend(&l.widget.ui);
//fmt::println(l.items[l.cursor])!;
match (list::selected(*l)) {
case let s: str =>
@@ -45,7 +56,7 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
defer free(out);
fmt::println(out)!;
};
- libui::resume(&l.widget.ui);
+ libtui::resume(&l.widget.ui);
return true;
case 'g' =>
list::top(l);
@@ -65,7 +76,7 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
};
case '?' =>
// TODO add commandline support maybe
- libui::suspend(&l.widget.ui);
+ libtui::suspend(&l.widget.ui);
fmt::fprint(l.widget.ui.f, '?')!;
let line = match (bufio::scanline(l.widget.ui.f)) {
case let s: []u8 =>
@@ -82,10 +93,10 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
searchforward = false;
let c = l.cursor;
list::rsearch(l, searchterm);
- libui::resume(&l.widget.ui);
+ libtui::resume(&l.widget.ui);
case '/' =>
// TODO add commandline support maybe
- libui::suspend(&l.widget.ui);
+ libtui::suspend(&l.widget.ui);
fmt::fprint(l.widget.ui.f, '/')!;
let line = match (bufio::scanline(l.widget.ui.f)) {
case let s: []u8 =>
@@ -102,7 +113,7 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
searchforward = true;
let c = l.cursor;
list::search(l, searchterm);
- libui::resume(&l.widget.ui);
+ libtui::resume(&l.widget.ui);
case 'o' =>
// order
sort::strings(l.items);
@@ -114,7 +125,7 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
list::setitems(l, itemscopy...);
case 's' =>
// TODO add commandline support maybe
- libui::suspend(&l.widget.ui);
+ libtui::suspend(&l.widget.ui);
fmt::fprint(l.widget.ui.f, "s: ")!;
let line = match (bufio::scanline(l.widget.ui.f)) {
case let s: []u8 =>
@@ -128,10 +139,10 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
};
defer free(line);
list::containsmark(l, strings::fromutf8(line));
- libui::resume(&l.widget.ui);
+ libtui::resume(&l.widget.ui);
case 'S' =>
// TODO add commandline support maybe
- libui::suspend(&l.widget.ui);
+ libtui::suspend(&l.widget.ui);
fmt::fprint(l.widget.ui.f, "S: ")!;
let line = match (bufio::scanline(l.widget.ui.f)) {
case let s: []u8 =>
@@ -145,10 +156,10 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
};
defer free(line);
list::fnmatchmark(l, strings::fromutf8(line));
- libui::resume(&l.widget.ui);
+ libtui::resume(&l.widget.ui);
case 'r' =>
// TODO add commandline support maybe
- libui::suspend(&l.widget.ui);
+ libtui::suspend(&l.widget.ui);
fmt::fprint(l.widget.ui.f, "r: ")!;
let line = match (bufio::scanline(l.widget.ui.f)) {
case let s: []u8 =>
@@ -168,7 +179,7 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
case let e: regex::error =>
fmt::fprintln(os::stderr, regex::strerror(e))!;
};
- libui::resume(&l.widget.ui);
+ libtui::resume(&l.widget.ui);
case ' ' =>
list::tmark(l);
list::down(l);
@@ -183,7 +194,7 @@ fn runehandler(l: *widget::widget, r: rune) bool = {
fmt::fprintln(os::stderr, "newline")!;
};
};
- libui::clear(l.widget.ui);
+ libtui::clear(l.widget.ui);
match (list::print(l)) {
case void =>
yield;
diff --git a/libui/README b/libui/README
deleted file mode 100644
index df8992f..0000000
--- a/libui/README
+++ /dev/null
@@ -1 +0,0 @@
-The libui module provides widgets for creating a tui (terminal user interface).
diff --git a/libui/layout/layout.ha b/libui/layout/layout.ha
deleted file mode 100644
index c15626f..0000000
--- a/libui/layout/layout.ha
+++ /dev/null
@@ -1,44 +0,0 @@
-use libui;
-use libui::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) = {
- libui::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);
-};
diff --git a/libui/libui.ha b/libui/libui.ha
deleted file mode 100644
index b81c971..0000000
--- a/libui/libui.ha
+++ /dev/null
@@ -1,134 +0,0 @@
-use fmt;
-use os;
-use io;
-use unix::tty;
-use errors;
-use bufio;
-use encoding::utf8;
-use strings;
-
-
-// A listener on a rune input that returns if the ui needs to terminate or not
-export type listener = *fn(ui: *ttyui, r: rune) bool;
-
-export type ttyui = struct {
- term: tty::termios,
- f: io::file,
- listeners: []listener,
-};
-
-// Initializes the UI and returns a ttyui.
-export fn init() ttyui = {
- let f = match (tty::open()) {
- case let f: io::file =>
- yield f;
- case let e: tty::error =>
- fmt::fatal(tty::strerror(e));
- };
- if (!tty::isatty(f)) {
- fmt::fatal("stream is not a tty");
- };
- let term = match (tty::termios_query(f)) {
- case let t: tty::termios =>
- yield t;
- case let e: errors::error =>
- fmt::fatal(errors::strerror(e));
- };
- tty::makeraw(&term)!;
- //tty::noecho(&term)!;
-
- let ui = ttyui {
- term = term,
- f = f,
- listeners = [],
- };
- hidecursor(ui);
- return ui;
-};
-
-export fn hidecursor(ui: ttyui) void = {
- print(ui, "\x1B[?25l");
-};
-
-export fn showcursor(ui: ttyui) void = {
- print(ui, "\x1B[?25h");
-};
-
-// Returns the window size for the given ttyui.
-export fn getwinsize(ui: ttyui) (tty::ttysize | tty::error) = {
- return tty::winsize(ui.f);
-};
-
-// Suspend the UI. To restore it, use [[resume]].
-export fn suspend(ui: *ttyui) void = {
- showcursor(*ui);
- tty::termios_restore(&ui.term);
-};
-
-// Resumes the UI after a [[suspend]].
-export fn resume(ui: *ttyui) void = {
- tty::makeraw(&ui.term)!;
- tty::noecho(&ui.term)!;
- hidecursor(*ui);
-};
-
-// Restores the UI state and closes and frees the resources associated with the
-// given ttyui.
-export fn finish(ui: *ttyui) void = {
- showcursor(*ui);
- tty::termios_restore(&ui.term);
- io::close(ui.f)!;
- free(ui.listeners);
-};
-
-// Scans a rune. A convenience function for [[bufio::scanrune]].
-export fn scan(ui: ttyui) (rune | utf8::invalid | io::EOF | io::error) = {
- return bufio::scanrune(ui.f);
-};
-
-// Notify (call) the ttyui's listeners with the ttyui and r as a parameter.
-// Returns true if a listener returned true, false otherwise.
-export fn notify(ui: *ttyui, r: rune) bool = {
- for (let i = 0z; i < len(ui.listeners); i += 1) {
- if (ui.listeners[i](ui, r)) {
- return true;
- };
- };
- return false;
-};
-
-fn loop(ui: *ttyui) void = {
- for (true) {
- let r = match (bufio::scanrune(ui.f)) {
- case let r: rune =>
- yield r;
- case utf8::invalid =>
- fmt::fatal("Invalid utf8 sequence found");
- case io::EOF =>
- fmt::fatal("EOF");
- case let e: io::error =>
- fmt::fatal(io::strerror(e));
- };
- for (let i = 0z; i < len(ui.listeners); i += 1) {
- if (ui.listeners[i](ui, r)) {
- return;
- };
- };
- };
-};
-
-// Add a listener to the given ttyui.
-export fn addlistener(ui: *ttyui, l: listener) void = {
- append(ui.listeners, l);
-};
-
-// Print a string or rune to the ttyui.
-export fn print(ui: ttyui, arg: (str | rune)) void = {
- fmt::fprint(ui.f, arg)!;
- //fmt::fprintf(ui.f, "{}\r", arg)!;
-};
-
-// Clear the ttyui.
-export fn clear(ui: ttyui) void = {
- fmt::fprintf(ui.f, "\x1B[2J\x1B[1;1H\r")!;
-};
diff --git a/libui/widget/README b/libui/widget/README
deleted file mode 100644
index ae62628..0000000
--- a/libui/widget/README
+++ /dev/null
@@ -1,2 +0,0 @@
-This module contains functions common to all widgets and provides a base for
-implementing custom widget types (print, finish, ...).
diff --git a/libui/widget/list/README b/libui/widget/list/README
deleted file mode 100644
index c8f71db..0000000
--- a/libui/widget/list/README
+++ /dev/null
@@ -1 +0,0 @@
-libui::list provides a list widget that supports line truncating, scrolling, item marking (contains, fnmatch and regex) and searching. Multiple convenient navigation functions are provided such as [[up]], [[down]], [[top]] and [[bottom]].
diff --git a/libui/widget/list/list.ha b/libui/widget/list/list.ha
deleted file mode 100644
index d8906d3..0000000
--- a/libui/widget/list/list.ha
+++ /dev/null
@@ -1,351 +0,0 @@
-use libui;
-use libui::widget;
-use fmt;
-use os;
-use strings;
-use io;
-use strio;
-use unix::tty;
-use regex;
-use fnmatch;
-use wcwidth;
-use set;
-
-export type listwidget = struct {
- widget: widget::widget,
- items: []str,
- marked: set::set,
- cursor: size,
- //listeners: []listener,
- frame: frame,
- sz: ttysize,
-};
-
-export type frame = struct {
- start: u16,
- // largest value is nb of items
- end: u16,
-};
-
-export type ttysize = struct {
- rows: u16,
- cols: u16,
-};
-
-// An input listener on a list widget. The returning value is intended to be
-// used as a signal that will be returned by [[notify]] in order to trigger
-// certain more global ui events (terminate the program, change widget focus,
-// etc.). To register a listener with a listwidget, use [[addlistener]].
-export type listener = *fn(l: *listwidget, r: rune) bool;
-
-// Create a new list with the given items.
-export fn newlist(ui: libui::ttyui, items: str...) listwidget = {
- let sz = libui::getwinsize(ui)!;
- let rows = sz.rows - 2;
- //let rows: (u16 | size) = if (sz.rows - 2 < len(items)) {
- //yield sz.rows - 2;
- //} else {
- //yield len(items);
- //};
- let w = listwidget {
- widget = widget::widget {
- print = &print,
- finish = &finish,
- ui = ui,
- ...
- },
- items = items,
- marked = set::set {...},
- cursor = 0z,
- //listeners = [],
- frame = frame {
- start = 0u16,
- end = rows: u16,
- },
- sz = ttysize {
- rows = rows: u16,
- cols = sz.columns,
- },
- };
- return w;
-};
-
-// Free the list's items, marked items and call the common widget finish
-// function [[widget::finishcommon]].
-export fn finish(list: *widget::widget) void = {
- const list = list: *listwidget;
- free(list.items);
- set::finish(&list.marked);
- widget::finishcommon(list);
-};
-
-// Set the list's items. If the length of the given items is smaller than the
-// list's current items, the cursor will be set to 0z and [[reframe]] will be
-// called to reset the frame.
-export fn setitems(list: *listwidget, items: str...) void = {
- const doreset = len(items) < len(list.items);
- list.items = strings::dupall(items);
- if (doreset) {
- reset(list);
- };
-};
-
-export fn reset(list: *listwidget) void = {
- list.cursor = 0z;
- list.frame.start = 0u16;
- list.frame.end = list.sz.rows;
-};
-
-// Add a listener to the given list.
-//export fn addlistener(list: *listwidget, l: listener) void = {
- //append(list.listeners, l);
-//};
-
-const SELECTED: str = "\x1B[104;1m\x1B[30m";
-const MARKED: str = "\x1B[46;1m\x1B[30m";
-const RESET: str = "\x1B[0m";
-
-// Print the list's items while truncating the items to not be wider than the
-// list.sz.cols.
-export fn print(list: *widget::widget) (void | widget::error) = {
- const list = list: *listwidget;
- //let sz = libui::getwinsize(list.ui)?;
- //let rows: (u16 | size) = if (sz.rows - 2 < len(list.items)) {
- //yield sz.rows - 2;
- //} else {
- //yield len(list.items);
- //};
-
- //fmt::fprintln(os::stderr, rows)!;
-
- //list.frame.end = list.frame.start + list.sz.rows;
-
- let st = strio::dynamic();
- strio::concat(&st, "\r")?;
- let end = if (list.frame.end < len(list.items)) {
- yield list.frame.end;
- } else {
- yield len(list.items);
- };
- for (let i = list.frame.start; i < end: u16; i += 1) {
- const item = list.items[i];
- const truncitem = wcwidth::truncate(item, list.sz.cols);
- if (list.cursor == i) {
- strio::concat(&st, SELECTED, truncitem, RESET)?;
- //libui::print(list.ui, strings::concat("\x1B[31;1m> ", list.items[i], "\x1B[0m"));
- } else if (set::contains(list.marked, i) is size){
- strio::concat(&st, MARKED, truncitem, RESET)?;
- //libui::print(list.ui, list.items[i]);
- } else {
- strio::concat(&st, truncitem)?;
- };
- strio::concat(&st, "\r\n")?;
- };
- // unsupported?
- //io::copy(list.ui.f, &st)?;
- let s = strio::string(&st);
- libui::print(list.widget.ui, s);
- io::close(&st)?;
-};
-
-// Notify (call) the listwidget's listeners with the listwidget and r as a
-// parameter. Returns true if a listener returned true, false otherwise.
-export fn notify(l: *listwidget, r: rune) bool = {
- for (let i = 0z; i < len(l.widget.listeners); i += 1) {
- if (l.widget.listeners[i](l, r)) {
- return true;
- };
- };
- return false;
-};
-
-// Reset the list's frame based on the cursor. Returns whether the frame was
-// updated.
-export fn reframe(l: *listwidget) bool = {
- let reframed: bool = false;
- if (l.cursor < l.frame.start) {
- l.frame.start = l.cursor: u16;
- l.frame.end = l.frame.start + l.sz.rows;
- //l.frame.end -= l.frame.start - l.cursor: u16;
- reframed = true;
- };
- if (l.cursor >= l.frame.end) {
- l.frame.start += l.cursor: u16 - l.frame.end + 1;
- l.frame.end = l.cursor: u16 + 1;
- reframed = true;
- };
- return reframed;
-};
-
-export fn resize(l: *listwidget, oldsz: ttysize) bool = {
- if (l.frame.end - l.frame.start != l.sz.rows) {
- if (l.cursor < (l.frame.end / 2)) {
- fmt::fprintln(os::stderr, "closer to start")!;
- l.frame.end = if (l.frame.start + l.sz.rows > len(l.items)) {
- l.frame.start = len(l.items): u16 - l.sz.rows;
- yield len(l.items): u16;
- } else {
- yield l.frame.start + l.sz.rows;
- };
- for (l.cursor > l.frame.end) {
- fmt::fprintln(os::stderr, "looping")!;
- l.frame.start += 1;
- l.frame.end += 1;
- };
- } else {
- fmt::fprintln(os::stderr, "closer to end")!;
- l.frame.start = if (l.frame.end: int - l.sz.rows: int < 0) {
- l.frame.end = 0 + l.sz.rows;
- yield 0;
- } else {
- yield l.frame.end - l.sz.rows;
- };
- for (l.cursor < l.frame.start) {
- fmt::fprintln(os::stderr, "looping")!;
- l.frame.start -= 1;
- l.frame.end -= 1;
- };
- };
- return true;
- };
- return false;
-};
-
-fn cursorinframe(l: *listwidget) bool = {
- return l.cursor >= l.frame.start && l.cursor < l.frame.end;
-};
-
-// Move the list's cursor down one item. Returns the new cursor.
-export fn down(l: *listwidget) size = {
- if (l.cursor < len(l.items) - 1) {
- l.cursor += 1;
- reframe(l);
- };
- return l.cursor;
-};
-
-// Move the list's cursor up one item. Returns the new cursor.
-export fn up(l: *listwidget) size = {
- if (l.cursor > 0) {
- l.cursor -= 1;
- reframe(l);
- };
- return l.cursor;
-};
-
-// Move the list's cursor up one page. Returns the new cursor.
-export fn pageup(l: *listwidget) size = {
- if (l.cursor: int - l.sz.rows: int >= 0) {
- l.cursor -= l.sz.rows;
- } else {
- l.cursor = 0z;
- };
- reframe(l);
- return l.cursor;
-};
-
-// Move the list's cursor down one page. Returns the new cursor.
-export fn pagedown(l: *listwidget) size = {
- if (l.cursor + l.sz.rows < len(l.items)) {
- l.cursor += l.sz.rows;
- } else {
- l.cursor = len(l.items) - 1;
- };
- reframe(l);
- return l.cursor;
-};
-
-// Move the list's cursor to the top (first item). Returns the new cursor.
-export fn top(l: *listwidget) size = {
- l.cursor = 0;
- l.frame.start = 0;
- l.frame.end = l.frame.start + l.sz.rows;
- return l.cursor;
-};
-
-// Move the list's cursor to the bottom (last item). Returns the new cursor.
-export fn bottom(l: *listwidget) size = {
- l.cursor = len(l.items) - 1;
- l.frame.end = len(l.items): u16;
- l.frame.start = l.frame.end - l.sz.rows;
- return l.cursor;
-};
-
-// Forward search through the list's items for an item containing s. Returns the
-// new cursor.
-export fn search(l: *listwidget, s: str) size = {
- for (let i = l.cursor + 1; i < len(l.items); i += 1) {
- if (strings::contains(l.items[i], s)) {
- l.cursor = i;
- reframe(l);
- return l.cursor;
- };
- };
- return l.cursor;
-};
-
-// Backwards search through the list's items for an item containing s. Returns
-// the new cursor.
-export fn rsearch(l: *listwidget, s: str) size = {
- // size wraps to max value for size when < 0
- for (let i = l.cursor: int - 1; i >= 0; i -= 1) {
- if (strings::contains(l.items[i], s)) {
- l.cursor = i: size;
- reframe(l);
- return l.cursor;
- };
- };
- return l.cursor;
-};
-
-// Toggles marking the currently selected item.
-export fn tmark(l: *listwidget) void = {
- if (!set::add(&l.marked, l.cursor)) {
- set::del(&l.marked, l.cursor);
- };
-};
-
-// Clears all marked items.
-export fn clearmarked(l: *listwidget) void = {
- set::clear(&l.marked);
-};
-
-// Marks items that contain s (case sensitive).
-export fn containsmark(l: *listwidget, s: str) void = {
- for (let i = 0z; i < len(l.items); i += 1) {
- if (strings::contains(l.items[i], s)) {
- set::add(&l.marked, i);
- };
- };
-};
-
-// Marks items based on fnmatch (globbing syntax).
-export fn fnmatchmark(l: *listwidget, s: str) void = {
- for (let i = 0z; i < len(l.items); i += 1) {
- if (fnmatch::fnmatch(s, l.items[i])) {
- set::add(&l.marked, i);
- };
- };
-};
-
-// Marks items according to a regular expression (POSIX ERE).
-export fn regexmark(l: *listwidget, re: *regex::regex) void = {
- for (let i = 0z; i < len(l.items); i += 1) {
- if (regex::test(re, l.items[i])) {
- set::add(&l.marked, i);
- };
- };
-};
-
-// Returns the selected item or marked items if there are any.
-export fn selected(l: listwidget) (str | []str) = {
- if (len(l.marked.items) > 0) {
- let result: []str = [];
- for (let i = 0z; i < len(l.marked.items); i += 1) {
- append(result, l.items[l.marked.items[i]]);
- };
- return result;
- } else {
- return l.items[l.cursor];
- };
-};
diff --git a/libui/widget/widget.ha b/libui/widget/widget.ha
deleted file mode 100644
index 3bccd2f..0000000
--- a/libui/widget/widget.ha
+++ /dev/null
@@ -1,65 +0,0 @@
-use libui;
-use io;
-use unix::tty;
-
-export type error = !(io::error | tty::error);
-
-// A function that displays the widget.
-export type print = fn(w: *widget) (void | error);
-
-// A function that frees the resources associated to the widget.
-export type finish = fn(w: *widget) void;
-
-// An input listener on a widget. The returning value is intended to be used as
-// a signal that will be returned by [[notify]] in order to trigger certain more
-// global ui events (terminate the program, change widget focus, etc.). To
-// register a listener with a widget, use [[addlistener]].
-export type listener = *fn(w: *widget, r: rune) bool;
-
-// A widget is an abstraction around a user-defined UI component. Custom widgets
-// can be created through sub-typing:
-//
-// export type my_widget = struct {
-// widget: widget::widget,
-// lines: []str,
-// };
-//
-// fn print(w: *widget::widget) (void | widget::error) = {
-// const w = w: *my_widget;
-// for (let i = 0z; i < len(w.lines); i += 1) {
-// fmt::println(w.lines[i])!;
-// };
-// };
-//
-// fn finish(w: *widget::widget) void = {
-// const w = w: *my_widget;
-// free(w.lines);
-// widget::finishcommon(w);
-// };
-//
-// let list = my_widget {
-// widget = widget::widget {
-// print = &print,
-// finish = &finish,
-// ...
-// };
-// lines = strings::split("one,two,three", ","),
-// };
-// let l = layout::newlayout(list);
-// layout::print(l)!;
-export type widget = struct {
- print: nullable *print,
- finish: nullable *finish,
- listeners: []listener,
- ui: libui::ttyui,
-};
-
-// Add a listener to the given widget.
-export fn addlistener(w: *widget, listener: listener) void = {
- append(w.listeners, listener);
-};
-
-// Free the widget's listeners.
-export fn finishcommon(w: *widget) void = {
- free(w.listeners);
-};
diff --git a/main.ha b/main.ha
index a7396cb..fa1850b 100644
--- a/main.ha
+++ b/main.ha
@@ -1,7 +1,7 @@
-use libui;
-use libui::widget;
-use libui::widget::list;
-use libui::layout;
+use libtui;
+use libtui::widget;
+use libtui::widget::list;
+use libtui::layout;
use encoding::utf8;
use io;
use fmt;
@@ -24,7 +24,7 @@ type mainUI = struct {
fn sighandler(sig: int, info: *signal::siginfo, ucontext: *void) void = {
switch (sig) {
case signal::SIGWINCH =>
- let sz = libui::getwinsize(u.list.widget.ui)!;
+ let sz = libtui::getwinsize(u.list.widget.ui)!;
let rows: (u16 | size) = if (sz.rows - 2 < len(u.list.items)) {
yield sz.rows - 2;
} else {
@@ -37,7 +37,7 @@ fn sighandler(sig: int, info: *signal::siginfo, ucontext: *void) void = {
u.list.sz.rows = rows: u16;
u.list.sz.cols = sz.columns;
list::resize(u.list, oldsz);
- libui::clear(u.list.widget.ui);
+ libtui::clear(u.list.widget.ui);
list::print(u.list)!;
case =>
yield;
@@ -45,7 +45,7 @@ fn sighandler(sig: int, info: *signal::siginfo, ucontext: *void) void = {
};
fn resize(list: *list::listwidget) void = {
- let sz = libui::getwinsize(list.widget.ui)!;
+ let sz = libtui::getwinsize(list.widget.ui)!;
let rows: (u16 | size) = if (sz.rows - 2 < len(list.items)) {
yield sz.rows - 2;
} else {
@@ -58,7 +58,7 @@ fn resize(list: *list::listwidget) void = {
list.sz.rows = rows: u16;
list.sz.cols = sz.columns;
list::resize(list, oldsz);
- libui::clear(list.widget.ui);
+ libtui::clear(list.widget.ui);
list::print(list)!;
};
@@ -73,20 +73,20 @@ export fn main() void = {
let sin = strings::fromutf8(in);
sin = strings::trim(sin, '\n');
let items = strings::split(sin, "\n"); // freed by list::finish
- itemscopy = strings::dupall(items);
- defer strings::freeall(itemscopy);
+ //itemscopy = strings::dupall(items);
+ //defer strings::freeall(itemscopy);
- let ui = libui::init();
- defer libui::finish(&ui);
+ let ui = libtui::init();
+ defer libtui::finish(&ui);
let l = list::newlist(ui, items...);
- libui::addlistener(&ui, &globalrunehandler);
+ libtui::addlistener(&ui, &globalrunehandler);
widget::addlistener(&l, &runehandler);
let layout = layout::newlayout(&l);
defer layout::finishall(&layout);
//defer free(searchterm);
- libui::clear(l.widget.ui);
+ libtui::clear(l.widget.ui);
match (layout::print(layout)) {
case void =>
@@ -137,7 +137,7 @@ export fn main() void = {
};
};
if (readin) {
- let r = match (libui::scan(ui)) {
+ let r = match (libtui::scan(ui)) {
case let r: rune =>
yield r;
case utf8::invalid =>
@@ -150,7 +150,7 @@ export fn main() void = {
fmt::fprintln(os::stderr, io::strerror(e))!;
break;
};
- if (libui::notify(&ui, r)) {
+ if (libtui::notify(&ui, r)) {
break;
};
if (list::notify(&l, r)) {
diff --git a/set/set.ha b/set/set.ha
deleted file mode 100644
index 504d716..0000000
--- a/set/set.ha
+++ /dev/null
@@ -1,85 +0,0 @@
-export type set = struct {
- items: []size,
-};
-
-export type nosuchitem = !void;
-
-export fn add(s: *set, item: size) bool = {
- for (let i = 0z; i < len(s.items); i += 1) {
- if (s.items[i] == item) {
- return false;
- };
- };
- append(s.items, item);
- return true;
-};
-
-export fn del(s: *set, item: size) bool = {
- match (contains(*s, item)) {
- case let i: size =>
- delete(s.items[i]);
- return true;
- case nosuchitem =>
- return false;
- };
-};
-
-export fn contains(s: set, item: size) (size | nosuchitem) = {
- for (let i = 0z; i < len(s.items); i += 1) {
- if (s.items[i] == item) {
- return i;
- };
- };
- return nosuchitem;
-};
-
-// Clears all items.
-export fn clear(s: *set) void = {
- delete(s.items[..]);
-};
-
-// Free the underlying slice.
-export fn finish(s: *set) void = {
- free(s.items);
-};
-
-@test fn add() void = {
- let s = set {...};
- assert(add(&s, 1z));
- assert(len(s.items) == 1);
- assert(!add(&s, 1z));
- assert(len(s.items) == 1);
-};
-
-@test fn del() void = {
- let s = set {...};
- assert(add(&s, 1z));
- assert(len(s.items) == 1);
- assert(del(&s, 1z));
- assert(len(s.items) == 0);
- assert(!del(&s, 1z));
- assert(len(s.items) == 0);
-};
-
-@test fn contains() void = {
- let s = set {...};
- assert(add(&s, 1z));
- assert(!add(&s, 1z));
- const c = contains(s, 1z);
- assert(c is size);
- assert(c as size == 0);
- assert(add(&s, 2z));
- const c = contains(s, 2z);
- assert(c is size);
- assert(c as size == 1);
-};
-
-@test fn clear() void = {
- let s = set {...};
- assert(add(&s, 1z));
- assert(len(s.items) == 1);
- assert(!add(&s, 1z));
- assert(len(s.items) == 1);
- clear(&s);
- assert(len(s.items) == 0);
-};