Filter captured values
Needs libtmux for Elixir 0.x Install
Capture one snapshot, then select from it. The examples run
on a private server from Server.with_owned/2. From a
checkout, with tmux on PATH:
$ mix run examples/filtering.exseditors = ["cat", "nvim", "vim"]
active = snapshot |> Snapshot.panes() |> Selection.where(active: true, current_command: [in: editors])
Enum.map(active, & &1.id)- Selections keep their graph and order. Enumerating one does no I/O.
size/1reads a cached count.Enum.member?/2compares elements;Selection.member_id?/2compares native IDs.
Select
where/2takes validated data. Keywords are shorthand for maps, and duplicate keys are rejected at every level.filter/2takes a unary function and usesEnumtruthiness. Predicate exceptions propagate.- Both validate before scanning, even when no items remain.
one/2returns{:ok, item}or a structured:no_matchor:multiple_matcheserror.one!/2raises the same error.one_or_nil/2also allows zero matches. Both stop at the second match.exists?/2stops at the first match.count/2counts matches;size/1returns the total.
Predicate grammar
- A bare scalar means equality;
%{equals: value}is the same. The generated schema lists each field’s type and operators. - Numbers compare numerically and strings compare case-sensitively, with no
normalization. Ordering a nullable number against a known
nilis false. notnegates equality.not_innegates membership, including knownnil.- Portable regex is not supported. Native predicates may use
Regex. - Fields in one object conjoin.
andandortake lists of criteria andnottakes one criteria object. An emptyandis true; an emptyoris false. - Separate relation quantifiers are never combined:
with_cat = Selection.where(Snapshot.sessions(snapshot), %{ panes: %{some: %{active: true, current_command: "cat"}} })That predicate needs one pane to satisfy both conditions. Two separate some
clauses can match different panes.
everyandnoneare true for a known-empty relation;someis false.countcounts the relation’s identities. Physical windows and panes deduplicate; window links keep their memberships.- To-one
is: criteriais false for known absence, andis_notnegates it.is: nilmeans absent;is_not: nilmeans present. - A session’s
active_windowcomes from a complete link domain and loaded active flags. Zero active links is known absence; several raise:ambiguous_relation. An unresolved active target stays unloaded. - Coverage checks run before evaluation, including fields behind a Boolean branch that cannot match.
External filters
Filter.new/3returns a validated portable value;new!/3raises its structured error. Applying the value keeps its constructor validation.- Prepared predicates may hold sets and closures. Portable filters hold only
data.
LibTmux.Filter.to_predicate/2reuses a filter as a predicate. Filter.from_json/3uses built-in JSON. It rejects duplicate keys, trailing data and unknown names.from_wire/3takes an already decoded map, which cannot show discarded duplicate keys.- Neither decoder creates atoms from external keys.
- Encode with
Filter.to_wire/1, thenJSON.encode!/1.
alias LibTmux.{Filter, Pane}
criteria = [active: true, current_command: [in: ["nvim", "vim"]]]{:ok, filter} = Filter.new(Pane, criteria)json = filter |> Filter.to_wire() |> JSON.encode!(){:ok, ^filter} = Filter.from_json(Pane, json)The document is:
{ "version": 1, "entity": "pane", "where": { "active": true, "currentCommand": {"in": ["nvim", "vim"]} }}Limits
Defaults: 65,536 bytes, depth 16, 1024 value nodes, 256 array elements and
8192 bytes per string. Positive integer options max_bytes, max_depth,
max_nodes, max_list and max_string_bytes narrow or widen them.
- A JSON object has at most 256 members.
- Wire depth starts at root zero and counts every member and element. The ceiling is inclusive.
- Native input depth counts criteria objects and bounds the normalized tree. A normalized conjunction adds no relation or Boolean nesting; disjunctions, negations and relation children do.
- Wire budgets also cover the versioned envelope.
- Errors carry the affected path, including Boolean and membership indexes. An early parser-limit error can name only the root.
Filters run locally on a captured snapshot. Server-side query planning is not implemented.