screen API reference
Terminal user interfaces: an explicit screen, not a GUI framework. Two layers. The output-only layer is pure strings over ANSI control sequences - a cell Buffer you draw into (text / box / fill) and paint with render, plus diff for a flicker-free update loop (dashboards, progress, self-updating tables); it needs no host capability and runs on both binaries. The interactive layer decodes a raw byte stream into Key events (decodeKey, pure and testable) and drives an event loop over the term library's raw mode (begin / nextKey / end) for menus, forms, and key navigation; that layer needs term, so it works on the default jennifer (a friendly error on jennifer-tiny, which stubs term).
Coordinates are 0-based with the origin at the top-left (0, 0); x is the column, y the row. Drawing that runs past an edge is clipped, not an error.
Import with import "screen.j" as screen;. See the screen guide for prose and examples.
Contents
| Name | What it is |
|---|---|
begin | Enter full-screen interactive mode: put the terminal in raw mode and switch to the alternate screen with th... |
box | A copy of buf with a single-line box border drawn at (x, y) of outer size w by h, using Unicode box... |
clear | The sequence that clears the whole screen and homes the cursor. |
clearBuffer | A copy of buf with every cell reset to a space. |
clearLine | The sequence that clears the current line. |
decodeKey | Decode one raw key byte sequence into a Key. |
diff | The minimal escape string that turns the terminal showing old into new - only the changed cells are rep... |
down | The sequence that moves the cursor down n rows. |
end | Leave interactive mode: show the cursor, leave the alternate screen, and restore the terminal from the hand... |
enterAlt | The sequence that switches to the alternate screen buffer (so the app's output does not scroll the user's s... |
exitAlt | The sequence that leaves the alternate screen buffer, restoring the prior terminal contents. |
fill | A copy of buf with the rectangle at (x, y) of size w by h filled with the glyph ch. |
get | The contents of the cell at (x, y), or an empty string if out of range. |
hasKey | Whether a decoded key is waiting (non-blocking): true when pollKey would return a real key rather than th... |
hideCursor | The sequence that hides the text cursor. |
hline | A copy of buf with a horizontal line of n cells of ch from (x, y). |
home | The sequence that homes the cursor to the top-left. |
left | The sequence that moves the cursor left n columns. |
moveTo | The sequence that moves the cursor to (x, y) (0-based; origin top-left). |
newScreen | A new blank Buffer of rows by cols cells, every cell a space. |
nextKey | Read and decode the next key from the terminal (blocking). |
pollKey | The next decoded key if one is waiting, else a "none" sentinel key (non-blocking) - the poll a free-runni... |
render | The escape string that paints the whole buf to the terminal (positions the cursor at the start of each ro... |
right | The sequence that moves the cursor right n columns. |
set | A copy of buf with the single cell at (x, y) set to cell (one column). |
setMany | A copy of buf with every plot applied in order, as a run of set calls would. |
showCursor | The sequence that shows the text cursor. |
size | The terminal size as a term.Size ({rows, cols}), a convenience passthrough to term.size so an app sta... |
startInput | Start a background keyboard reader for a free-running loop (a game, a live dashboard, anything that must ke... |
text | A copy of buf with s written left-to-right starting at (x, y), one rune per cell, clipped at the row'... |
textColor | A copy of buf with s written from (x, y) in the named foreground colour, one rune per cell, clipped a... |
textMany | A copy of buf with every span written in order, as a run of text / textColor calls would. |
up | The sequence that moves the cursor up n rows. |
vline | A copy of buf with a vertical line of n cells of ch from (x, y). |
waitKey | Block until the next decoded key and return it - the buffered equivalent of nextKey, for a loop that wait... |
Buffer | struct: A rectangular grid of character cells, drawn into then painted to the terminal. |
Input | struct: A live keyboard reader for a free-running event loop. |
Key | struct: A decoded key event from nextKey / decodeKey. |
Plot | struct: One cell write for setMany: the glyph cell at (x, y). |
Span | struct: One text run for textMany: text written from (x, y), in the named foreground color, or uncoloured w... |
Functions
begin
screen.begin()Enter full-screen interactive mode: put the terminal in raw mode and switch to the alternate screen with the cursor hidden and the screen cleared. Pair with end, ideally via defer screen.end($state);. Requires the term library (default binary).
- Returns
term.State - the raw-mode handle to pass to
end
box
screen.box(buf as Buffer, x as int, y as int, w as int, h as int)A copy of buf with a single-line box border drawn at (x, y) of outer size w by h, using Unicode box-drawing glyphs. The interior is untouched.
buf- the source buffer
x- the left column (0-based)
y- the top row (0-based)
w- the outer width in columns (>= 2)
h- the outer height in rows (>= 2)
- Returns
Buffer - the updated buffer
clear
screen.clear()The sequence that clears the whole screen and homes the cursor.
- Returns
string - the clear-screen escape sequence
clearBuffer
screen.clearBuffer(buf as Buffer)A copy of buf with every cell reset to a space.
buf- the source buffer
- Returns
Buffer - the cleared buffer
clearLine
screen.clearLine()The sequence that clears the current line.
- Returns
string - the clear-line escape sequence
decodeKey
screen.decodeKey(seq as list of int)Decode one raw key byte sequence into a Key. Pure and total: seq is the bytes of a single key press ([65] for A, [27, 91, 65] for Up, [27, 91, 51, 126] for Delete, [195, 182] for a two-byte UTF-8 character). A multi-byte UTF-8 sequence decodes to a "char" key holding the full rune. Unrecognized input decodes to "unknown"; an empty sequence to "eof". This is what nextKey calls after reading the bytes, exposed separately so key handling is testable without a terminal.
seq- the raw byte values of one key event
- Returns
Key - the decoded key
diff
screen.diff(old as Buffer, new as Buffer)The minimal escape string that turns the terminal showing old into new - only the changed cells are repositioned and rewritten, in row runs. When the two buffers differ in size (a resized terminal) it falls back to clearing the screen and a full render(new), so no cell of the old frame is left behind outside the new one. This is the flicker-free update path: keep the previous buffer, diff against the next.
old- the buffer currently on screen
new- the buffer to display
- Returns
string - the minimal-update escape string
down
screen.down(n as int)The sequence that moves the cursor down n rows.
n- the number of rows
- Returns
string - the cursor-down escape sequence
end
screen.end(state as term.State)Leave interactive mode: show the cursor, leave the alternate screen, and restore the terminal from the handle begin returned.
state- the handle returned by
begin - Returns
null - nothing
enterAlt
screen.enterAlt()The sequence that switches to the alternate screen buffer (so the app's output does not scroll the user's shell history).
- Returns
string - the enter-alternate-screen escape sequence
exitAlt
screen.exitAlt()The sequence that leaves the alternate screen buffer, restoring the prior terminal contents.
- Returns
string - the leave-alternate-screen escape sequence
fill
screen.fill(
buf as Buffer,
x as int,
y as int,
w as int,
h as int,
ch as string)A copy of buf with the rectangle at (x, y) of size w by h filled with the glyph ch. Clipped to the buffer.
buf- the source buffer
x- the left column (0-based)
y- the top row (0-based)
w- the width in columns
h- the height in rows
ch- the fill glyph (one column)
- Returns
Buffer - the updated buffer
get
screen.get(buf as Buffer, x as int, y as int)The contents of the cell at (x, y), or an empty string if out of range.
buf- the buffer
x- the column (0-based)
y- the row (0-based)
- Returns
string - the cell contents
hasKey
screen.hasKey(input as Input)Whether a decoded key is waiting (non-blocking): true when pollKey would return a real key rather than the "none" sentinel.
input- the live input handle (from
startInput) - Returns
bool - true if a key is buffered
hideCursor
screen.hideCursor()The sequence that hides the text cursor.
- Returns
string - the hide-cursor escape sequence
hline
screen.hline(buf as Buffer, x as int, y as int, n as int, ch as string)A copy of buf with a horizontal line of n cells of ch from (x, y).
buf- the source buffer
x- the starting column (0-based)
y- the row (0-based)
n- the length in columns
ch- the line glyph (one column)
- Returns
Buffer - the updated buffer
home
screen.home()The sequence that homes the cursor to the top-left.
- Returns
string - the cursor-home escape sequence
left
screen.left(n as int)The sequence that moves the cursor left n columns.
n- the number of columns
- Returns
string - the cursor-back escape sequence
moveTo
screen.moveTo(x as int, y as int)The sequence that moves the cursor to (x, y) (0-based; origin top-left).
x- the target column (0-based)
y- the target row (0-based)
- Returns
string - the cursor-position escape sequence
newScreen
screen.newScreen(rows as int, cols as int)A new blank Buffer of rows by cols cells, every cell a space.
rows- the number of rows (must be positive)
cols- the number of columns (must be positive)
- Returns
Buffer - the blank buffer
- Throws
Error - when
rowsorcolsis not positive
nextKey
screen.nextKey()Read and decode the next key from the terminal (blocking). Reads raw bytes through the term library, assembling a full escape sequence before decoding, and returns the Key. At end of input the name is "eof". Requires raw mode (see begin) and the term library (default binary).
A lone Escape is told apart from the start of an escape sequence by timing: after ESC, the next byte is awaited for ESCAPE_WAIT_MS (50 ms). A terminal sends a sequence's bytes together, so nothing arriving in that window is a bare Escape, returned as "escape" without consuming the key pressed after it. A key pressed within the window still merges with the Escape (ESC then q decodes as alt-q, the way terminals encode Alt).
- Returns
Key - the next key event
pollKey
screen.pollKey(input as Input)The next decoded key if one is waiting, else a "none" sentinel key (non-blocking) - the poll a free-running loop calls each tick.
input- the live input handle (from
startInput) - Returns
Key - the next key, or
Key{name: "none", char: ""}when none is ready
render
screen.render(buf as Buffer)The escape string that paints the whole buf to the terminal (positions the cursor at the start of each row and writes its cells). Print it with io.printf; usually preceded once by clear.
buf- the buffer to paint
- Returns
string - the full-repaint escape string
right
screen.right(n as int)The sequence that moves the cursor right n columns.
n- the number of columns
- Returns
string - the cursor-forward escape sequence
set
screen.set(buf as Buffer, x as int, y as int, cell as string)A copy of buf with the single cell at (x, y) set to cell (one column). Out-of-range coordinates are clipped (returns buf unchanged).
buf- the source buffer
x- the column (0-based)
y- the row (0-based)
cell- the cell contents (one visible column)
- Returns
Buffer - the updated buffer
setMany
screen.setMany(buf as Buffer, plots as list of Plot)A copy of buf with every plot applied in order, as a run of set calls would. Where a drawing call copies the grid (see Buffer), this costs one copy for the whole batch instead of one per cell. Out-of-range plots are clipped.
buf- the source buffer
plots- the cells to write
- Returns
Buffer - the updated buffer
showCursor
screen.showCursor()The sequence that shows the text cursor.
- Returns
string - the show-cursor escape sequence
size
screen.size()The terminal size as a term.Size ({rows, cols}), a convenience passthrough to term.size so an app stays in one namespace. Requires the term library.
- Returns
term.Size - the terminal dimensions
startInput
screen.startInput()Start a background keyboard reader for a free-running loop (a game, a live dashboard, anything that must keep ticking without a keypress). It spawns a task that reads and decodes keys through nextKey into a buffered channel; poll it with hasKey / pollKey (non-blocking) or drain it with waitKey (blocking) while the loop renders. Requires raw mode (begin) and the term library (default binary).
The reader task is deliberately discarded: it blocks in term.readByte and cannot be interrupted mid-read (neither task.cancel nor closing the channel reaches a syscall read), so it runs until end of input or program exit, when the OS reaps it. Discarding it is what lets the program exit instead of hanging on the never-returning reader. One reader owns stdin; do not also call nextKey while it runs.
- Returns
Input - the live input handle
text
screen.text(buf as Buffer, x as int, y as int, s as string)A copy of buf with s written left-to-right starting at (x, y), one rune per cell, clipped at the row's right edge (no wrapping).
buf- the source buffer
x- the starting column (0-based)
y- the row (0-based)
s- the text to write
- Returns
Buffer - the updated buffer
textColor
screen.textColor(
buf as Buffer,
x as int,
y as int,
s as string,
color as string)A copy of buf with s written from (x, y) in the named foreground colour, one rune per cell, clipped at the row's right edge.
buf- the source buffer
x- the starting column (0-based)
y- the row (0-based)
s- the text to write
color- the colour name (e.g.
"red","cyan") - Returns
Buffer - the updated buffer
- Throws
Error - when
coloris not a known colour
textMany
screen.textMany(buf as Buffer, spans as list of Span)A copy of buf with every span written in order, as a run of text / textColor calls would. Where a drawing call copies the grid (see Buffer), this costs one copy for the whole batch instead of one per call. Each span writes one rune per cell from its (x, y), clipped at the row's right edge, in its colour (none when color is empty). A control character is written as a space, so untrusted text cannot smuggle a terminal escape through render.
buf- the source buffer
spans- the text runs to write
- Returns
Buffer - the updated buffer
- Throws
Error - when a span names an unknown colour
up
screen.up(n as int)The sequence that moves the cursor up n rows.
n- the number of rows
- Returns
string - the cursor-up escape sequence
vline
screen.vline(buf as Buffer, x as int, y as int, n as int, ch as string)A copy of buf with a vertical line of n cells of ch from (x, y).
buf- the source buffer
x- the column (0-based)
y- the starting row (0-based)
n- the length in rows
ch- the line glyph (one column)
- Returns
Buffer - the updated buffer
waitKey
screen.waitKey(input as Input)Block until the next decoded key and return it - the buffered equivalent of nextKey, for a loop that waits rather than spins. At end of input it returns "eof", on every call after that too.
input- the live input handle (from
startInput) - Returns
Key - the next key event
Structs
Buffer
A rectangular grid of character cells, drawn into then painted to the terminal. cells is row-major (y * cols + x); each cell is a one-column string, optionally wrapped in an SGR colour sequence. Value-semantic like any struct: a drawing function returns the updated Buffer. Written as $buf = screen.set($buf, ...); in a function, a call updates the grid in place; anywhere else (top-level code, a global, inside a try, or keeping the old buffer too) it copies the whole grid, so batch such writes with setMany / textMany.
A cell is assumed to be one terminal column wide. A wide glyph (CJK, most emoji) takes two columns on screen and an East-Asian-ambiguous one (such as ♥) takes two on some CJK-locale terminals, shifting the rest of its row out of step with the buffer; keep cell text to single-width characters.
def struct Buffer {
rows as int,
cols as int,
cells as list of string
};rows- the number of rows (height)
cols- the number of columns (width)
cells- row-major cell contents, length
rows * cols
Input
A live keyboard reader for a free-running event loop. A background task drains the terminal into a buffered channel of decoded Key events, so the loop can poll for input (hasKey / pollKey) without blocking the way nextKey does. Build one with startInput.
def struct Input {
keys as channel of Key
};keys- the buffered decoded-key stream
Key
A decoded key event from nextKey / decodeKey. name is a symbolic key name: a printable key is "char" (with the character in char); the rest are named - "up" / "down" / "left" / "right", "enter" / "tab" / "escape" / "backspace" / "delete" / "insert", "home" / "end" / "pageup" / "pagedown", "f1".."f12", "ctrl-a".."ctrl-z", "alt-<c>", "eof" at end of input, and "unknown".
def struct Key {
name as string,
char as string
};name- the symbolic key name
char- the character for a
"char"/"alt-"key, else empty
Plot
One cell write for setMany: the glyph cell at (x, y).
def struct Plot {
x as int,
y as int,
cell as string
};x- the column (0-based)
y- the row (0-based)
cell- the cell contents (one visible column)
Span
One text run for textMany: text written from (x, y), in the named foreground color, or uncoloured when color is empty.
def struct Span {
x as int,
y as int,
text as string,
color as string
};x- the starting column (0-based)
y- the row (0-based)
text- the text to write
color- a colour name (as for
textColor), or""for none