tmux

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tmux drives otag's private tmux server (`tmux -L otag`). The user's own tmux server and Herdr sessions are never addressed: every command carries -L, and TMUX / TMUX_PANE are removed from the environment of tmux commands.

Index

Constants

View Source
const (
	OptRun   = "@otag_run"
	OptOwner = "@otag_db"
)

Window user options that tag otag's windows (see Window.Run).

View Source
const (
	Alive   = "alive"   // pane exists and its process runs
	Exited  = "exited"  // pane kept after its process exited (ExitStatus set)
	Gone    = "gone"    // pane, window or server no longer exists
	Unknown = "unknown" // no pane recorded, or tmux unavailable
)

Liveness states.

View Source
const OptUI = "@otag_ui"

OptUI is the server option that turns EnsureUI off: `set -g @otag_ui off`.

View Source
const ViewTimeout = time.Second

ViewTimeout bounds one PaneView call.

Variables

View Source
var ErrNoTmux = errors.New("tmux not found on PATH")

ErrNoTmux means the tmux binary is not installed.

Functions

func PopupSupported

func PopupSupported(v string) bool

PopupSupported reports whether a tmux of version v (`tmux -V`: "tmux 3.5a", "tmux next-3.6", "tmux master") has display-popup -B (3.3 and later).

func ProcessAlive

func ProcessAlive(pid int) bool

ProcessAlive reports whether a process with this pid exists.

func QuoteCommandArg

func QuoteCommandArg(s string) string

QuoteCommandArg quotes s as one argument of a tmux command string: single quotes (inside them tmux takes everything literally), or double quotes with \ " $ escaped when s holds a single quote.

func ShellQuote

func ShellQuote(argv []string) string

ShellQuote renders argv as a shell command line (";" becomes `\;`).

func SocketPath

func SocketPath(name string) string

SocketPath is where tmux puts the socket of `tmux -L name`.

func UIScript

func UIScript() []string

UIScript is the tmux command EnsureUI runs: uiCommands, unless @otag_ui is off.

Types

type Client

type Client struct {
	Socket string // tmux -L name
	Conf   string // tmux -f file, used when the server starts
	Bin    string // default "tmux"
}

Client runs tmux commands against one socket.

func New

func New(socket, conf string) *Client

New returns a client for socket (tmux -L) using conf as server config.

func (*Client) AttachArgs

func (c *Client) AttachArgs(session, pane string) []string

AttachArgs returns the argv that attaches a terminal to the window of pane.

func (*Client) Available

func (c *Client) Available() bool

Available reports whether tmux can be run.

func (*Client) Base

func (c *Client) Base() []string

Base returns the tmux argv prefix (binary, -L, -f).

func (*Client) Check

func (c *Client) Check(ctx context.Context, paneID string, pid int) Liveness

Check returns the liveness of paneID; pid (when non-zero) must match the pane's process, which guards against pane ids reused by a restarted server.

func (*Client) Control

func (c *Client) Control(paneID string) adapter.Pane

Control returns an adapter.Pane for paneID.

func (*Client) EnsureUI

func (c *Client) EnsureUI(ctx context.Context) error

EnsureUI applies otag's attach bindings to its server in one invocation, at runtime (never written to a file), before every attach: they survive a server restart and respect `set -g @otag_ui off` in the user's own otag tmux.conf. Only otag's private server (c.Socket) is addressed. A server that is not running is not an error.

func (*Client) HasSession

func (c *Client) HasSession(ctx context.Context, session string) bool

HasSession reports whether session exists (exact name).

func (*Client) KillOwned

func (c *Client) KillOwned(ctx context.Context, paneID string, pid int, own func(PaneInfo) bool) (bool, error)

KillOwned is KillPane with one more condition (nil: none) on the pane as tmux lists it now, e.g. its tags: a pane that fails it is left alone, like a reused one.

func (*Client) KillPane

func (c *Client) KillPane(ctx context.Context, paneID string, pid int) (bool, error)

KillPane kills the window of paneID only if that pane still runs pid (pid 0: no check). A pane id reused after a server restart is left alone. It reports whether a window was killed.

func (*Client) KillServer

func (c *Client) KillServer(ctx context.Context) error

KillServer stops the tmux server and removes its socket file (tests only; production never calls it).

func (*Client) KillWindow

func (c *Client) KillWindow(ctx context.Context, target string) error

KillWindow kills the window containing target (a pane or window id); a missing window or server is not an error.

func (*Client) NewWindow

func (c *Client) NewWindow(ctx context.Context, w Window) (PaneRef, error)

NewWindow starts w.Argv in a new detached window of w.Session, creating the session if needed. Windows keep their pane after the process exits (remain-on-exit), so the exit status stays observable until the window is killed.

func (*Client) PaneView

func (c *Client) PaneView(ctx context.Context, pane string, history int) (PaneView, error)

PaneView captures pane with one tmux invocation. history 0 is the visible screen, row for row (no -J, so rows match the pane grid and the cursor); history n > 0 is up to n lines of scrollback above it, wrapped lines joined.

func (*Client) Panes

