···2020//
2121// term.VTWrite([]byte("Hello, world!\r\n"))
2222//
2323+// # Concurrency
2424+//
2525+// Unless documented otherwise, exported handle types in this package are
2626+// not safe for concurrent use. Keep each [Terminal], [Formatter],
2727+// [KeyEncoder], [MouseEncoder], [KeyEvent], [MouseEvent], and borrowed
2828+// view confined to one goroutine at a time or protect it with your own
2929+// synchronization.
3030+//
3131+// [RenderState] is the main exception. Hold exclusive access to the
3232+// terminal while calling [RenderState.Update]. After Update returns, the
3333+// render state can be read without touching the terminal until the next
3434+// Update. Do not call Update concurrently with reads from the same
3535+// render state.
3636+//
3737+// Borrowed views such as [GridRef], [KittyGraphics], [KittyGraphicsImage],
3838+// and [Selection], plus raw pixel slices returned by Kitty graphics
3939+// accessors, are only valid until the next mutating terminal call. Read
4040+// and copy what you need before mutating the terminal again.
4141+//
4242+// Plain copied values such as [Cell], [Row], [Style], [ColorRGB], and
4343+// [Palette] are regular Go values and may be retained after the call
4444+// that produced them.
4545+//
2346// # Effects
2447//
2548// The terminal communicates side-effects back to the host through
2649// effect callbacks. Register them at creation time with functional
2750// options like [WithWritePty], [WithBell], and [WithEnquiry], or
2851// on a live terminal with [Terminal.SetEffectWritePty] and friends.
5252+//
5353+// Effect callbacks run synchronously during [Terminal.VTWrite]. They
5454+// must not call [Terminal.VTWrite] on the same terminal and should avoid
5555+// blocking for long periods.
2956//
3057// [WithWritePty] is the most common effect — it delivers data that
3158// the terminal wants to send back to the pty (e.g. query responses):
+9-4
formatter.go
···161161162162// Formatter wraps a Ghostty formatter handle that can produce
163163// plain text, VT sequences, or HTML from a terminal's current state.
164164-// The terminal must outlive the formatter.
164164+// The formatter stores a borrowed reference to a terminal, so the
165165+// terminal must outlive the formatter and formatter calls must be
166166+// serialized with all other access to that terminal.
165167//
166168// Formatter implements io.WriterTo so formatted output can be written
167169// directly to any io.Writer.
···173175// NewFormatter creates a formatter for the given terminal's active screen.
174176// The terminal must outlive the formatter. The formatter captures a
175177// borrowed reference to the terminal and reads its current state on
176176-// each Format call.
178178+// each [Formatter.Format] call, so formatter calls must be serialized
179179+// with other access to the terminal.
177180func NewFormatter(t *Terminal, opts ...FormatterOption) (*Formatter, error) {
178181 // Start with GHOSTTY_INIT_SIZED defaults; options only touch
179182 // fields the caller explicitly sets.
···197200}
198201199202// Format runs the formatter and returns the output as a byte slice.
200200-// Each call reflects the terminal's current state at the time of the call.
201201-// The returned buffer is allocated by libghostty and copied into Go memory.
203203+// Each call reflects the terminal's current state at the time of the
204204+// call. Serialize Format with all other access to the underlying
205205+// terminal. The returned buffer is allocated by libghostty and copied
206206+// into Go memory.
202207func (f *Formatter) Format() ([]byte, error) {
203208 var outPtr *C.uint8_t
204209 var outLen C.size_t
+8-3
grid_ref.go
···22222323// GridRef is a resolved reference to a specific cell position in the
2424// terminal's internal page structure. Obtain a GridRef from
2525-// Terminal.GridRef, then extract cell or row data from it.
2525+// [Terminal.GridRef], then extract cell or row data from it.
2626//
2727-// A GridRef is only valid until the next update to the terminal
2828-// instance. Read and cache any needed information immediately.
2727+// A GridRef is a borrowed view into terminal internals, so callers
2828+// must use it under the same serialized access that protects the
2929+// owning terminal. Any later terminal operation may invalidate the
3030+// GridRef, even if it looks unrelated, so read and cache what you
3131+// need immediately. Values returned by its getter methods are copied
3232+// snapshots and may be retained after the GridRef itself becomes
3333+// invalid.
2934// C: GhosttyGridRef
3035type GridRef struct {
3136 ref C.GhosttyGridRef
+4-1
key_encoder.go
···12121313// KeyEncoder encodes key events into terminal escape sequences,
1414// supporting both legacy encoding and the Kitty Keyboard Protocol.
1515+// It maintains mutable encoding options and is not safe for concurrent
1616+// use.
1517//
1618// Basic usage:
1719// 1. Create an encoder with NewKeyEncoder.
···156158//
157159// Note that the macOS option-as-alt option cannot be determined from
158160// terminal state and is reset to OptionAsAltFalse by this call. Use
159159-// SetOptOptionAsAlt afterward if needed.
161161+// SetOptOptionAsAlt afterward if needed. The caller must serialize
162162+// access to both the encoder and the terminal during this call.
160163func (enc *KeyEncoder) SetOptFromTerminal(t *Terminal) {
161164 C.ghostty_key_encoder_setopt_from_terminal(enc.ptr, t.ptr)
162165}
+2-1
key_event.go
···12121313// KeyEvent is an opaque handle representing a keyboard input event
1414// containing information about the physical key pressed, modifiers,
1515-// and generated text.
1515+// and generated text. It is mutable and reusable, but not safe for
1616+// concurrent use.
1617//
1718// C: GhosttyKeyEvent
1819type KeyEvent struct {
+14-6
kitty_graphics.go
···122122)
123123124124// KittyGraphics is a handle to the Kitty graphics image storage
125125-// associated with a terminal's active screen. It is borrowed from
126126-// the terminal and remains valid until the next mutating terminal
127127-// call (e.g. VTWrite or Reset).
125125+// associated with a terminal's active screen. It is borrowed from the
126126+// terminal and remains valid until the next mutating terminal call
127127+// (for example [Terminal.VTWrite] or [Terminal.Reset]). Access to this
128128+// handle must be serialized with mutations of the owning terminal.
128129//
129130// C: GhosttyKittyGraphics
130131type KittyGraphics struct {
···133134134135// KittyGraphicsImage is a handle to a single Kitty graphics image.
135136// It is borrowed from the storage and remains valid until the next
136136-// mutating terminal call.
137137+// mutating terminal call. Access to this handle and any borrowed pixel
138138+// data derived from it must be serialized with mutations of the owning
139139+// terminal.
137140//
138141// C: GhosttyKittyGraphicsImage
139142type KittyGraphicsImage struct {
···143146// KittyGraphicsPlacementIterator iterates over placements in the
144147// Kitty graphics storage. It is independently owned and must be
145148// freed by calling Close, but the data it yields is only valid
146146-// while the underlying terminal is not mutated.
149149+// while the underlying terminal is not mutated. Access to the iterator
150150+// must therefore be serialized with mutations of the terminal that
151151+// produced it.
147152//
148153// C: GhosttyKittyGraphicsPlacementIterator
149154type KittyGraphicsPlacementIterator struct {
···207212 KittyImageCompressionZlibDeflate KittyImageCompression = C.GHOSTTY_KITTY_IMAGE_COMPRESSION_ZLIB_DEFLATE
208213)
209214210210-// Selection represents a grid selection range defined by two grid references.
215215+// Selection represents a grid selection range defined by two grid
216216+// references. Because Start and End are [GridRef] values, a Selection
217217+// returned by Kitty graphics helpers is also a borrowed view and is
218218+// invalidated by the next mutation of the owning terminal.
211219//
212220// C: GhosttySelection
213221type Selection struct {
+4-1
mouse_encoder.go
···19192020// MouseEncoder encodes mouse events into terminal escape sequences,
2121// supporting X10, UTF-8, SGR, URxvt, and SGR-Pixels mouse protocols.
2222+// It maintains mutable encoder state and is not safe for concurrent
2323+// use.
2224//
2325// Basic usage:
2426// 1. Create an encoder with NewMouseEncoder.
···182184183185// SetOptFromTerminal reads the terminal's current mouse tracking mode
184186// and output format and applies them to the encoder. It does not
185185-// modify size or any-button state.
187187+// modify size or any-button state. The caller must serialize access to
188188+// both the encoder and the terminal during this call.
186189func (enc *MouseEncoder) SetOptFromTerminal(t *Terminal) {
187190 C.ghostty_mouse_encoder_setopt_from_terminal(enc.ptr, t.ptr)
188191}
+2-1
mouse_event.go
···10101111// MouseEvent is an opaque handle representing a normalized mouse
1212// input event containing action, button, modifiers, and surface-space
1313-// position.
1313+// position. It is mutable and reusable, but not safe for concurrent
1414+// use.
1415//
1516// C: GhosttyMouseEvent
1617type MouseEvent struct {
+14-2
render_state.go
···1313// for repeated updates from a single terminal, only updating dirty
1414// regions of the screen.
1515//
1616+// A render state owns its own snapshot storage. Hold exclusive access
1717+// to the terminal while calling [RenderState.Update]. After Update
1818+// returns, the render state can be read without touching the terminal
1919+// until the next Update. Do not call Update concurrently with reads
2020+// from the same render state.
2121+//
2222+// Iterators populated from the render state are only valid until the
2323+// next Update, but copied values returned from their getter methods can
2424+// be retained.
2525+//
1626// Basic usage:
1727// 1. Create an empty render state with NewRenderState.
1828// 2. Update it from a terminal via Update whenever needed.
···96106}
9710798108// Update updates the render state from a terminal instance. This
9999-// consumes terminal/screen dirty state. The terminal must not be
100100-// used concurrently during this call.
109109+// consumes terminal/screen dirty state and is the only render-state
110110+// operation that touches the terminal. Hold exclusive access to the
111111+// terminal while this call is running, and do not read from the same
112112+// render state concurrently with Update.
101113func (rs *RenderState) Update(t *Terminal) error {
102114 return resultError(C.ghostty_render_state_update(rs.ptr, t.ptr))
103115}
+7-4
render_state_cell.go
···48484949// RenderStateRowCells iterates over cells in a render-state row.
5050// Create one with NewRenderStateRowCells, populate it via
5151-// RenderStateRowIterator.Cells, then advance with Next (or jump
5252-// with Select) and read data with getter methods.
5151+// [RenderStateRowIterator.Cells], then advance with
5252+// [RenderStateRowCells.Next] (or jump with [RenderStateRowCells.Select])
5353+// and read data with getter methods.
5354//
5455// A single instance can be reused across rows to avoid repeated
5555-// allocation. Cell data is only valid until the next call to
5656-// RenderState.Update.
5656+// allocation. The cells view is only valid until the next call to
5757+// [RenderState.Update]. Do not use it while [RenderState.Update] may
5858+// run on the same render state. Extracted [Cell], [Style], color
5959+// values, and grapheme slices are copied values and may be retained.
5760//
5861// C: GhosttyRenderStateRowCells
5962type RenderStateRowCells struct {
+2-2
render_state_data.go
···313313// from the render state. The iterator can then be advanced with Next
314314// and queried with getter methods.
315315//
316316-// The iterator can be reused across multiple calls. Row data is only
317317-// valid until the next call to Update.
316316+// The iterator can be reused across multiple calls. The iterator view
317317+// is only valid until the next call to [RenderState.Update].
318318func (rs *RenderState) RowIterator(ri *RenderStateRowIterator) error {
319319 return resultError(C.ghostty_render_state_get(
320320 rs.ptr,
+7-5
render_state_row.go
···35353636// RenderStateRowIterator iterates over rows in a render state.
3737// Create one with NewRenderStateRowIterator, populate it via
3838-// RenderState.RowIterator, then advance with Next and read data
3939-// with getter methods.
3838+// [RenderState.RowIterator], then advance with [RenderStateRowIterator.Next]
3939+// and read data with getter methods.
4040//
4141-// Row data is only valid as long as the underlying render state
4242-// is not updated. It is unsafe to use row data after calling
4343-// RenderState.Update.
4141+// The iterator's current position is only valid as long as the
4242+// underlying render state is not updated. Do not use the iterator
4343+// while [RenderState.Update] may run on the same render state.
4444+// Extracted [Row] values are copied snapshots and may be retained after
4545+// the iterator itself becomes invalid.
4446//
4547// C: GhosttyRenderStateRowIterator
4648type RenderStateRowIterator struct {
+6-2
screen.go
···9999)
100100101101// Cell is a wrapper around an opaque terminal grid cell value.
102102-// Use getter methods to extract data from it.
102102+// Use getter methods to extract data from it. A Cell is a copied value
103103+// snapshot, not a borrowed handle, so it may be retained after the
104104+// [GridRef] or render-state iterator that produced it becomes invalid.
103105// C: GhosttyCell
104106type Cell struct {
105107 c C.GhosttyCell
106108}
107109108110// Row is a wrapper around an opaque terminal grid row value.
109109-// Use getter methods to extract data from it.
111111+// Use getter methods to extract data from it. A Row is a copied value
112112+// snapshot, not a borrowed handle, so it may be retained after the
113113+// [GridRef] or render-state iterator that produced it becomes invalid.
110114// C: GhosttyRow
111115type Row struct {
112116 c C.GhosttyRow
+5-3
style.go
···6060 UnderlineDashed = C.GHOSTTY_SGR_UNDERLINE_DASHED
6161)
62626363-// Style is a thin wrapper around the C GhosttyStyle. It provides
6464-// getter methods to access individual style attributes without
6565-// copying the entire struct upfront.
6363+// Style is a thin wrapper around the copied C GhosttyStyle value. It
6464+// provides getter methods to access individual style attributes
6565+// without copying the entire struct upfront. A Style is a value
6666+// snapshot and may be retained after the terminal, [GridRef], or
6767+// render-state iterator that produced it becomes invalid.
6668// C: GhosttyStyle
6769type Style struct {
6870 c C.GhosttyStyle
+16-5
terminal.go
···1111)
12121313// Terminal wraps a Ghostty VT terminal handle.
1414+// It is stateful, not safe for concurrent use, and not reentrant.
1515+// Serialize all calls that touch a terminal, including getters,
1616+// setters, [Terminal.VTWrite], [Terminal.Resize], [Terminal.Close],
1717+// and any borrowed handles derived from it. Effect callbacks run
1818+// synchronously during [Terminal.VTWrite]; they must not call
1919+// [Terminal.VTWrite] on the same terminal and should avoid blocking
2020+// for long periods.
1421// C: GhosttyTerminal
1522type Terminal struct {
1623 ptr C.GhosttyTerminal
···281288282289// VTWrite feeds raw VT-encoded bytes through the terminal's parser,
283290// updating terminal state. Malformed input is handled gracefully and
284284-// will not cause an error.
291291+// will not cause an error. Effect callbacks run synchronously before
292292+// this call returns; they must not call [Terminal.VTWrite] on the same
293293+// terminal.
285294func (t *Terminal) VTWrite(data []byte) {
286295 if len(data) == 0 {
287296 return
···376385}
377386378387// KittyGraphics returns the Kitty graphics image storage for the
379379-// terminal's active screen. The returned handle is borrowed from
380380-// the terminal and remains valid until the next mutating call
381381-// (e.g. VTWrite or Reset).
388388+// terminal's active screen. The returned handle is borrowed from the
389389+// terminal and remains valid until the next mutating call (for example
390390+// [Terminal.VTWrite] or [Terminal.Reset]). Serialize use of the
391391+// returned handle with mutations of the terminal.
382392func (t *Terminal) KittyGraphics() (*KittyGraphics, error) {
383393 var ptr C.GhosttyKittyGraphics
384394 if err := resultError(C.ghostty_terminal_get(
···392402}
393403394404// GridRef resolves a point in the terminal grid to a grid reference.
395395-// The returned GridRef is only valid until the next terminal update.
405405+// The returned GridRef is borrowed from terminal internals and should
406406+// be used immediately; any later terminal operation may invalidate it.
396407//
397408// Lookups using PointTagActive and PointTagViewport are fast.
398409// PointTagScreen and PointTagHistory may be expensive for large