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
- Variables
- func PopupSupported(v string) bool
- func ProcessAlive(pid int) bool
- func QuoteCommandArg(s string) string
- func ShellQuote(argv []string) string
- func SocketPath(name string) string
- func UIScript() []string
- type Client
- func (c *Client) AttachArgs(session, pane string) []string
- func (c *Client) Available() bool
- func (c *Client) Base() []string
- func (c *Client) Check(ctx context.Context, paneID string, pid int) Liveness
- func (c *Client) Control(paneID string) adapter.Pane
- func (c *Client) EnsureUI(ctx context.Context) error
- func (c *Client) HasSession(ctx context.Context, session string) bool
- func (c *Client) KillOwned(ctx context.Context, paneID string, pid int, own func(PaneInfo) bool) (bool, error)
- func (c *Client) KillPane(ctx context.Context, paneID string, pid int) (bool, error)
- func (c *Client) KillServer(ctx context.Context) error
- func (c *Client) KillWindow(ctx context.Context, target string) error
- func (c *Client) NewWindow(ctx context.Context, w Window) (PaneRef, error)
- func (c *Client) PaneView(ctx context.Context, pane string, history int) (PaneView, error)
- func (c *Client) Panes(ctx context.Context) (map[string]PaneInfo, error)
- func (c *Client) PopupArgs(outer, target, session, pane string) []string
- func (c *Client) SendKey(ctx context.Context, g Guard, key string) error
- func (c *Client) SendText(ctx context.Context, g Guard, buffer, text string, bracket bool) error
- func (c *Client) WaitGone(ctx context.Context, paneID string, timeout time.Duration) bool
- type Error
- type Guard
- type Liveness
- type PaneInfo
- type PaneRef
- type PaneView
- type Window
Constants ¶
const ( OptRun = "@otag_run" OptOwner = "@otag_db" )
Window user options that tag otag's windows (see Window.Run).
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.
const OptUI = "@otag_ui"
OptUI is the server option that turns EnsureUI off: `set -g @otag_ui off`.
const ViewTimeout = time.Second
ViewTimeout bounds one PaneView call.
Variables ¶
var ErrNoTmux = errors.New("tmux not found on PATH")
ErrNoTmux means the tmux binary is not installed.
Functions ¶
func PopupSupported ¶
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 ¶
ProcessAlive reports whether a process with this pid exists.
func QuoteCommandArg ¶
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 ¶
ShellQuote renders argv as a shell command line (";" becomes `\;`).
func SocketPath ¶
SocketPath is where tmux puts the socket of `tmux -L name`.
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 (*Client) AttachArgs ¶
AttachArgs returns the argv that attaches a terminal to the window of pane.
func (*Client) Check ¶
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) EnsureUI ¶
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 ¶
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 ¶
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 ¶
KillServer stops the tmux server and removes its socket file (tests only; production never calls it).
func (*Client) KillWindow ¶
KillWindow kills the window containing target (a pane or window id); a missing window or server is not an error.
func (*Client) NewWindow ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
type Guard ¶
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.
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.