You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Resolve the pane you are running inside: from_env() (#714)
Code running inside tmux -- a script in a split, a hook, a test harness,
an agent -- could not ask libtmux where it was, and the obvious hand-roll
is wrong. tmux exports TMUX and TMUX_PANE into every pane it spawns, but
it writes them once, at spawn, and never revises them: the session id in
TMUX records where the process was launched, not where it is. Move the
pane's window to another session and that id is simply wrong.
- **New**: Pane.from_env() returns the pane the calling process runs in,
and Server.from_env(), Session.from_env() and Window.from_env() do the
same for the rest of the hierarchy. Each takes an optional env mapping
defaulting to os.environ, so code that locates itself stays testable
outside a pane.
- **Correct by construction**: from_env reads only the socket path out of
TMUX, anchors on TMUX_PANE -- which stays true for the life of the pane
-- and asks tmux for the rest. The stale session id is never consulted,
not as a fallback and not as a tie-break. So the answer survives a
move-window, and for a window linked into several sessions it is the
session tmux itself would act on. Server.from_env() runs no tmux
command at all.
- **Refuses to guess**: outside a pane, or with a TMUX that does not
parse, the family raises exc.NotInsideTmux. TMUX is split from the
right, because the pid and session id are integers and a socket path
may contain a comma; a TMUX_PANE without its % sigil is rejected rather
than matched against session names, which would silently resolve to the
wrong object.
- **Internal**: Server.__init__ derived socket_path from TMUX_TMPDIR in a
branch that could never execute -- it required socket_name to be both
None and not "default". Removed; callers that need the real path ask
tmux for #{socket_path}.
Documentation: a new self-location topic page, linked from the topics
index and from traversal, with front-page examples in the README and the
docs index, and TMUX / TMUX_PANE in the glossary.
Compatibility: purely additive. No existing behaviour changes.
Refs #704. Supersedes #705.
[**Learn more about Locating yourself**](https://libtmux.git-pull.com/topics/self_location/)
233
+
234
+
Code *running inside* a pane — a script in a split, a tmux hook, an agent — can ask where it is. tmux writes `TMUX` and `TMUX_PANE` into every pane it spawns, and `Server`, `Session`, `Window`, and `Pane` each read them back:
In a real pane tmux has already set those two variables, so `from_env()` takes no arguments and there is nothing to arrange — this README is not running in a pane, so the example sets them first. Outside tmux there is no pane to return, and `from_env()` raises `NotInsideTmux`.
0 commit comments