# Architecture

Source: https://libtmux.org/en/go/latest/topics/architecture/

> Locate operations, distinguish snapshots from live commands, and find their implementation.

A server handle selects a tmux server. Object IDs select sessions, windows, and
panes within it. For the object hierarchy and stable IDs, start with [Server,
session, window, pane](https://libtmux.org/en/go/latest/concepts/server-session-window-pane/).

<a id="where-behavior-lives-on-the-object-or-through-the-server"></a>

## Calling operations

Session, window, and pane handles carry their ID and server context. Call an
operation on the object you want to change. The examples below send input and
kill that pane.

```go
cmd := "printf 'hello\\n'"
if err := pane.SendKeys(ctx, tmux.SendKeysRequest{Command: &cmd, Literal: true}); err != nil {
    return err
}
if err := pane.Kill(ctx); err != nil {
    return err
}
```

<a id="a-generated-data-table-under-a-hand-written-surface"></a>

## Reading tmux fields

Object IDs become tmux targets (`-t`). Format variables (`#{...}`) provide
the state returned by tmux.

[`tmux/format_generated.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/format_generated.go>) defines format fields, and
[`tmux/option_generated.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/option_generated.go>) defines options. The format generator lives in
[`tmux/internal/generate/formats/`](<https://github.com/libtmux/libtmux-go/tree/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/internal/generate/formats>). Accessors return a value and a boolean
when a field can be unavailable; check the boolean before using the value.

<a id="module-layout-by-port"></a>

## Source layout

The [`tmux`](<https://libtmux.org/en/go/latest/reference/#tmux>) package groups its implementation by concern. [`tmux/model.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/model.go>)
defines the core structs. [`tmux/lifecycle_kill.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/lifecycle_kill.go>), [`tmux/pane_capture.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/pane_capture.go>),
[`tmux/pane_geometry.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/pane_geometry.go>), and [`tmux/hierarchy.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/hierarchy.go>) implement lifecycle,
capture, geometry, and traversal. [`tmux/plan_server.go`](<https://github.com/libtmux/libtmux-go/blob/2de249b600d65218ee8b8fdc07c6b2b919242151/tmux/plan_server.go>) combines commands
into fewer invocations.

[`tmuxq/`](<https://github.com/libtmux/libtmux-go/tree/2de249b600d65218ee8b8fdc07c6b2b919242151/tmuxq>) provides predicate queries over an already-read snapshot; see
[Filtering and queries](https://libtmux.org/en/go/latest/concepts/queries/). Workspace support lives in
[`workspace/`](<https://github.com/libtmux/libtmux-go/tree/2de249b600d65218ee8b8fdc07c6b2b919242151/workspace>).
