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

An ordered, materialized Enumerable over one captured entity type.

Enumeration never refreshes or performs I/O. `where/2` accepts predicate
data; `filter/2` accepts unary Elixir functions with Enum truthiness.
Holding a selection retains its snapshot graph. `Enum.to_list/1` projects
ordinary values when graph context is no longer needed.

# `t`

```elixir
@type t() :: t(LibTmux.Schema.observation())
```

A selection without a concrete observation type; use `t:t/1` to preserve item types.

# `t`

```elixir
@opaque t(item)
```

An ordered selection retaining items of one observation type and their originating snapshot.

# `count`

```elixir
@spec count(t(), LibTmux.Filter.input()) :: non_neg_integer()
```

Counts observed matching items; use size/1 for cached unfiltered size.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `exists?`

```elixir
@spec exists?(t(), LibTmux.Filter.input()) :: boolean()
```

Returns true on the first matching observed item.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `filter`

```elixir
@spec filter(t(item), (item -&gt; as_boolean(term()))) :: t(item)
when item: LibTmux.Schema.observation()
```

Filters with a unary local function; normal predicate exceptions propagate.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `member_id?`

```elixir
@spec member_id?(t(), LibTmux.Ref.id()) :: boolean()
```

Checks native identity separately from Enumerable element membership.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `new`

```elixir
@spec new(LibTmux.Snapshot.t(), :session | LibTmux.Session) :: t(LibTmux.Session.t())
@spec new(LibTmux.Snapshot.t(), :window | LibTmux.Window) :: t(LibTmux.Window.t())
@spec new(LibTmux.Snapshot.t(), :pane | LibTmux.Pane) :: t(LibTmux.Pane.t())
@spec new(LibTmux.Snapshot.t(), :window_link | LibTmux.WindowLink) ::
  t(LibTmux.WindowLink.t())
@spec new(LibTmux.Snapshot.t(), :client | LibTmux.Client) :: t(LibTmux.Client.t())
```

Materializes the observed items of an entity without I/O.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `one`

```elixir
@spec one(t(item), LibTmux.Filter.input()) ::
  {:ok, item} | {:error, LibTmux.Error.t()}
when item: LibTmux.Schema.observation()
```

Returns exactly one match, or a structured cardinality error; stops at the second match.

## Examples

    iex> alias LibTmux.{Selection, Snapshot}
    iex> {:ok, snapshot} = Snapshot.new(%{pane: [%{id: "%0"}, %{id: "%1"}]},
    ...>   server_instance: "example", captured_from: 0, captured_until: 0,
    ...>   fields: %{pane: [:id]})
    iex> panes = Snapshot.panes(snapshot)
    iex> {:ok, pane} = Selection.one(panes, id: "%1")
    iex> pane.id
    "%1"
    iex> {:error, error} = Selection.one(panes, id: "%9")
    iex> error.kind
    :no_match
    iex> {:error, error} = Selection.one(panes)
    iex> error.kind
    :multiple_matches

# `one!`

```elixir
@spec one!(t(item), LibTmux.Filter.input()) :: item
when item: LibTmux.Schema.observation()
```

Returns exactly one match or raises its cardinality error.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `one_or_nil`

```elixir
@spec one_or_nil(t(item), LibTmux.Filter.input()) ::
  {:ok, item | nil} | {:error, LibTmux.Error.t()}
when item: LibTmux.Schema.observation()
```

Returns one match or nil; a second match remains an error.

## Examples

    iex> alias LibTmux.{Selection, Snapshot}
    iex> {:ok, snapshot} = Snapshot.new(%{pane: [%{id: "%0"}, %{id: "%1"}]},
    ...>   server_instance: "example", captured_from: 0, captured_until: 0,
    ...>   fields: %{pane: [:id]})
    iex> panes = Snapshot.panes(snapshot)
    iex> Selection.one_or_nil(panes, id: "%9")
    {:ok, nil}
    iex> {:ok, pane} = Selection.one_or_nil(panes, id: "%0")
    iex> pane.id
    "%0"
    iex> {:error, error} = Selection.one_or_nil(panes)
    iex> error.kind
    :multiple_matches

# `one_or_nil!`

```elixir
@spec one_or_nil!(t(item), LibTmux.Filter.input()) :: item | nil
when item: LibTmux.Schema.observation()
```

Returns one match or nil, raising on multiple matches.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `size`

```elixir
@spec size(t()) :: non_neg_integer()
```

Returns cached materialized size in constant time.

See the [worked example](api-examples.html#local-graph-and-filtering).

# `where`

```elixir
@spec where(t(item), LibTmux.Filter.input()) :: t(item)
when item: LibTmux.Schema.observation()
```

Filters validated data while retaining order and captured graph context.

## Examples

One pane must satisfy both conditions inside `some`. Window `@0` has an
active shell and an inactive editor; only `@1` has an active editor.
See [filtering](filtering.html#predicate-grammar) for the complete semantics.

    iex> alias LibTmux.{Selection, Snapshot}
    iex> rows = %{window: [%{id: "@0"}, %{id: "@1"}], pane: [
    ...>   %{id: "%0", window_id: "@0", active: true, current_command: "bash"},
    ...>   %{id: "%1", window_id: "@0", active: false, current_command: "nvim"},
    ...>   %{id: "%2", window_id: "@1", active: true, current_command: "nvim"}]}
    iex> {:ok, snapshot} = Snapshot.new(rows,
    ...>   server_instance: "example", captured_from: 0, captured_until: 0,
    ...>   complete: [:pane], fields: %{pane: [:window_id, :active, :current_command]})
    iex> snapshot |> Snapshot.windows()
    ...> |> Selection.where(panes: [some: [active: true, current_command: "nvim"]])
    ...> |> Enum.map(& &1.id)
    ["@1"]

---

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