# `LibTmux.Session`
[🔗](https://github.com/libtmux/libtmux-elixir/blob/86ad4ca80f157adb0c11f6bf7c371b04ca5aa688/lib/libtmux/entities.ex#L1)

An immutable session observation with explicit actions. Attachment retains tmux's client count.

## What is on here

A Session is a value captured in a `LibTmux.Snapshot`. Relationship functions
read the snapshot you pass; actions take a `LibTmux.Ref` and a Server or
Control connection. Start with these groups:

**Finding the session.** Select sessions from a captured snapshot, or look one
up by its exact ID. Neither contacts tmux. An ID that was not captured returns
`{:error, error}` with kind `:not_found`. Turn the session into a reference
tied to the server incarnation that supplied it, ready for an action.

- Select: `LibTmux.Snapshot.sessions/1`
- By ID: `LibTmux.Snapshot.fetch/3`
- Reference: `LibTmux.Ref.from_observation/1`

**Looking inside a session.** Read windows, panes and the active window from the
snapshot, without I/O. A window linked more than once appears once, and a
covered empty relation returns an empty selection. An invalid or stale
identity, or a relationship the snapshot did not capture, raises
`LibTmux.Error`. For per-session window indexes use `LibTmux.Snapshot.related/4`
with `:window_links`.

- Windows: `windows/2`
- Panes: `panes/2`
- Focused: `active_window/2`

**Creating a window.** Create a named detached window in the session through
one-shot execution. The completed value holds the new ID with `ref: nil`;
capture a fresh observation to obtain a reference. A Control connection is
unsupported, and a missing server is not started.

- Create: `new_window/4`
- Raise on failure: `new_window!/4`

**Renaming.** Rename the session through a Server or a Control connection.
A name holds 1..1024 UTF-8 bytes with no control characters, and tmux still
normalizes it. Observations you already hold keep the old name.

- Rename: `rename/4`
- Raise on failure: `rename!/4`

**Killing.** Kill the session and the windows and panes only it holds,
through a Server. A Control connection returns `unsupported_transport`.
Killing the last session ends a server that exits when empty.

- Kill: `kill/3`
- Raise on failure: `kill!/3`

# `t`

```elixir
@type t() :: %LibTmux.Session{
  attached: LibTmux.Schema.observed(boolean()),
  attached_count: LibTmux.Schema.observed(non_neg_integer()),
  id: String.t(),
  name: LibTmux.Schema.observed(String.t()),
  server_instance: String.t()
}
```

A session with explicit field coverage and an attachment count separate from its Boolean projection.

# `active_window`

```elixir
@spec active_window(LibTmux.Snapshot.t(), t()) :: LibTmux.Window.t() | nil
```

Returns the captured active physical window, or `nil` when none is active.

Multiple active links raise `LibTmux.Error`, even when they name the same
window. Unrelated sessions do not affect this lookup. Identity, coverage
and no-I/O behavior follow `windows/2`, which includes a worked example.

# `kill`

```elixir
@spec kill(LibTmux.Server.t(), LibTmux.Ref.t(), [LibTmux.Action.option()]) ::
  {:ok, LibTmux.Action.result(:ok)} | {:error, LibTmux.Action.error()}
```

Kills an exact session through one-shot execution.

Returns `{:ok, result}` on command completion. The only option is `timeout`, 5000ms by default. The reference is
checked against the answering server before dispatch, with the usual one-shot
check/use race. A reference to something that is already gone returns
`{:error, %LibTmux.Action.CompletionError{}}` with the original evidence.
Control connections return `unsupported_transport` before any I/O: a
connection cannot kill what it is attached to. Observations you already hold
keep describing the session.

    alias LibTmux.Session

    Session.kill(server, session_ref)

# `kill!`

```elixir
@spec kill!(LibTmux.Server.t(), LibTmux.Ref.t(), [LibTmux.Action.option()]) :: :ok
```

Returns `:ok` when `kill/3` completes; raises API errors or `LibTmux.Action.CompletionError` otherwise.

# `new_window`

```elixir
@spec new_window(LibTmux.Server.t(), LibTmux.Ref.t(), binary(), [
  LibTmux.Action.creation_option()
]) ::
  {:ok, LibTmux.Action.result(LibTmux.Action.creation_value())}
  | {:error, LibTmux.Action.error()}
```

Creates a named detached window in an exact session through one-shot execution.

Returns `{:ok, result}` on completion, with `%{id: window_id, ref: nil}`
as its value. Capture a fresh observation for a bound reference. The session
reference is checked against the answering server before dispatch, with
the usual one-shot check/use race. Missing servers are not started.
Control connections are unsupported. tmux permits duplicate window names.

Names, `timeout`, `cwd` and explicit `command` forms follow
`LibTmux.Server.new_session/3`. The new window uses tmux's next available
index in this session. Existing observations remain unchanged.

    alias LibTmux.Session

    Session.new_window(server, session_ref, "logs", command: {:argv, ["/bin/cat"]})

# `new_window!`

```elixir
@spec new_window!(LibTmux.Server.t(), LibTmux.Ref.t(), binary(), [
  LibTmux.Action.creation_option()
]) ::
  LibTmux.Action.creation_value()
```

Returns the created window value when `new_window/4` completes; raises the original error otherwise.

# `panes`

```elixir
@spec panes(LibTmux.Snapshot.t(), t()) :: LibTmux.Selection.t(LibTmux.Pane.t())
```

Returns a selection of captured physical panes in numeric pane-ID order.

Linked windows contribute their panes once. Identity, coverage, empty-selection
and no-I/O behavior follow `windows/2`, which includes a worked example.

# `rename`

```elixir
@spec rename(LibTmux.Action.connection(), LibTmux.Ref.t(), binary(), [
  LibTmux.Control.call_option()
]) ::
  {:ok, LibTmux.Action.result(:ok)} | {:error, LibTmux.Action.error()}
```

Renames an exact session; returns `{:ok, result}` on command completion.

Names contain 1..1024 UTF-8 bytes without Unicode control characters (Cc).
Format syntax remains literal;
tmux still normalizes names (including dollar/backslash escaping, and dots
and colons on older releases). The only option is `timeout`, which defaults
to 5000ms.
Old observations stay unchanged. See `LibTmux.Action` for outcome semantics.

    alias LibTmux.Session

    Session.rename(server, session_ref, "work")

# `rename!`

```elixir
@spec rename!(LibTmux.Action.connection(), LibTmux.Ref.t(), binary(), [
  LibTmux.Control.call_option()
]) ::
  :ok
```

Returns `:ok` when `rename/4` completes; raises API errors or `LibTmux.Action.CompletionError` otherwise.

# `windows`

```elixir
@spec windows(LibTmux.Snapshot.t(), t()) :: LibTmux.Selection.t(LibTmux.Window.t())
```

Returns a selection of captured physical windows in first-link order.

Repeated links to a window contribute it once. For membership indexes use
`LibTmux.Snapshot.related/4` with `:window_links`.

The supplied snapshot owns all returned fields and relationships. Only the
session's ID and incarnation are used; its other fields may be older.
Invalid or stale identities and missing source/relationship coverage raise
`LibTmux.Error`. A covered empty relation returns an empty selection.
No I/O occurs. The selection retains the supplied snapshot for filtering;
relationship predicates still validate their whole captured source domain.
Use `Enum.to_list/1` when only the list is needed.

## Examples

    iex> alias LibTmux.{Selection, Session, Snapshot}
    iex> rows = %{session: [%{id: "$0"}], window: [%{id: "@7"}],
    ...>   window_link: [%{session_id: "$0", window_id: "@7", index: 0, active: true},
    ...>     %{session_id: "$0", window_id: "@7", index: 3, active: false}],
    ...>   pane: [%{id: "%9", window_id: "@7"}]}
    iex> {:ok, snapshot} = Snapshot.new(rows,
    ...>   server_instance: "example", captured_from: 0, captured_until: 0,
    ...>   complete: [:window_link, :pane],
    ...>   fields: %{window_link: [:session_id, :window_id, :active], pane: [:window_id]})
    iex> {:ok, session} = Snapshot.fetch(snapshot, :session, "$0")
    iex> {:ok, window} = Session.windows(snapshot, session) |> Selection.one()
    iex> window.id
    "@7"
    iex> Session.panes(snapshot, session) |> Enum.map(& &1.id)
    ["%9"]
    iex> Session.active_window(snapshot, session) == window
    true

---

*Consult [api-reference.md](api-reference.md) for complete listing*
