Reference

Program, Cmd, and Sub

The runtime API — the Program record, its builders, commands, and subscriptions.

You describe an app as a Program and hand it to Runtime.run (both in Mire.App).

type Program<'model, 'msg> =
    { Init:          unit -> 'model * Cmd<'msg>
      Update:        'msg -> 'model -> 'model * Cmd<'msg>
      View:          'model -> LayoutNode<'msg>
      MapInput:      InputEvent -> 'msg option
      Subscriptions: 'model -> Sub<'msg> list
      // plus the opt-in fields set by the builders below
    }

Builders

Start with Program.create init update view, then compose:

BuilderPurpose
withMapInput (InputEvent -> 'msg option)Turn input into messages. Default: ignore everything.
withSubscriptions ('model -> Sub<'msg> list)External event sources.
withQuitOn (InputEvent -> bool)Which input ends the loop. Default: Ctrl+C.
withOnError (exn -> unit)Handle exceptions thrown inside the loop.
withKeyReleases boolForward Kitty key release events (default off).
withThemeNotifications boolReport light/dark scheme changes (DEC 2031).
withMouseRegion (RegionId option -> MouseEvent -> 'msg option)Route mouse via the region table.
Program.create init update view
|> Program.withMapInput mapInput
|> Program.withSubscriptions subscriptions
|> Runtime.run

Commands

init and update return a model and a Cmd — side effects the runtime performs.

type Cmd<'msg> =
    | NoOp
    | Batch of Cmd<'msg> list
    | AsyncCmd of (('msg -> unit) -> Async<unit>)
    | OfMsg of 'msg
    | Quit
    | WriteRaw of string
ConstructorEffect
Cmd.noneDo nothing.
Cmd.ofMsg msgEnqueue a message (chain updates).
Cmd.batch [a; b]Run several commands.
Cmd.ofAsync (fun dispatch -> async { … dispatch r … })Run async work; dispatch results back. This is how you do I/O without blocking the loop.
Cmd.quitRequest a clean shutdown (restores the terminal).
Cmd.writeRaw sWrite a raw escape outside the cell diff.
Cmd.setClipboard textCopy via OSC 52.
Cmd.kittyImage col row cols rows pngBase64Display an image (Kitty graphics).
Cmd.clearImagesClear all placed images.
let update msg model =
    match msg with
    | Refresh -> { model with Loading = true },
        Cmd.ofAsync (fun dispatch -> async {
            let! data = fetchAsync ()
            dispatch (Loaded data) })
    | Loaded d -> { model with Loading = false; Data = d }, Cmd.none
    | Copy -> model, Cmd.setClipboard model.Selected
    | Done -> model, Cmd.quit

Runtime.run supervises Cmd.ofAsync work under a shared cancellation token. Quit cancels that token (available through Async.CancellationToken), drops late messages, and delivers async failures to Program.OnError on the runtime thread. Its message pump stops after 4096 messages or 16 ms per iteration so input and rendering can proceed.

Subscriptions

Long-lived event sources, recomputed from the model each frame.

type Sub<'msg> =
    | TerminalResize of (Size -> 'msg)
    | Every of TimeSpan * (unit -> 'msg)
let subscriptions model =
    [ Sub.TerminalResize Resized
      if model.Busy then Sub.Every(TimeSpan.FromMilliseconds 100.0, fun () -> SpinnerTick) ]

init should seed the starting terminal size with Mire.Protocol.TerminalMode.getTerminalSize (); TerminalResize keeps it current.

The loop

Runtime.run drives, each frame: read input → decode to InputEvent → MapInput to a message → Update the model → build the View → Layout.measure onto a Surface → Diff.compute the changed cell runs → write only those, bracketed in synchronized output. Keep view pure and push I/O into Cmd.ofAsync. See the loop.