Core Library0.xExamples

Choose documentation 1

pr-173

Current version

pr-173
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice

Capture a pane and read it

Needs libtmux for Elixir 0.x Install

Capture once, then query the immutable result with no more tmux commands. Install the published package as shown in the README, then save this program as capture_and_filter.exs. It starts a private server with Server.with_owned/2, so it runs as it is:

alias LibTmux.{Selection, Server, Snapshot}
tmux = System.find_executable("tmux") || raise "tmux is not on PATH"
Server.with_owned([executable: tmux], fn server ->
{:ok, _} = Server.new_session(server, "demo", command: {:argv, ["/bin/cat"]})
{:ok, snapshot} = Server.snapshot(server)
active_ids =
snapshot
|> Snapshot.panes()
|> Selection.where(active: true)
|> Enum.map(& &1.id)
IO.puts(JSON.encode!(%{active_panes: active_ids}))
end)

Run it with tmux 3.3 or newer on PATH:

Terminal window
$ mix run capture_and_filter.exs

It prints the IDs of the active panes, in numeric order, as JSON.

  • To capture a server that already runs, build the descriptor with Server.new/1 in place of Server.with_owned/2, passing socket: or socket_name:.
  • With neither, Server.new/1 reads LIBTMUX_SOCKET_PATH, then LIBTMUX_SOCKET (a socket name). With neither set, it passes tmux no socket, and tmux uses its default socket.
  • A pane is active within its physical window. This does not choose one session’s active window. Linked windows do not duplicate their panes.
  • Server.new/1 builds a descriptor with no I/O. Server.snapshot/2 runs bounded one-shot reads, without attaching a control client or creating a missing server.
  • Selection.where/2 and Enum.map/2 use the captured graph.
  • Capture covers an interval, not an atomic tmux transaction. Capture again for fresh state.
  • The {:ok, snapshot} match makes a capture error visible. Production code can handle {:error, %LibTmux.Error{}}.
  • Default capture budgets: 5000ms, 512 commands, 1024 contextual topology rows, 64 KiB per response and 8 MiB in total. A limit failure returns an error, not a partial graph.

Next: filter and select, one-shot actions or managed control.

Choose a socket

Build a server descriptor from a socket path, a socket name or an environment map. Constructing the descriptor performs no I/O.

alias LibTmux.Server
tmux = System.find_executable("tmux") || raise "tmux is not on PATH"
by_path = Server.new(executable: tmux, socket: "/run/user/1000/work.sock")
by_name = Server.new(executable: tmux, socket_name: "work")
from_variable = Server.new(executable: tmux, env: %{"LIBTMUX_SOCKET" => "work"})
path_env = %{"LIBTMUX_SOCKET_PATH" => "/tmp/w.sock"}
from_path_variable = Server.new(executable: tmux, env: path_env)
default = Server.new(executable: tmux, env: %{})
%Server{socket: "/run/user/1000/work.sock", socket_name: nil} = by_path
%Server{socket: nil, socket_name: "work"} = by_name
%Server{socket: nil, socket_name: "work"} = from_variable
%Server{socket: "/tmp/w.sock", socket_name: nil} = from_path_variable
%Server{socket: nil, socket_name: nil} = default
IO.puts(
JSON.encode!(%{
by_path: by_path.socket,
by_name: by_name.socket_name,
from_variable: from_variable.socket_name,
from_path_variable: from_path_variable.socket,
default: "tmux chooses"
})
)
Esc
Search in

Type to search.

Reference previewOpen reference