Core Library0.xAPI reference

Choose documentation 1

latest

Current version

latest
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

attribute libtmux-lua

OwnedServer.server

A handle to one running tmux server, reached through a connected runtime.

Source and package
Qualified name
libtmux.OwnedServer.server
Module
libtmux
Declared in
OwnedServer
Package
libtmux
Source
lua/libtmux/_internal/server.lua

Description

libtmux.Runtime:connect() resolves to a server for a daemon that is already running; it never starts one, and closing the handle leaves the daemon running. libtmux.Runtime:start_server() starts a private daemon and owns it (see libtmux.OwnedServer). With no socket named, connect passes tmux none, and tmux picks the server (see libtmux.ConnectOptions). A method that talks to tmux returns a libtmux.Request: call libtmux.Request:await() from a task the runtime manages, or pass a callback to libtmux.Request:on_complete(). A failed request reports a libtmux.Error. If the daemon restarts or its socket is replaced, the handle stops working and the caller connects again.

What is on here

A tmux server does a great many things, so this class has a great many methods. They fall into a few groups:

Connecting
libtmux.Runtime:connect() takes libtmux.ConnectOptions: an absolute path to the tmux binary and, optionally, to the daemon's socket. Server:close() closes the connections the handle owns. Server:observe() takes a session handle and resolves to a libtmux.Observation, a control-mode view of that session whose watches report pane output, notifications and format values.
Finding one thing
Take one record from a libtmux.Snapshot, or from the rows of a query, with libtmux.Selection:one(), which reports no match or several as errors, or libtmux.Selection:one_or_nil(), which also accepts no match. Pass it, with its snapshot, to Server:handle(): that binds the record to a live session, window, window link, pane, client or buffer handle of the same class, and returns it directly rather than as a request.
Listing everything
Server:snapshot() lists sessions, windows, window links, panes, clients and buffers, and resolves to a libtmux.Snapshot whose collections are selections. The capture spans several tmux commands, so it is not an atomic view: observed races land in libtmux.Snapshot.races and clear libtmux.Snapshot.complete, and libtmux.SnapshotOptions.strict adds a verification pass.
Changing things
Server:new_session() creates a detached session and resolves to a libtmux.Creation holding handles for the session and the window, pane and window link it starts with. Server:switch_client() points an attached client, selected by the name and TTY a snapshot reports, at a session handle, and Server:detach_client() detaches it.
Everything else tmux keeps
Paste buffers, always by explicit name: Server:set_buffer() stores bytes, Server:show_buffer() reads them back and Server:delete_buffer() removes the buffer. None infers the most recent buffer. Key bindings, configuration files, shell jobs and wait-for channels have no method here; reach them through Server:command().
Things that need a terminal
None. The luv and Neovim adapters do not own an interactive terminal, so libtmux.Session:attach() returns unsupported_tty.
Running a command
Anything tmux can do that is not here is reachable through Server:command(), which runs one tmux command from literal arguments and resolves to its stdout, stderr, exit code and signal; a nonzero exit is reported in that result, not as a failure. Server:group() sends several commands as one ordered group with a single aggregate result, and Server:batch() runs independent commands and reports an outcome for each.

Examples

Start a private tmux server, create a session on it, and stop it. Use Lua 5.1 or newer with LuaRocks, tmux 3.3a or newer, and a C compiler and CMake for luv on Linux. In a new directory, install the library and its standalone runtime dependency from LuaRocks:

Terminal window
$ luarocks --tree ./rocks install luv &&
luarocks --tree ./rocks install libtmux &&
eval "$(luarocks --tree ./rocks path)"

Save the complete program as start_server.lua. It starts its own private tmux server, uses it and stops it, so no existing session is needed. View source

start_server.luaComplete program
local adapter = require("libtmux.runtime.luv")
local function must(value, err)
if err ~= nil then
error(tostring(err), 0)
end
return value
end
-- tmux is /usr/bin/tmux unless TMUX_BIN names another absolute path.
local binary = os.getenv("TMUX_BIN") or "/usr/bin/tmux"
must(adapter.run(function(runtime)
-- The server is private: its own directory, socket and environment.
local owned = must(runtime:start_server({ binary = binary }):await())
local server = owned.server
local before = must(server:snapshot():await())
must(server:new_session({ name = "demo", argv = { "/bin/cat" } }):await())
local after = must(server:snapshot():await())
print(("sessions: %d, then %d"):format(#before.sessions, #after.sessions))
must(owned:stop():await())
return true
end))

Run the saved program. It runs /usr/bin/tmux; set TMUX_BIN to another absolute path to use a different binary. Assertions and operation errors produce a failing exit status:

Terminal window
$ eval "$(luarocks --tree ./rocks path)" &&
lua start_server.lua

Guides

task material

Pages that discuss server.

Esc
Search in

Type to search.

Reference previewOpen reference