Subscriptions for a Tui.Program.
import Tui.Sub
type Msg
= KeyPressed Tui.Sub.KeyEvent
| Resized { width : Int, height : Int }
subscriptions : Model -> Tui.Sub.Sub Msg
subscriptions _ =
Tui.Sub.batch
[ Tui.Sub.onKeyPress KeyPressed
, Tui.Sub.onResize Resized
]
update : Msg -> Model -> ( Model, Effect.Effect Msg )
update msg model =
case msg of
KeyPressed event ->
case event.key of
Tui.Sub.Character 'q' ->
( model, Effect.exit )
Tui.Sub.Arrow Tui.Sub.Up ->
( { model | count = model.count + 1 }
, Effect.none
)
_ ->
( model, Effect.none )
Resized _ ->
( model, Effect.none )Subscribe to keyboard events.
Subscribe to mouse events (click, scroll). Enables SGR extended mouse reporting in the terminal when subscribed.
Subscribe to paste events. You can receive paste events and choose how to handle it in your program (treating it as normal text, or as you sometimes see in TUIs, bracketed text).
Subscribe to terminal resize events. Fires once on init with the starting
terminal dimensions, and again whenever the terminal is resized. The record
contains width and height in columns/rows.
Use this to store dimensions in your model (e.g., for Layout.withContext
or Layout.handleMouse).
Tui.Sub.onResize (\{ width, height } -> Resized width height)Periodic tick at the given interval in milliseconds. The message
constructor receives the wall-clock time (Time.Posix) at which the tick
actually fired. You can store it in your Model and subtract from the previous tick
to compute smooth deltas for animations.
import Time
type Msg
= SpinnerTick Time.Posix
| ClockTick Time.Posix
subscriptions _ =
Tui.Sub.batch
[ Tui.Sub.everyMillis 50 SpinnerTick
, Tui.Sub.everyMillis 1000 ClockTick
]Each unique interval runs independently. When the runtime is blocked
for longer than the interval, catch-up fires once with the actual elapsed
Posix (it does not rapid-fire the missed ticks).
The values you pattern-match on inside update when handling
onKeyPress.
Key values.
Arrow key direction.
Key modifier.
The values you pattern-match on inside update when handling
onMouse.
Mouse event from the terminal. Uses SGR extended mouse mode for accurate coordinates on any terminal size.
Coordinates are 0-based: { row = 0, col = 0 } is the top-left corner.
amount on scroll events is the number of coalesced scroll steps. Rapid
scrolling batches events on the JS side (like gocui's event drain) so you
get one event with amount = 5 instead of 5 separate events. Multiply your
scroll distance by amount for responsive feel.
Mouse button for click events.
A TUI subscription. Build values with
none,batch,onKeyPress,onMouse,onPaste,onResize, andeveryMillis. The constructors are implementation details; application code should not pattern-match on them.