Cheatsheet
Every export of @skiffyard/clispinner. Programs import the deck once and use the spinner verbs; the catalogue module frames.j is imported by path when a program wants to validate a name before passing it.
jennifer
import "@skiffyard/clispinner/" as clispinner;
import "@skiffyard/clispinner/frames.j" as frames;Spinner
| Call | Returns | Does |
|---|---|---|
clispinner.startSpinner(opts as Options) | Spinner | launch the animating task and return at once |
clispinner.statusSpinner(sp as Spinner, msg as string) | Spinner | replace the status text |
clispinner.updateSpinner(sp as Spinner, opts as Options) | Spinner | replace a running spinner's options and restart at frame 0 |
clispinner.removeSpinner(sp as Spinner) | int | stop the spinner, erase its block, show the cursor; the frames drawn |
clispinner.finishSpinner(sp as Spinner, mark as string, msg as string) | int | removeSpinner, then write mark msg as a line that stays |
clispinner.options(name as string, label as string) | Options | options with speed 0 and stream STDERR |
clispinner.spinnerNames() | list of string | every catalogue name, sorted |
clispinner.spinnerSize(name as string) | Size | the room an animation needs |
Options and layout
| Call | Returns | Does |
|---|---|---|
clispinner.resolveOptions(opts as Options) | Options | normalise options: known name, speed of 0 or more, a known stream |
clispinner.intervalOf(opts as Options) | int | frame interval in ms: the speed, or the animation's own |
clispinner.drawable(stream as string) | bool | whether a stream is a terminal |
clispinner.terminalWidth(stream as string) | int | terminal width in columns, or FALLBACK_COLS |
clispinner.terminalHeight(stream as string) | int | terminal height in rows, or FALLBACK_ROWS |
clispinner.visibleRows(stream as string, wanted as int) | int | rows a spinner may draw: the window height minus one at most |
clispinner.frameRows(frame as string) | list of string | split a frame on \n |
clispinner.clipFrame(frame as string, maxRows as int) | string | keep the first maxRows rows of a frame |
clispinner.fitStatus(status as string, budget as int) | string | trim text to budget runes, a cut ending in … |
clispinner.wrapHard(text as string, width as int) | list of string | break text into chunks of at most width runes, mid-word |
clispinner.layoutStatus(status as string, rows as int, width as int) | list of string | lay a status out in at most rows lines of width runes |
clispinner.renderBlock(frame as string, status as string, frameCols as int, width as int) | string | compose a frame and its status into a block that does not wrap |
clispinner.renderLine(frame as string, status as string) | string | frame, a space, status; no width limit or erase sequence |
Control sequences
| Call | Returns | Does |
|---|---|---|
clispinner.eraseLine() | string | erase the current line, cursor to its start |
clispinner.eraseBlock(rows as int) | string | erase the last rows lines, cursor at the start of the topmost |
clispinner.hideCursor() | string | hide the cursor |
clispinner.showCursor() | string | show the cursor |
The animating task's state
Exported so the tests drive the state machine without a terminal.
| Call | Returns | Does |
|---|---|---|
clispinner.newState(opts as Options) | State | frame 0, nothing drawn, status set to the label |
clispinner.applyCommand(st as State, c as Command) | State | apply one command |
clispinner.currentFrame(st as State) | string | the frame to draw next |
clispinner.advance(st as State, counted as bool) | State | move to the next frame, counting it in drawn when counted |
clispinner.drewRows(st as State, rows as int) | State | record the rows just written, for the next erase |
The catalogue (frames.j)
| Call | Returns | Does |
|---|---|---|
frames.names() | list of string | every catalogue name, sorted |
frames.has(name as string) | bool | whether the catalogue has the name |
frames.get(name as string) | Frames | an animation by name, DEFAULT_NAME's when unknown; never throws |
Constants
| Constant | value | Is |
|---|---|---|
clispinner.STDOUT | "stdout" | stream name for standard output |
clispinner.STDERR | "stderr" | stream name for standard error, the default |
clispinner.FALLBACK_COLS | 80 | width assumed when the real one cannot be read |
clispinner.FALLBACK_ROWS | 24 | height assumed when the real one cannot be read |
frames.DEFAULT_NAME | "dots" | the animation get returns for an unknown name |
Types
Options
How a spinner looks and where it writes. Build one with options.
name as string: catalogue name; unknown or""falls back todotsspeed as int: frame interval in ms; 0 or less uses the animation's ownlabel as string: initial status textstream as string:STDOUTorSTDERR; anything else meansSTDERR
Spinner
Handle to a running spinner.
commands as channel of Command: the queue the task readsworker as task of int: the animating task; yields the frame countoptions as Options: the resolved options last sent to the task
Command
One instruction to a running spinner.
kind as string:"stop","status"or"update"; other kinds are ignoredtext as string: new status text, for"status"options as Options: new options, for"update"
State
The animating task's state.
options as Options: the resolved optionsspec as frames.Frames: the animation the options selectstatus as string: text beside the spinnerindex as int: next frame to draw, not wrappeddrawn as int: frames writtenrows as int: lines the block on screen occupies, for the next erasestopped as bool: whether a stop command has arrived
Size
Room an animation needs.
cols as int: terminal columns every frame occupiesrows as int: lines every frame occupies
Frames
One named animation and the box it draws in (frames.j).
name as string: catalogue nameinterval as int: design frame interval in millisecondscols as int: terminal columns every frame occupies, after paddingrows as int: lines every frame occupies; 12 forsoccerVertical, else 1frames as list of string: the frames, in order