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

Rust API

Rust documentation

server.Server.from_env

Rust

View as Markdown

Module
server
Declared in
Server
Package
libtmux
Source
crates/libtmux/src/server.rs
Other languages
Python TypeScript Go Java .NETC++ Swift
from_env ( ) → Result<Self, Error>
method [source]
method [source]
from_env

Build a server from the TMUX variable tmux exports into every pane.

tmux sets TMUX to `<socket_path>,<server_pid>,<session_id>`. Only the socket path is used: the pid and session id are frozen when the pane spawns, and the session id goes stale as soon as the pane's window is moved to another session.

Being frozen is what makes the pid worth something to a caller who wants to know the daemon has not been replaced since. A server that restarts on the same socket answers here just as well, and reissues ids from the start, so Server::generation and Server::require_generation are what tell the two apart. This does not check on its own initiative, because that would cost a round trip on a call documented as running no tmux command.

This runs no tmux command and does not check that the server is alive; use Server::is_alive for that.

Errors

Returns an error when TMUX is unset, empty, or not shaped like that triple, and when the socket path it names is unusable.

Discussed in Environment

In other ports The server this process is running inside

Examples

let outside = libtmux::Server::from_env_value(None::<OsString>);
assert!(outside.is_err(), "a process outside tmux has no TMUX value");
let inside = libtmux::Server::from_env_value(Some("/tmp/tmux-1000/default,7,$0"))?;
assert_eq!(inside.socket_path(), std::path::Path::new("/tmp/tmux-1000/default"));
>>> from libtmux.server import Server as TmuxServer # doctest: +HIDE
>>> socket_path = server.cmd( # doctest: +HIDE
... "display-message", "-p", "-t", pane.pane_id, "#{socket_path}"
... ).stdout[0]
>>> env = { # doctest: +HIDE
... "TMUX": f"{socket_path},1,0",
... "TMUX_PANE": pane.pane_id,
... }
>>> TmuxServer.from_env(env)
Server(socket_path=...)
>>> TmuxServer.from_env(env).is_alive()
True

Outside a pane there is no server to name:

>>> try:
... TmuxServer.from_env({})
... except exc.NotInsideTmux as e:
... print(e)
Not inside a tmux pane: $TMUX is unset or empty

0 declared, 0 inherited

Esc

Type to search.