From 9740eee555cac43cc27e08a39a38e09a96ecb002 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Mon, 16 May 2022 00:35:21 +0200 Subject: Add layout and a common widget type --- libui/README | 1 + libui/layout/layout.ha | 42 ++++++++ libui/libui.ha | 6 +- libui/list/list.ha | 245 ------------------------------------------ libui/widget/README | 2 + libui/widget/list/README | 1 + libui/widget/list/list.ha | 268 ++++++++++++++++++++++++++++++++++++++++++++++ libui/widget/widget.ha | 63 +++++++++++ main.ha | 34 ++++-- set/set.ha | 7 +- 10 files changed, 414 insertions(+), 255 deletions(-) create mode 100644 libui/README create mode 100644 libui/layout/layout.ha delete mode 100644 libui/list/list.ha create mode 100644 libui/widget/README create mode 100644 libui/widget/list/README create mode 100644 libui/widget/list/list.ha create mode 100644 libui/widget/widget.ha diff --git a/libui/README b/libui/README new file mode 100644 index 0000000..df8992f --- /dev/null +++ b/libui/README @@ -0,0 +1 @@ +The libui module provides widgets for creating a tui (terminal user interface). diff --git a/libui/layout/layout.ha b/libui/layout/layout.ha new file mode 100644 index 0000000..ef6bf71 --- /dev/null +++ b/libui/layout/layout.ha @@ -0,0 +1,42 @@ +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) = { + 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 index 6ad33d1..7e40a9d 100644 --- a/libui/libui.ha +++ b/libui/libui.ha @@ -26,7 +26,7 @@ export fn init() ttyui = { fmt::fatal(tty::strerror(e)); }; if (!tty::isatty(f)) { - fmt::fatal("/dev/tty is not a tty"); + fmt::fatal("stream is not a tty"); }; let term = match (tty::termios_query(f)) { case let t: tty::termios => @@ -74,6 +74,7 @@ export fn scan(ui: ttyui) (rune | utf8::invalid | io::EOF | io::error) = { }; // 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)) { @@ -110,7 +111,8 @@ export fn addlistener(ui: *ttyui, l: listener) void = { // Print a string or rune to the ttyui. export fn print(ui: ttyui, arg: (str | rune)) void = { - fmt::fprintf(ui.f, "{}\r", arg)!; + fmt::fprint(ui.f, arg)!; + //fmt::fprintf(ui.f, "{}\r", arg)!; }; // Clear the ttyui. diff --git a/libui/list/list.ha b/libui/list/list.ha deleted file mode 100644 index 7b181c4..0000000 --- a/libui/list/list.ha +++ /dev/null @@ -1,245 +0,0 @@ -use libui; -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 { - ui: libui::ttyui, - 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, -}; - -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: (u16 | size) = if (sz.rows - 2 < len(items)) { - yield sz.rows - 2; - } else { - yield len(items); - }; - let w = listwidget { - 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; -}; - -// Add a listener to the given list. -export fn addlistener(list: *listwidget, l: listener) void = { - append(list.listeners, l); -}; - -// Print the list's items while truncating the items to not be wider than the -// list.sz.cols. -export fn print(list: *listwidget) (void | io::error | tty::error) = { - //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")?; - for (let i = list.frame.start; i < list.frame.end: u16; i += 1) { - let item = list.items[i]; - let truncitem = wcwidth::truncate(item, list.sz.cols); - if (list.cursor == i) { - strio::concat(&st, "\x1B[104;1m\x1B[30m")?; - strio::concat(&st, truncitem)?; - strio::concat(&st, "\x1B[0m")?; - //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, "\x1B[46;1m\x1B[30m")?; - strio::concat(&st, truncitem)?; - strio::concat(&st, "\x1B[0m")?; - //libui::print(list.ui, list.items[i]); - } else { - strio::concat(&st, truncitem)?; - }; - strio::concat(&st, "\r\n")?; - }; - let s = strio::string(&st); - defer free(s); - libui::print(list.ui, s); -}; - -// Notify (call) the listwidget's listeners with the listwidget and r as a -// parameter. -export fn notify(l: *listwidget, r: rune) bool = { - for (let i = 0z; i < len(l.listeners); i += 1) { - if (l.listeners[i](l, r)) { - return true; - }; - }; - return false; -}; - -// 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; -}; - -// Reset the list's frame based on the cursor. Returns whether the frame was -// updated. -fn reframe(l: *listwidget) bool = { - let reframed: bool = false; - if (l.cursor < l.frame.start) { - l.frame.end -= l.frame.start - l.cursor: u16; - 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; - reframed = true; - }; - return reframed; -}; - -// 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). -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/README b/libui/widget/README new file mode 100644 index 0000000..ae62628 --- /dev/null +++ b/libui/widget/README @@ -0,0 +1,2 @@ +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 new file mode 100644 index 0000000..c8f71db --- /dev/null +++ b/libui/widget/list/README @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000..57239f3 --- /dev/null +++ b/libui/widget/list/list.ha @@ -0,0 +1,268 @@ +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, + ui: libui::ttyui, + 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: (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); +}; + +// Add a listener to the given list. +//export fn addlistener(list: *listwidget, l: listener) void = { + //append(list.listeners, l); +//}; + +// 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")?; + for (let i = list.frame.start; i < list.frame.end: u16; i += 1) { + let item = list.items[i]; + let truncitem = wcwidth::truncate(item, list.sz.cols); + if (list.cursor == i) { + strio::concat(&st, "\x1B[104;1m\x1B[30m")?; + strio::concat(&st, truncitem)?; + strio::concat(&st, "\x1B[0m")?; + //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, "\x1B[46;1m\x1B[30m")?; + strio::concat(&st, truncitem)?; + strio::concat(&st, "\x1B[0m")?; + //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.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; +}; + +// 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; +}; + +// Reset the list's frame based on the cursor. Returns whether the frame was +// updated. +fn reframe(l: *listwidget) bool = { + let reframed: bool = false; + if (l.cursor < l.frame.start) { + l.frame.end -= l.frame.start - l.cursor: u16; + 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; + reframed = true; + }; + return reframed; +}; + +// 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 new file mode 100644 index 0000000..1f23aac --- /dev/null +++ b/libui/widget/widget.ha @@ -0,0 +1,63 @@ +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, +}; + +// 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 2f96e56..0aeda39 100644 --- a/main.ha +++ b/main.ha @@ -1,5 +1,7 @@ use libui; -use libui::list; +use libui::widget; +use libui::widget::list; +use libui::layout; use encoding::utf8; use io; use fmt; @@ -26,7 +28,8 @@ fn globalrunehandler(ui: *libui::ttyui, r: rune) bool = { return false; }; -fn runehandler(l: *list::listwidget, r: rune) bool = { +fn runehandler(l: *widget::widget, r: rune) bool = { + const l = l: *list::listwidget; switch (r) { case 'j' => list::down(l); @@ -40,6 +43,7 @@ fn runehandler(l: *list::listwidget, r: rune) bool = { case let s: str => fmt::println(s)!; case let s: []str => + defer free(s); const out = strings::join("\n", s...); defer free(out); fmt::println(out)!; @@ -192,7 +196,6 @@ fn runehandler(l: *list::listwidget, r: rune) bool = { fn sighandler(sig: int, info: *signal::siginfo, ucontext: *void) void = { switch (sig) { case signal::SIGWINCH => - fmt::fprintln(os::stderr, "winch")!; let sz = libui::getwinsize(u.list.ui)!; let rows: (u16 | size) = if (sz.rows - 2 < len(u.list.items)) { yield sz.rows - 2; @@ -215,16 +218,21 @@ export fn main() void = { let sin = strings::fromutf8(in); sin = strings::trim(sin, '\n'); let items = strings::split(sin, "\n"); - defer free(items); let ui = libui::init(); defer libui::finish(&ui); let l = list::newlist(ui, items...); libui::addlistener(&ui, &globalrunehandler); - list::addlistener(&l, &runehandler); + widget::addlistener(&l, &runehandler); + + let layout = layout::newlayout(&l); + + defer layout::finishall(&layout); + //defer free(searchterm); libui::clear(l.ui); - match (list::print(&l)) { + + match (layout::print(layout)) { case void => yield; case let e: io::error => @@ -234,10 +242,22 @@ export fn main() void = { fmt::fprintln(os::stderr, tty::strerror(e))!; return; }; + + //match (list::print(&l)) { + //case void => + //yield; + //case let e: io::error => + //fmt::fprintln(os::stderr, io::strerror(e))!; + //return; + //case let e: tty::error => + //fmt::fprintln(os::stderr, tty::strerror(e))!; + //return; + //}; u = mainUI { list = &l, }; - signal::handle(signal::SIGWINCH, &sighandler, signal::flag::RESTART); + const sigs = signal::handle(signal::SIGWINCH, &sighandler, signal::flag::RESTART); + defer signal::restore(signal::SIGWINCH, &sigs); for (true) { let r = match (libui::scan(ui)) { case let r: rune => diff --git a/set/set.ha b/set/set.ha index 2c56efe..504d716 100644 --- a/set/set.ha +++ b/set/set.ha @@ -33,11 +33,16 @@ export fn contains(s: set, item: size) (size | nosuchitem) = { return nosuchitem; }; -// Clears all items +// 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)); -- cgit v1.2.3