Immediate-mode terminal UI library for MoonBit native targets, built on moonbit-community/tty.
Early work in progress. The API is small and will change.
///|
struct State {
mut count : Int32
}
///|
fn view(state : State, size : @tui.Size) -> @tui.Frame {
let frame = @tui.Frame(width=size.width, height=size.height)
let dim = @tui.Style(fg=Basic(BrightBlack))
// Two rows at the top; the rest of the screen stays blank.
let rows = @tui.Layout::vertical([Length(1), Length(1), Fill(1)])
guard! rows.split(frame.area()) is [count, help, _]
frame.line(count, @tui.Line([Span("count: "), Span("\{state.count}")]))
frame.line(help, @tui.Line::from_text("space: +1 · Esc: quit", style=dim))
frame
}
///|
async fn main {
@tui.with_terminal(terminal => {
let state = State::{ count: 0, }
for ;; {
terminal.draw(view(state, terminal.size()))
match terminal.read_event() {
Input(Key(key)) =>
match key.code {
Escape => break
Char(' ') => state.count += 1
_ => ()
}
// A resize needs nothing special: the next turn paints a frame of
// the new size.
_ => ()
}
}
})
}let rows = @tui.Layout::vertical([Length(1), Fill(1), Length(1)])
guard! rows.split(frame.area()) is [header, body, footer]
let columns = @tui.Layout::horizontal([Fill(1), Length(8)], spacing=1)
guard! columns.split(header) is [title, clock]| Constraint | Size |
|---|---|
| Length(n) | exactly n |
| Percentage(p) | p percent of the space |
| Ratio(a, b) | a / b of the space |
| Min(n) | at least n; grows when no Fill claims the leftover space |
| Max(n) | at most n; takes n when there is room |
| Fill(w) | a share of the leftover space, in proportion to its weight w |
let rows = @tui.Layout::vertical([Length(5)], flex=Center)
guard! rows.split(frame.area()) is [band]
let columns = @tui.Layout::horizontal([Length(40)], flex=Center)
guard! columns.split(band) is [dialog]let prompt = @tui.Line([Span("> ", style=dim), Span(input)])
frame.line(area, prompt) // clipped to the area
frame.line(area, status, align=Right)
frame.lines(body, long_line.wrap(body.width))
frame.set_cursor(area, col=prompt.width()) // relative to the areastruct State {
log : Array[String]
scroll : @tui.Scroll // Scroll(stick_to_bottom=true) for a chat or a log
}
// In view: wrap to the area's width, then draw through the scroll position.
frame.lines(body, lines, scroll=state.scroll)
// In the event loop:
PageUp => state.scroll.page_up()
PageDown => state.scroll.page_down()
Input(Mouse({ kind: Scroll(Up), .. })) => state.scroll.scroll_by(-3)// In view:
frame.lines(body, lines, scroll=state.scroll, selection=state.selection)
// In the event loop:
Input(Mouse(mouse)) => {
state.selection.mouse(mouse)
if mouse.kind is Release(Left) && state.selection.text() is Some(text) {
terminal.set_clipboard(text)
}
}// In view. The input grows with its text, so measure it before splitting.
let rows = state.editor.height(width=size.width - 2, max=4)
let layout = @tui.Layout::vertical([Fill(1), Length(rows)])
guard! layout.split(frame.area()) is [body, input]
let layout = @tui.Layout::horizontal([Length(2), Fill(1)])
guard! layout.split(input) is [marker, text]
frame.line(marker, @tui.Line::from_text("> "))
state.editor.draw(frame, text)
// In the event loop.
Input(Key(key)) =>
if !state.editor.key(key) && key.code is Enter {
submit(state.editor.text())
state.editor.clear()
}
Input(Paste(text)) => state.editor.insert(text)state.editor.draw(
frame,
body,
style=text_style, // the text, and the area behind it
highlight=highlight_moonbit, // (String) -> Array[Span], one line at a time
line_numbers=line_number_style, // a number column on the left
current_line=current_line_style, // laid over the cursor's line
)moon run examples/counter # the snippet above
moon run examples/hello # an input box, scrolling, selection and copy
moon run examples/text_editor -- frame.mbt # a code editor: highlighting, line numbers, mousemoon fmt
moon check --deny-warn
moon test
moon infotype Frametype Layoutfn Layout::horizontal(constraints : Array[Constraint], spacing? : Int, margin? : Int, flex? : Flex) -> Layoutguard! Layout::vertical([Length(1), Fill(1), Length(1)]).split(area)
is [header, body, footer]fn Layout::vertical(constraints : Array[Constraint], spacing? : Int, margin? : Int, flex? : Flex) -> Layouttype Scrolltype Selectiontype TerminalInstall
Download zipImmediate-mode terminal UI library for MoonBit native targets, built on moonbit-community/tty.