diff options
Diffstat (limited to 'libtui/widget')
| -rw-r--r-- | libtui/widget/README | 2 | ||||
| -rw-r--r-- | libtui/widget/ed/ed.ha | 312 | ||||
| -rw-r--r-- | libtui/widget/list/README | 1 | ||||
| -rw-r--r-- | libtui/widget/list/list.ha | 354 | ||||
| -rw-r--r-- | libtui/widget/widget.ha | 89 |
5 files changed, 758 insertions, 0 deletions
diff --git a/libtui/widget/README b/libtui/widget/README new file mode 100644 index 0000000..ae62628 --- /dev/null +++ b/libtui/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/libtui/widget/ed/ed.ha b/libtui/widget/ed/ed.ha new file mode 100644 index 0000000..64157cd --- /dev/null +++ b/libtui/widget/ed/ed.ha @@ -0,0 +1,312 @@ +// License: MPL-2.0 +// (c) 2022 Julian Hurst <ark@mansus.space> + +use libtui; +use libtui::widget; +use strio; +use strconv; +use strings; +use wcwidth; +use set; +use io; +use fmt; +use os; +use encoding::utf8; + +let linestofree: set::set = set::set {...}; + +export type editorwidget = struct { + widget: widget::widget, + lines: []str, + cursor: (size, size), + marked: set::set, + frame: frame, + sz: ttysize, + mode: mode, +}; + +export type frame = struct { + start: u16, + // largest value is nb of lines + end: u16, +}; + +export type mode = enum { + NORMAL, + INSERT, +}; + +export type ttysize = struct { + rows: u16, + cols: u16, +}; + +// An input listener on an editor 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 an editorwidget, use [[addlistener]]. +export type listener = *fn(l: *editorwidget, r: (rune | libtui::specialkey)) bool; + +// Create a new editor with the given lines. +export fn neweditor(ui: libtui::ttyui, lines: str...) editorwidget = { + let sz = libtui::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 = editorwidget { + widget = widget::widget { + print = &print, + finish = &finish, + ui = ui, + ... + }, + lines = lines, + marked = set::set {...}, + cursor = (0z, 0z), + frame = frame { + start = 0u16, + end = rows: u16, + }, + sz = ttysize { + rows = rows: u16, + cols = sz.columns, + }, + mode = mode::INSERT, + }; + return w; +}; + +// Notify (call) the editorwidget's editoreners with the editorwidget and r as a +// parameter. Returns true if a editorener returned true, false otherwise. +//export fn notify(l: *editorwidget, 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; +//}; + +//const SELECTED: str = "\x1B[104;1m\x1B[30m"; +const SELECTED: str = "\x1B[7m"; +const MARKED: str = "\x1B[46;1m\x1B[30m"; +const RESET: str = "\x1B[0m"; + +// Print the editor's lines while truncating the lines to not be wider than the +// editor.sz.cols. +export fn print(editor: *widget::widget) (void | widget::error) = { + const editor = editor: *editorwidget; + //let sz = libtui::getwinsize(editor.ui)?; + //let rows: (u16 | size) = if (sz.rows - 2 < len(editor.lines)) { + //yield sz.rows - 2; + //} else { + //yield len(editor.lines); + //}; + + //fmt::fprintln(os::stderr, rows)!; + + //editor.frame.end = editor.frame.start + editor.sz.rows; + + let st = strio::dynamic(); + strio::concat(&st, "\r")?; + let end = if (editor.frame.end < len(editor.lines)) { + yield editor.frame.end; + } else { + yield len(editor.lines); + }; + const maxlinenosz = len(strconv::ztos(len(editor.lines))); + for (let i = editor.frame.start; i < end: u16; i += 1) { + let lineno = strconv::ztos(i+1); + lineno = strings::padstart(lineno, ' ', maxlinenosz); + const line = strings::concat(lineno, "| ", editor.lines[i]); + const truncitem = wcwidth::truncate(line, editor.sz.cols); + defer free(truncitem); + if (editor.cursor.0 == i) { + //strio::concat(&st, SELECTED, truncitem, RESET)?; + + const linepos = editor.cursor.1 + maxlinenosz + 2; + strio::concat(&st,strings::sub(truncitem, 0, linepos))?; + strio::concat(&st, SELECTED, strings::sub(truncitem, linepos, linepos+1), RESET)?; + strio::concat(&st, strings::sub(truncitem, linepos+1, strings::end))?; + + //libtui::print(editor.ui, strings::concat("\x1B[31;1m> ", editor.lines[i], "\x1B[0m")); + } else if (set::contains(editor.marked, i) is size){ + strio::concat(&st, MARKED, truncitem, RESET)?; + //libtui::print(editor.ui, editor.lines[i]); + } else { + strio::concat(&st, truncitem)?; + }; + strio::concat(&st, "\r\n")?; + }; + // unsupported? + //io::copy(editor.ui.f, &st)?; + let s = strio::string(&st); + libtui::print(editor.widget.ui, s); + io::close(&st)?; +}; + +// Free the editor's lines, marked lines and call the common widget finish +// function [[widget::finishcommon]]. +export fn finish(editor: *widget::widget) void = { + const editor = editor: *editorwidget; + for (let i = 0z; i < len(linestofree.items); i += 1) { + free(editor.lines[linestofree.items[i]]); + }; + free(editor.lines); + //strings::freeall(editor.lines); + set::finish(&editor.marked); + set::finish(&linestofree); + widget::finishcommon(editor); +}; + +// Reset the editor's frame based on the cursor. Returns whether the frame was +// updated. +export fn reframe(l: *editorwidget) bool = { + let reframed: bool = false; + if (l.cursor.0 < l.frame.start) { + l.frame.start = l.cursor.0: u16; + l.frame.end = l.frame.start + l.sz.rows; + //l.frame.end -= l.frame.start - l.cursor: u16; + reframed = true; + }; + if (l.cursor.0 >= l.frame.end) { + l.frame.start += l.cursor.0: u16 - l.frame.end + 1; + l.frame.end = l.cursor.0: u16 + 1; + reframed = true; + }; + return reframed; +}; + +// Move the editor's cursor up one line. Returns the new cursor. +export fn up(l: *editorwidget) (size, size) = { + if (l.cursor.0 > 0) { + l.cursor.0 -= 1; + reframe(l); + resetcursorlinepos(l); + }; + return l.cursor; +}; + +// Move the editor's cursor down one line. Returns the new cursor. +export fn down(l: *editorwidget) (size, size) = { + if (l.cursor.0 < len(l.lines) - 1) { + l.cursor.0 += 1; + reframe(l); + resetcursorlinepos(l); + }; + return l.cursor; +}; + +fn resetcursorlinepos(l: *editorwidget) (size, size) = { + const line = l.lines[l.cursor.0]; + if (l.cursor.1 >= len(line)) { + l.cursor.1 = if (len(line) > 0) { + yield len(line) - 1; + } else { + yield firstcharindex(line); + }; + } else { + const firstcharidx = firstcharindex(line); + if (firstcharidx > l.cursor.1) { + l.cursor.1 = firstcharidx; + }; + }; + return l.cursor; +}; + +// Move the editor's cursor to the right one character. Returns the new cursor. +export fn right(l: *editorwidget) (size, size) = { + if (l.cursor.1 < len(l.lines[l.cursor.0]) - 1) { + l.cursor.1 += 1; + }; + return l.cursor; +}; + +// Move the editor's cursor to the left one character. Returns the new cursor. +export fn left(l: *editorwidget) (size, size) = { + if (l.cursor.1 > 0) { + l.cursor.1 -= 1; + }; + return l.cursor; +}; + +export fn insertmode(l: *editorwidget, lt: widget::listener) void = { + l.mode = mode::INSERT; + widget::clearlisteners(l); + widget::addlistener(l, lt); +}; + +export fn normalmode(l: *editorwidget, lt: widget::listener) void = { + l.mode = mode::NORMAL; + widget::clearlisteners(l); + widget::addlistener(l, lt); +}; + +export fn insertrune(l: *editorwidget, r: rune) (void | io::error) = { + //match (sanitizerune(r)) { + //case let k: libtui::keycode => + //specialkey(l, k)?; + //case let r: rune => + let line = l.lines[l.cursor.0]; + let st = strio::dynamic(); + strio::concat(&st, strings::sub(line, 0, l.cursor.1))?; + strio::appendrune(&st, r)?; + strio::concat(&st, strings::sub(line, l.cursor.1, strings::end))?; + l.lines[l.cursor.0] = strings::dup(strio::string(&st)); + right(l); + set::add(&linestofree, l.cursor.0); + io::close(&st)?; + //}; +}; + +fn specialkey(l: *editorwidget, keycode: libtui::keycode) (void | io::error) = { + switch (keycode) { + case libtui::keycode::BACKSPACE => + if (l.cursor.1 > 0) { + let line = l.lines[l.cursor.0]; + let st = strio::dynamic(); + strio::concat(&st, strings::sub(line, 0, l.cursor.1-1))?; + if (len(line) > l.cursor.1) { + strio::concat(&st, strings::sub(line, l.cursor.1, strings::end))?; + }; + l.lines[l.cursor.0] = strings::dup(strio::string(&st)); + left(l); + set::add(&linestofree, l.cursor.0); + io::close(&st)?; + }; + case libtui::keycode::RIGHT => + right(l); + //fmt::fprintln(os::stderr, "poopy")!; + }; +}; + +// Doesn't work, RIGHT ends up being 3 separate runes not one with 3 bytes... +//fn sanitizerune(r: rune) (rune | libtui::keycode) = { + //if (libtui::iskey(r, libtui::BACKSPACE)) { + //return libtui::keycode::BACKSPACE; + //}; + //if (libtui::iskey(r, libtui::RIGHT)) { + //return libtui::keycode::RIGHT; + //}; + //return r; +//}; + +fn firstcharindex(s: str) size = { + let it = strings::iter(s); + let i = 0z; + for (true) { + match (strings::next(&it)) { + case let r: rune => + if (r != ' ' && r != '\t') { + return i; + }; + case void => + return 0z; + }; + i += 1; + }; + return 0z; +}; diff --git a/libtui/widget/list/README b/libtui/widget/list/README new file mode 100644 index 0000000..c8f71db --- /dev/null +++ b/libtui/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/libtui/widget/list/list.ha b/libtui/widget/list/list.ha new file mode 100644 index 0000000..1f8a0fb --- /dev/null +++ b/libtui/widget/list/list.ha @@ -0,0 +1,354 @@ +// License: MPL-2.0 +// (c) 2022 Julian Hurst <ark@mansus.space> + +use libtui; +use libtui::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 | libtui::specialkey)) bool; + +// Create a new list with the given items. The given items will be duplicated +// using [[strings::dupall]]], so the original items can be freed. +export fn newlist(ui: libtui::ttyui, items: str...) listwidget = { + let sz = libtui::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 = strings::dupall(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; + strings::freeall(list.items); + set::finish(&list.marked); + widget::finishcommon(list); +}; + +// Set the list's items. The given items are duplicated via [[strings::dupall]]. +// 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 = libtui::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)?; + //libtui::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)?; + //libtui::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); + libtui::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 | libtui::specialkey)) 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; +}; + +// Use signalfd and [[unix::signal::SIGWINCH]] and call this function in the +// handler to support resizing the list. +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)) { + 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) { + l.frame.start += 1; + l.frame.end += 1; + }; + } else { + 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) { + 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/libtui/widget/widget.ha b/libtui/widget/widget.ha new file mode 100644 index 0000000..6875692 --- /dev/null +++ b/libtui/widget/widget.ha @@ -0,0 +1,89 @@ +// License: MPL-2.0 +// (c) 2022 Julian Hurst <ark@mansus.space> + +use libtui; +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: libtui::key) 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: libtui::ttyui, +}; + +// Add a listener to the given widget. +export fn addlistener(w: *widget, listener: listener) void = { + append(w.listeners, listener); +}; + +// Delete a listener from the given widget. +export fn dellistener(w: *widget, idx: size) void = { + delete(w.listeners[idx]); +}; + +// Clear the listeners of the given widget. +export fn clearlisteners(w: *widget) void = { + w.listeners = []; +}; + +// Free the widget's listeners. +export fn finishcommon(w: *widget) void = { + free(w.listeners); +}; + +// Notify (call) the widget's listeners with the widget and r as a parameter. +// Returns true if a listener returned true, false otherwise. +export fn notify(w: *widget, r: (rune | libtui::specialkey)) bool = { + for (let i = 0z; i < len(w.listeners); i += 1) { + if (w.listeners[i](w, r)) { + return true; + }; + }; + return false; +}; |
