LibTmux.Selection (libtmux v0.1.3)

Copy Markdown View Source

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.

Summary

Types

t()

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

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

Functions

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

Returns true on the first matching observed item.

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

Checks native identity separately from Enumerable element membership.

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

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

Returns exactly one match or raises its cardinality error.

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

Returns one match or nil, raising on multiple matches.

Returns cached materialized size in constant time.

Filters validated data while retaining order and captured graph context.

Types

t()

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

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

t(item)

@opaque t(item)

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

Functions

count(selection, criteria \\ [])

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

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

See the worked example.

exists?(selection, criteria \\ [])

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

Returns true on the first matching observed item.

See the worked example.

filter(selection, predicate)

@spec filter(t(item), (item -> as_boolean(term()))) :: t(item)
when item: LibTmux.Schema.observation()

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

See the worked example.

member_id?(selection, id)

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

Checks native identity separately from Enumerable element membership.

See the worked example.

new(snapshot, entity)

@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.

one(selection, criteria \\ [])

@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!(selection, criteria \\ [])

@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.

one_or_nil(selection, criteria \\ [])

@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!(selection, criteria \\ [])

@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.

size(selection)

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

Returns cached materialized size in constant time.

See the worked example.

where(selection, criteria)

@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 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"]