func (c *Client) Panes(ctx context.Context) (map[string]PaneInfo, error)

Panes lists every pane of the server by pane id; a stopped server yields an empty map. Output it cannot parse is an error, never a shorter map: a pane missing from the map counts as gone.

func (*Client) PopupArgs

func (c *Client) PopupArgs(outer, target, session, pane string) []string

PopupArgs is the command that shows the window of pane in a full-screen popup of the user's own tmux (outer: its tmux binary; $TMUX names its server): a nested client of otag's server, addressed by socket path, so the popup's environment (the outer server's) cannot point it elsewhere. The return keys detach that client and -E closes the popup. target, when not empty, is the outer pane the popup belongs to ($TMUX_PANE).

func (*Client) SendKey

func (c *Client) SendKey(ctx context.Context, g Guard, key string) error

SendKey sends one key by its tmux name (Enter, C-a, M-;, ...) to g.Pane if g holds. The name is quoted for tmux's parser; it is never text the user typed.

func (*Client) SendText

func (c *Client) SendText(ctx context.Context, g Guard, buffer, text string, bracket bool) error

SendText writes text to g.Pane if g holds, as typed characters (bracket: as a paste, with bracketed-paste codes when the application asked for them, LF as CR). The text goes through a buffer loaded from stdin, never through a command line or a tmux command string, so no text can be read as a tmux command, an option or a format, and `ps` never shows it. The buffer is deleted either way.

func (*Client) WaitGone

func (c *Client) WaitGone(ctx context.Context, paneID string, timeout time.Duration) bool

WaitGone polls until paneID's pane disappears or dies (true), or timeout passes or ctx ends (false). A check that cannot tell (tmux failed) is not "gone".

type Error

type Error struct {
	Args []string
	Msg  string
}

Error is a failed tmux command.

func (*Error) Error

func (e *Error) Error() string

type Guard

type Guard struct {
	Pane, Run string
	PID       int
}

Guard is who a pane must still belong to when typed keys reach it: alive, its window's @otag_run tag is Run, and its process is PID. With PID 0 the tag must match; with a PID an untagged window passes on the PID alone. A restarted server reuses pane ids; the guard keeps typed keys from reaching whatever runs in a reused pane.

type Liveness

type Liveness struct {
	State      string `json:"state"`
	Pane       string `json:"pane,omitempty"`
	PID        int    `json:"pid,omitempty"`
	ExitStatus *int   `json:"exit_status,omitempty"`
	Command    string `json:"command,omitempty"`
	Detail     string `json:"detail,omitempty"`
	// Activity is the last output of the pane's window (alive panes; zero if unknown).
	Activity time.Time `json:"activity,omitzero"`
}

Liveness is the result of a pane check.

func CheckIn

func CheckIn(panes map[string]PaneInfo, paneID string, pid int) Liveness

CheckIn evaluates liveness against a Panes snapshot (one tmux call for many runs).

func (Liveness) Live

func (l Liveness) Live() bool

Live reports whether the harness may still be running.

type PaneInfo

type PaneInfo struct {
	PaneID     string
	Session    string
	WindowID   string
	WindowName string
	PID        int
	Dead       bool
	ExitStatus int // valid when Dead
	Command    string
	// Activity is when the pane's window last produced output (#{window_activity},
	// seconds). otag gives every run pane a window of its own. Zero when unknown.
	Activity time.Time
	// Run and Owner are the window's tags (Window.Run, Window.Owner); "" = untagged.
	Run, Owner string
}

PaneInfo is the live state of a pane.

type PaneRef

type PaneRef struct {
	Session  string `json:"session"`
	WindowID string `json:"window"`
	PaneID   string `json:"pane"`
	PID      int    `json:"pid"`
}

PaneRef identifies a created pane.

type PaneView

type PaneView struct {
	Width, Height    int
	CursorX, CursorY int
	CursorOn         bool // cursor_flag: the application shows its cursor
	AltScreen        bool
	InMode           bool // copy or view mode
	Dead             bool
	Lines            []string
}

PaneView is one capture of a pane for the TUI preview: the pane's geometry and cursor, and its screen (or the history above it) with SGR escapes.

type Window

type Window struct {
	Session string // created when missing
	Name    string
	Cwd     string
	Env     map[string]string
	Argv    []string // exec'd directly
	// Run and Owner tag the window with the user options @otag_run (the run id) and
	// @otag_db (the database the run lives in). They are set in the same command list
	// that creates the window, so no window otag starts ever exists untagged.
	Run, Owner string
	// CleanEnv starts Argv with only Env and the pane's terminal variables (keepEnv):
	// whatever the tmux server's global environment holds (the shell that started
	// it: GITHUB_TOKEN, AWS_*, ...) is unset first. Values never appear on a command
	// line: a small sh wrapper unsets the other names, then execs Argv.
	CleanEnv bool
	// EnvDir, with CleanEnv, is a private (0700) directory where Env is handed to the
	// pane in a 0600 file the pane sources and deletes, instead of `-e NAME=value`
	// arguments that `ps` shows.
	EnvDir string
}

Window describes a window to create.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL