term: a terminal's grid
#import "trame:term". The grid of a terminal as a widget: the cells of a screen, a selection made with the pointer, the keys
and the mouse turned into bytes, the scrollback under the toolkit's scroll bar, a zoom (a zoom zone), a cursor. It emulates
nothing: a Source gives it the screen and takes what it sends back. An emulator stands behind it (trame:vt,
libghostty-vt: vt.md), or a program's own model of a screen (a command's output, a log with colours).
v: term.View
term.init(&v, my_source(&screen), cols = 80, rows = 24) // the size until the grid is laid out
defer term.destroy(&v)
// once: the shells' zooms remembered by their id (optional; ui.Zoom_Memory, docs/zoom-zones.md)
v.zone.memory = &app.term_zooms
// each build: all the room it is given, a box with the id "###term_box", which is the zoom zone v.zone showing zoom_key
term.build(&v, style, take_focus = asked, zoom_key = shell_id)
// before the interface sees the frame's events: the keys to the view with the keyboard, the pointer to the one that
// holds a press (v.press != .None), else to the one under it (term.contains)
switch term.event(&v, e) {
case .Ignored: // the interface sees it too
case .Taken:
case .Copy: clipboard_set(term.selection_text(&v))
case .Paste: term.paste(&v, clipboard_get())
case .Menu: // the program's menu at e.pos (term.select_all, term.has_selection...)
}The source
Source is a pointer (data) and procedures. The optional ones may be nil.
| Procedure | What |
|---|---|
picture(data) -> (Picture, ok) | the screen now: dirty (None, Rows, All), the colours of a cell without its own, the cursor (shown, cell, Block / Bar / Underline, colour) |
rows(data, sink) | the rows of that picture, top to bottom: sink_row(sink, changed) for each, and when it returns true, sink_cell(sink, cell) for each of its cells; then the source takes its dirty flags as read |
scrollbar(data) -> (Scrollbar, ok) | rows: total, the first shown (offset), how many are shown (len) |
scroll(data, delta, bottom) | the rows shown move by delta (up: below 0), or to the last ones |
resize(data, cols, rows, cell, changed) | the grid laid out, at each build; changed: another size than the last one |
write(data, bytes) | optional: what is typed, pasted, or the mouse. nil: a grid to read |
modes(data) -> Modes | Bracketed_Paste, App_Cursor (read by xterm_key), Mouse_Tracking |
encode_key(data, key, buf) -> int | optional: a key's bytes. nil: xterm_key |
encode_mouse(data, mouse, buf) -> int | optional: a mouse event's bytes, with the grid's geometry (Mouse_Input). nil: the mouse is never reported |
select(data, op, cell) -> bool | optional: the selection (Begin, Extend, End, All, Drop, Has), in cells of the rows shown |
selection_text(data, allocator) -> string | the text selected |
A Cell is its grapheme (UTF-8, read during the call), fg, bg and has_bg, and flags: Bold, Faint, Inverse,
Invisible, Underline, Strike, Selected. The widget swaps an inverse cell's colours, draws a faint one at
Style.faint, a selected one in Style.selection.
The selection is the source's: an emulator knows where its ends go when rows come, when the scrollback drops its
oldest rows, when the grid is wrapped again. The widget makes the gesture: a press begins a drag on a cell (Begin), the
selection starts once the pointer leaves that cell (Extend), the release ends it (End); typing drops it.
Where the cells are
The grid holds as many whole cells as its box takes, at least Style.pad from each edge. It starts at the box's top left,
past Style.pad (grid_origin(&v, rect), kept in v.origin at each build); what the cells leave goes to the right and
under the last row. The pointer reads the cell under it from there (cell_at), and a program that tracks the mouse is
told the margin on each side (Mouse_Input.pad_lo: left and top, pad_hi: right and bottom).
When the box moves (a window or a panel resized), the grid stays anchored at that corner and keeps its columns and rows
until the box has rested for REST_MS: then it takes the box's size once, and the source is told it once
(resize with changed). A larger box shows a wider band meanwhile, a smaller one clips. The whole box, the band
included, is painted with the screen's background (Picture.background, which the program in the shell may set):
the band looks like more of the screen. Style.background is painted only until the first picture. The first size is at once. Why:
src/md/features/term-resize.md.
The rows drawn
The widget keeps each row as it draws it, in columns: runs of text of one look (Run), background rectangles (Fill),
underlines and lines through (Mark). refresh (the draw calls it) takes the picture and builds again only the rows the
source says changed: a frame where nothing changed reads no cell and builds no row. A new zoom or face moves the rows
without building them. recolour(&v) (a new theme: the source's colours moved) builds every row at the next draw.
v.hold = true keeps the picture drawn last (a program that awaits a whole new screen after a resize).
rows_built counts the rows built, and row_text(&v, y) gives a row's text as drawn: what a test reads.
Each row is drawn as a retained picture (draw.Picture) keyed by its content (Row.sum) and its look (the faces, their
size, the cell, the selection's colour, where the grid starts within a pixel). A frame at rest replays every row; a line
printed records only the new row, as the rows moved up find the pictures their content had a line lower. A picture no
row showed in a draw is released; destroy frees them all. rows_recorded counts the rows recorded: what a test
reads. A grid moved to another window replays its pictures there (they upload in the new draw context). The intent:
src/md/features/term-rows.md.
Input
- Keys: a printable key alone gives nothing, its
Textevent types the character; Ctrl with Alt is AltGr. Ctrl+Shift+C and Ctrl+Insert copy (Ctrl+C alone where nothing is typed,read_only), Ctrl+Shift+V and Shift+Insert paste, Shift+Page Up and Page Down scroll a page. The zoom keys (ui.ZOOM_IN_KEYS,ZOOM_OUT_KEYS,ZOOM_RESET_KEYS) are never the shell's:eventleaves them to the interface (.Ignored), which zooms the grid's zone,v.zone(the Feel'szoom_step, betweenZOOM_MINandZOOM_MAX; Ctrl+0 forgets the shell's zoom).zoom_set(&v, zoom)sets it from the program (a menu). What is typed goes to the last rows and drops the selection. paste_textputs a paste in brackets when the program asks for them; else its line ends are returns.- The pointer: a press on the toolkit's scroll bar is the interface's (
on_bar). A program that tracks the mouse gets the presses, the moves and the wheel (Shift: the pointer selects, and a right click opens the menu); the release of a press it was told of comes wherever the button is let go, or when the press is taken away (press_end). - The wheel scrolls the rows shown by the lines the system says; Ctrl and the wheel are the interface's, which zooms
the zone under the pointer (
.Ignored). - The grid keeps its size while the zoom moves and takes its new number of columns once the zoom has rested for
REST_MS, as for a box that moves: each new number wraps every row of the scrollback again.buildasks for that frame itself.
Tests
tests/term drives the widget on a screen of its own, without a window or a font: the row cache, the selection, the
keys, the paste keys, the press held and let go, the zoom keys left to the interface. In a session (an offscreen window),
the rows as pictures: one line printed records one row, a draw at rest none. -define:TERM_TIMING=true (with -o:speed)
times the box's draw of 40 rows of 120 cells, at rest and while lines are printed (tests/term/bench_test.fm).