Core LibraryAPI Reference

Choose documentation 3

Concept and alpha: example support scope and release maturity.

v0.1.0.alpha.2

Current version

v0.1.0.alpha.2
English

Prerelease This site documents an alpha of libtmux. Its structure, URLs and APIs are subject to change.

class libtmux-ruby v0.1.0.alpha.2

Server

Module LibTmux::Server LibTmux

Connects to an existing endpoint. Closing retires clients, never the daemon.

Source and package
Module
LibTmux
Package
libtmux
Source
gems/libtmux/lib/libtmux/terminal.rb
Other languages
PowerShell (cmdlet)Julia (struct)PythonLuaTypeScriptRustGoJavaC#C++SwiftKotlinScalaF# (module)
class LibTmux::Server

Description#

A server is bound to one tmux executable and one Unix socket. It keeps a private link to the socket it bound, so commands never reach a different daemon that later takes over the same path. An EntityRef is valid only on the server that issued it; another server raises TargetNotFoundError .

Methods that run a tmux command take timeout: and cancel:. The typed methods raise CommandError when tmux rejects a command, and a listing that fails raises, so an empty array means tmux answered with nothing to list. #run and #run_group answer their result whatever tmux's exit status. max_requests: bounds concurrent commands and max_controls: bounds control connections; a call past either limit raises CapacityError instead of waiting. After #close the server refuses new requests with ClosedError , and #diagnostics still answers. A forked child can close an inherited server but cannot use it.

What is on here

The methods fall into the groups below. A Session , Window , or Pane carries the operations on that one thing.

Connecting. Server.new and Server.open bind an endpoint that already exists; Server.open with a block closes the server when the block ends. Server.start starts a private daemon instead, creates no session, and owns the daemon's exit. #endpoint answers the Endpoint and #owned? says whether this server started its daemon. #diagnostics reports the request and control slots in use, and the limits. #close retires the server's clients; on an owned server it also stops the daemon. #open_control opens a ControlConnection to a session.

Finding one thing. #session , #window , #pane , and #window_link wrap an EntityRef in a handle without running a command. Take the ref from a handle or a snapshot record. A ref of the wrong kind raises TargetNotFoundError ; one whose entity has gone fails at its first command.

Listing everything. #list_sessions , #list_windows , and #list_panes answer frozen arrays of handles in numeric ID order, and #list_window_links answers every WindowLink . #list_clients answers one hash per client. #snapshot captures the session, window, and pane tree, and clients with clients: true, as one validated Snapshot .

Querying things and getting around. #search_panes filters the panes of a snapshot with where:, a hash of criteria or a FilterExpr , and answers a Selection ; #explain_panes answers the plan for a query without running tmux. A Selection narrows with Selection#where , Selection#select , Selection#filter , and Selection#reject , tests with Selection#exists? , and takes a single match with Selection#one , which raises NoMatchError or MultipleMatchesError otherwise, or Selection#one_or_nil . Build and compose criteria with FilterExpr.build , FilterExpr#and , FilterExpr#or , FilterExpr#not , and FilterExpr.from_json .

A Snapshot answers relational questions without another command: Snapshot#sessions , Snapshot#windows , Snapshot#panes , and Snapshot#clients . Its records follow relations by name: a SessionSnapshot reaches windows, panes, and current_window , a WindowSnapshot reaches panes and active_pane , and a PaneSnapshot reaches its window. Entity#snapshot reads one handle back as a record, and CapturedRecord#ref with #session , #window , or #pane turns a record into a handle. Live handles go down one command at a time with Session#list_windows , Session#list_panes , and Window#list_panes , and back up with Entity#server .

Changing things. #new_session starts a detached session running command:, an argv array rather than a shell string, and answers the Session or, with receipt: true, a CreationReceipt . #kill stops the tmux server, which #close does only for an owned server. #switch_client points the client named by client: at a session.

Options and hooks. #options answers Options at server scope, or at the global session or window scope. #hooks answers Hooks at the global session or window scope. #environment , #set_environment , and #unset_environment read, set, and unset one variable in the server's global environment.

Everything else tmux keeps. Paste buffers: #list_buffers , #read_buffer , #write_buffer , and #delete_buffer . #source_file loads a configuration file. #display expands a tmux format; the argument is a format, not literal text. #wait_for waits on, signals, locks, or unlocks a channel, as action: says.

Things that need a terminal. #attach runs an interactive client on a TTY the caller supplies and keeps, blocks until that client exits, and answers a TerminalResult . Cancelling detaches the client and does not stop pane commands.

Anything else tmux can do is reachable through #run , which runs one command from an argv array and keeps tmux's separators and format semantics, or #run_group , which runs several in one tmux invocation. A GroupResult cannot attribute the client's final status to a single step.

Examples#

Connect a Server client to an existing endpoint and close it without stopping the daemon. The outer Server.start block owns the private demonstration server. Use Ruby 4.0.7, Bundler, Git and tmux 3.2a or newer on Unix, with /bin/cat and /bin/sh available. In an empty directory, fetch the documented library revision:

Terminal window
$ git clone https://github.com/libtmux/libtmux-ruby.git libtmux-source && \
git -C libtmux-source checkout dcc9f8b5a6970144a9e7d80cd5f381d8f6198372

Save this dependency file as Gemfile:

source "https://rubygems.org"
gem "libtmux", path: "libtmux-source/gems/libtmux"

Save this complete program as server.rb. Its Server.start block owns a private tmux server and stops it on exit, including after an error. View source

# frozen_string_literal: true
require "libtmux"
begin
LibTmux::Server.start do |owner|
owner.new_session(name: "work", command: ["/bin/cat"])
server = LibTmux::Server.new(endpoint: owner.endpoint)
begin
puts "connected sessions: #{server.list_sessions.length}"
ensure
server.close
end
puts "sessions after client close: #{owner.list_sessions.length}"
end
rescue StandardError => error
warn "#{error.class}: #{error.message}"
exit 1
end

Install the runtime dependencies and run the saved program. An operation or cleanup error makes it exit unsuccessfully:

Terminal window
$ bundle config set --local path vendor/bundle && \
bundle install --jobs 2 && \
bundle exec ruby server.rb

Expected output:

connected sessions: 1
sessions after client close: 1
In other ports A tmux server

Members39#

Collections and queries7#

Common operations19#

Other members13#

Esc

Type to search.

Reference preview Open reference