Ghostty.LiveTerminal (Ghostty v0.4.0)

Copy Markdown View Source

Low-level Phoenix LiveView helpers for terminal rendering.

Provides utilities for translating browser keyboard events, building JSON-safe render payloads, and pushing terminal state to the client. Use these when you need full control over the LiveView wiring.

For a higher-level drop-in component, see Ghostty.LiveTerminal.Component.

JavaScript hook

Run mix igniter.install ghostty in your Phoenix app to vendor ghostty.js into assets/vendor/ghostty.js and wire GhosttyTerminal into assets/js/app.js.

Summary

Functions

Converts terminal cells into a JSON-safe nested list.

Returns JSON-safe cursor metadata for the visible viewport.

Encodes a terminal focus change event.

Converts a browser key event into an encoded terminal escape sequence.

Converts a browser mouse event into an encoded terminal escape sequence.

Resizes the terminal and optional PTY to the given dimensions.

Writes committed browser text input to the terminal.

Parses browser key event params into a Ghostty.KeyEvent.

Parses browser mouse event params into a Ghostty.MouseEvent.

Returns JSON-safe mouse reporting mode metadata.

Pushes a "ghostty:render" event with the current terminal cells.

Returns a render payload map for push_event/3.

Renders a terminal container <div> with the GhosttyTerminal JS hook.

Functions

cells_payload(term)

@spec cells_payload(GenServer.server()) :: [[list()]]

Converts terminal cells into a JSON-safe nested list.

Cell tuples {grapheme, fg, bg, flags} become [grapheme, fg, bg, flags] where colors are [r, g, b] or nil.

cursor_payload(term)

@spec cursor_payload(GenServer.server()) :: map()

Returns JSON-safe cursor metadata for the visible viewport.

handle_focus(gained?)

@spec handle_focus(boolean()) :: {:ok, binary()} | :none

Encodes a terminal focus change event.

handle_key(term, params)

@spec handle_key(GenServer.server(), map()) :: {:ok, binary()} | :none

Converts a browser key event into an encoded terminal escape sequence.

Returns {:ok, binary} or :none for unrecognized keys.

handle_mouse(term, params)

@spec handle_mouse(GenServer.server(), map()) :: {:ok, binary()} | :none

Converts a browser mouse event into an encoded terminal escape sequence.

handle_resize(term, cols, rows, pty \\ nil)

@spec handle_resize(
  GenServer.server(),
  pos_integer(),
  pos_integer(),
  GenServer.server() | nil
) :: :ok

Resizes the terminal and optional PTY to the given dimensions.

handle_text(term, data)

@spec handle_text(GenServer.server(), binary()) :: :ok

Writes committed browser text input to the terminal.

This is intended for paste and IME composition commits.

key_event_from_params(params)

@spec key_event_from_params(map()) :: Ghostty.KeyEvent.t() | :none

Parses browser key event params into a Ghostty.KeyEvent.

Returns a Ghostty.KeyEvent struct or :none for unrecognized keys.

Examples

key_event_from_params(%{"key" => "Enter"})
#=> %Ghostty.KeyEvent{action: :press, key: :enter}

key_event_from_params(%{"key" => "Dead"})
#=> :none

mouse_event_from_params(params)

@spec mouse_event_from_params(map()) :: Ghostty.MouseEvent.t() | :none

Parses browser mouse event params into a Ghostty.MouseEvent.

Returns a Ghostty.MouseEvent struct or :none for invalid events.

mouse_payload(term)

@spec mouse_payload(GenServer.server()) :: map()

Returns JSON-safe mouse reporting mode metadata.

push_render(socket, id, term)

Pushes a "ghostty:render" event with the current terminal cells.

The payload includes the element id for multi-terminal filtering.

render_payload(id, term)

@spec render_payload(String.t(), GenServer.server()) :: map()

Returns a render payload map for push_event/3.

Includes the component id so the JS hook can filter events when multiple terminals share a LiveView.

terminal(assigns)

Renders a terminal container <div> with the GhosttyTerminal JS hook.

This is a stateless function component. For a stateful LiveComponent that handles key events internally, use Ghostty.LiveTerminal.Component.

Supports global HTML attributes via :rest.

Attributes

  • id (:string) (required)
  • cols (:integer) - Defaults to 80.
  • rows (:integer) - Defaults to 24.
  • fit (:boolean) - Defaults to false.
  • autofocus (:boolean) - Defaults to false.
  • class (:string) - Defaults to "".
  • Global attributes are accepted.