Trame

terminal: a local terminal

#import "trame:terminal". A shell or a command on this machine, in a pseudo-terminal (pty.md), emulated by Ghostty (vt.md), shown in the grid (term.md): one call to start it, one per build to show it, one per event. The design: src/md/features/local-terminal.md.

Formula
t := terminal.start(&app.ui, {
	command = {"make", "test"},    // nil: the user's shell
	dir     = project,
	user    = &app,
	on_exit = proc(user: rawptr, code: int) { ... },   // the child ended, once
})

// each build: the grid, all the room it is given ("###term_box")
terminal.build(t, style, take_focus = asked, zoom_key = id)

// before the interface sees the frame's events (term.md says which go to which grid)
switch terminal.event(t, e) {
case .Copy:  clipboard_set(term.selection_text(terminal.view(t)))
case .Paste: terminal.paste(t, clipboard_get())
case .Menu, .Taken, .Ignored:
}

terminal.destroy(t)   // out of a build: returns at once, the child and the threads end on their own

The program declares toolbox.Feature.Vt (vt.md). Without it, start gives a terminal that is Failed at once; the program compiles and links nothing of Ghostty.

The API

Procedure
start(s, options) -> ^Terminalon the interface's thread of s: returns at once, Starting; the child starts on a chore
build(t, style, take_focus, zoom_key) -> (box, signal)the grid (term.build). What the child wrote is fed before the build, when the posts run
event(t, e) -> term.Outcomekeys and the pointer to the grid (term.event)
write(t, bytes)bytes for the child, as they are
paste(t, text)a paste: in brackets when the child asks for them
stop(t)ends the child and what it started; the grid stays, to read. Exited follows
destroy(t)frees the terminal at once; its child ends, its threads end and free what they hold on their own
state(t), exit_code(t), failure(t), pid(t)Starting, Running, Exited (its code: the exit code, or 128 + the signal) or Failed (why); the child's process id once Running
title(t)the title the child set last
text(t, allocator)the screen and the scrollback as the child wrote them (a wrapped line is one line): what a program searches (a file:line:col)
row_text(t, y, allocator)row y of the rows shown, as drawn: what is under the pointer
view(t) -> ^term.Viewthe grid, for what the package does not wrap (select_all, zoom_set, selection_text, the scroll bar)
recolour(t, colours)a new theme
live() -> intterminals not yet ended: a child, a thread or a post still holds them. A test waits until 0
Options
commandthe program and its arguments. nil: the user's shell ($SHELL, else /bin/sh; Windows %COMSPEC%, else cmd.exe)
dirits working folder ("": this process's)
envNAME=value on top of this process's environment
colours, scrollbackthe emulation's (vt.Colours; 0: vt.SCROLLBACK_DEFAULT)
cols, rowsthe size until the grid is laid out (0: 80 by 24)
user, on_title, on_exit, on_failthe callbacks, on the interface's thread. on_exit and on_fail come once

A program's terminals: the group

A program with several terminals, in one window or several, gives each window's events to its Group before the interface sees them. The keys go to the grid with the keyboard, the pointer to the grid under it, and the moves and the release of a press to the grid that took it, wherever the pointer goes. A grid not built in its window's last build (its view closed, under another tab) has neither. Copy and paste are done by the group; a right click's menu goes to on_menu.

Formula
group: terminal.Group                       // the program's, with on_menu and user if it has a menu
terminal.group_add(&group, t)               // after start; destroy takes it out

// each window's input hook, before the interface:
events = terminal.group_events(&group, &win.state, events)
// each window's build:
terminal.group_begin(&group, &win.state); ...terminal.build(t, ...)...; terminal.group_end(&group, &win.state)

terminal.group_destroy(&group)              // destroys the terminals still in it

The child's environment

This process's environment, without its TERM and COLORTERM (they describe the terminal this process runs in), then TERM=xterm-256color and COLORTERM=truecolor (Ghostty answers as an xterm with true colours), then env on top. A TERM in env wins.

Where it runs

Nothing waits on the interface's thread: start hands the child's start to a chore (ui.task), which starts three threads for it: a reader (pty.read), a writer (what is typed, from a queue the interface fills), and a keeper (the size, stop, the end). The reader's bytes wait in a bounded inbox (INBOX_MOST, 1 MiB) until a frame feeds them: one post at a time wakes the window. A child that writes without end waits for the frames, and fills no memory.

The size goes to the child once the grid rests (term.REST_MS after the box's last move): a box dragged through thirty sizes tells it one.

A post can come after destroy, or after ui.destroy: it goes through a counted link (ui.task_link) and finds the terminal gone. Destroy the terminals before the State all the same: destroy reads nothing of the State, but its grid's box does until the next build.

Tests

tests/terminal (Linux, ./build/run_tests terminal, after trame build_native -features:text,vt): a command's output and its code, a shell typed into, stty size that answers the grid's size, a drag of thirty sizes that tells the child one, destroy while yes writes and before the start answered (its threads end, its process is gone), the child's TERM, a command that does not exist.

Edit this page on GitHub