Skip to main content

Overview

The Kraken class is the main entry point for Kraken TUI applications. It manages:
  • Application lifecycle (initialization and shutdown)
  • Root widget mounting
  • Event loop execution
  • Focus management
  • Terminal state queries
  • Developer-assigned widget IDs
  • Performance counters
  • Animation choreography
Always call shutdown() to restore terminal state, even if errors occur. Use try/finally blocks.

Initialization

init()

Initialize the TUI system in normal terminal mode. Enters alternate screen, raw mode, and captures mouse input.
static init(): Kraken
Returns
Kraken
A new Kraken instance with the TUI system initialized.

initHeadless()

Initialize the TUI system in headless mode (no terminal needed). Useful for testing and server-side rendering.
static initHeadless(width: number, height: number): Kraken
width
number
required
Virtual terminal width in columns.
height
number
required
Virtual terminal height in rows.
Returns
Kraken
A new Kraken instance in headless mode.

shutdown()

Shut down the TUI system and restore the terminal to its original state. Clears all internal state.
shutdown(): void
Call this before your process exits to avoid leaving the terminal in raw mode.

Widget Tree

setRoot()

Set the root widget of the composition tree. This widget and its descendants will be rendered.
setRoot(widget: Widget): void
widget
Widget
required
The widget to use as the tree root.

Event Loop

run()

Run the application event loop. Resolves when stop() is called. Does not call shutdown() automatically.
async run(options?: RunOptions): Promise<void>
options
RunOptions
Configuration for the event loop.
options.mode
'onChange' | 'continuous'
default:"onChange"
Loop mode:
  • "onChange": Renders only when work exists (input, animations, dirty nodes)
  • "continuous": Runs at fixed FPS regardless of activity
options.fps
number
default:"60"
Target frames per second for continuous mode or animation bursts.
options.idleTimeout
number
default:"100"
Input poll timeout in milliseconds when idle in onChange mode.
options.onEvent
(event: KrakenEvent) => void
Callback invoked for each drained event.
options.onTick
() => void
Callback invoked each tick after events are drained, before render.
options.debugOverlay
boolean
default:"false"
Enable debug logging to stderr.
options.disableJsxDispatch
boolean
default:"false"
Disable automatic dispatch to JSX event handler props.
Use "onChange" mode for better battery life. Animations automatically trigger frame-rate rendering.

stop()

Signal the event loop to stop after the current tick. Safe to call from event handlers or external async code.
stop(): void

readInput()

Read terminal input and buffer events. Does not return the events directly; use drainEvents() to retrieve them.
readInput(timeoutMs?: number): number
timeoutMs
number
default:"0"
Timeout in milliseconds:
  • 0: Non-blocking (return immediately)
  • > 0: Block up to N milliseconds waiting for input
Returns
number
Number of events buffered by this call.

drainEvents()

Drain all buffered events. Events are removed from the internal buffer and returned as an array.
drainEvents(): KrakenEvent[]
Returns
KrakenEvent[]
Array of events drained from the buffer.

render()

Execute the full render pipeline: layout computation, diff, and terminal I/O.
render(): void
The run() method calls this automatically. Only needed for manual event loops.

Terminal Queries

getTerminalSize()

Get the current terminal dimensions.
getTerminalSize(): { width: number; height: number }
Returns
{ width: number; height: number }
Object containing terminal dimensions:
  • width: Terminal width in columns
  • height: Terminal height in rows

Focus Management

getFocused()

Get the handle of the currently focused widget.
getFocused(): number
Returns
number
Widget handle of the focused widget, or 0 if nothing is focused.

focusNext()

Advance focus to the next focusable widget in tree order.
focusNext(): void

focusPrev()

Move focus to the previous focusable widget in tree order.
focusPrev(): void

Widget IDs

setId()

Register a developer-assigned string ID for a widget. Useful for focus management and programmatic access.
setId(id: string, widget: Widget): void
id
string
required
Developer-assigned identifier.
widget
Widget
required
Widget to associate with the ID.

getHandle()

Get a widget handle by developer-assigned ID.
getHandle(id: string): number | undefined
id
string
required
Developer-assigned identifier.
Returns
number | undefined
Widget handle, or undefined if the ID is not registered.

Theme Management

switchTheme()

Apply a theme to the current root widget. Shorthand for theme.applyTo(root).
switchTheme(theme: Theme): void
theme
Theme
required
Theme to apply.

Animation Choreography

chainAnimation()

Chain animation B to start when animation A completes. Cancelling A prevents B from auto-starting.
chainAnimation(afterAnim: number, nextAnim: number): void
afterAnim
number
required
Animation handle that triggers the next animation.
nextAnim
number
required
Animation handle to start when the first completes.

createChoreoGroup()

Create a choreography animation group for coordinating multiple animations on an absolute timeline.
createChoreoGroup(): number
Returns
number
Choreography group handle.

choreoAdd()

Add an animation to a choreography group at a specific timeline offset.
choreoAdd(group: number, animationHandle: number, startAtMs: number): void
group
number
required
Choreography group handle.
animationHandle
number
required
Animation to add to the group.
startAtMs
number
required
Absolute timeline offset in milliseconds.

startChoreo()

Start a choreography group timeline.
startChoreo(group: number): void
group
number
required
Choreography group handle.

cancelChoreo()

Cancel a running choreography group and all its animations.
cancelChoreo(group: number): void
group
number
required
Choreography group handle.

destroyChoreoGroup()

Destroy a choreography group handle and free its resources.
destroyChoreoGroup(group: number): void
group
number
required
Choreography group handle.

Debug & Performance

setDebug()

Enable or disable debug logging to stderr.
setDebug(enabled: boolean): void
enabled
boolean
required
Whether to enable debug logging.

getPerfCounter()

Query a performance counter by ID.
getPerfCounter(id: number): bigint
id
number
required
Performance counter ID.
Returns
bigint
Current value of the performance counter.

getNodeCount()

Get the total number of nodes in the widget tree.
getNodeCount(): number
Returns
number
Total node count.

RunOptions Interface

mode
'onChange' | 'continuous'
default:"onChange"
Loop mode. "onChange" renders only when work exists (default). "continuous" runs at fixed fps.
fps
number
default:"60"
FPS target for continuous mode or animation bursts in onChange mode.
idleTimeout
number
default:"100"
Input poll timeout (ms) when idle in onChange mode.
onEvent
(event: KrakenEvent) => void
Called for each drained event.
onTick
() => void
Called each tick after events are drained, before render.
debugOverlay
boolean
default:"false"
Enable debug overlay (wires setDebug).
disableJsxDispatch
boolean
default:"false"
Disable automatic dispatch to JSX event handler props.

Build docs developers (and LLMs) love