Create sessions, windows and panes
Needs libtmux for Haskell unreleased Install
createSession creates a detached session through
an existing Server. It returns the actual session,
initial window and pane references printed by tmux. Use those references for
commands, then capture again to observe the resulting graph.
createWindow and splitPane use the same historical
receipts and recovery path with explicit placement and pane targets.
Run from a provisioned checkout.
$ cabal run --verbose=0 create-sessionThe program reads one option type and one hook scope from
the daemon’s catalogue. It checks an empty value, a removal
marker and an absent entry
in the new session’s environment.
It adds a window and splits an explicit pane, then links that window
at a second index, selects an exact pane, tiles the window and removes the
extra link. The new pane disables terminal echo, prints a readiness marker and starts /bin/cat -.
The caller waits for the marker and captures a baseline.
It sends literal text and a named Enter,
then waits for fresh text from the pane.
The program checks duplicate-name rejection and removes its targets. The
private scope retires the daemon. Captures remain readable afterward.
{-# LANGUAGE OverloadedRecordDot #-}{-# LANGUAGE OverloadedStrings #-}
-- | Create native targets, refresh their graph, and retain detached values.module Main (main) where
import Control.Exception (ExceptionWithContext (..), rethrowIO)import Control.Monad (unless)import Data.List.NonEmpty (NonEmpty (..))import Data.Maybe (isNothing)import Data.Text qualified as Textimport LibTmux qualified as Tmuximport LibTmux.Creation qualified as Creationimport LibTmux.Recovery qualified as Recoveryimport System.Exit (die)import System.OsString.Posix qualified as Os
main :: IO ()main = do width <- require "Width" (Tmux.mkDimension 120) extraIndex <- require "Second link index" (Tmux.mkWindowIndex 4) let options = Creation.defaultSessionOptions { Creation.name = Just "created-workspace" , Creation.windowName = Just (Tmux.labelFromBytes "editor") , Creation.width = Just width , Creation.process = Creation.DirectCommand (Os.fromBytestring "/bin/cat") (Os.fromBytestring "-" :| []) } result <- Tmux.tryTmux $ Tmux.withOwnedServer Tmux.defaultOwnedServerConfig $ \server -> do let capabilities = Tmux.serverCapabilities server history <- maybe (die "Missing history-limit option") pure (Tmux.lookupOption capabilities "history-limit") renameHook <- maybe (die "Missing after-rename-window hook") pure (Tmux.lookupHook capabilities "after-rename-window") putStrLn $ "history-limit kind: " <> show history.optionKind putStrLn $ "after-rename-window scopes: " <> show renameHook.hookScopes before <- Tmux.capture server created <- Tmux.createSession server options let environment = Tmux.SessionEnvironment created.session key = "LIBTMUX_EXAMPLE_STATE" Tmux.setEnvironment server environment key Tmux.Visible "" empty <- Tmux.getEnvironment server environment key Tmux.markEnvironmentRemoved server environment key removed <- Tmux.getEnvironment server environment key Tmux.unsetEnvironment server environment key absent <- Tmux.getEnvironment server environment key unless (empty == Just (Tmux.EnvironmentValue Tmux.Visible "") && removed == Just (Tmux.EnvironmentRemoved Tmux.Visible) && absent == Nothing) (die "Expected distinct empty, removed and absent entries") Tmux.renameWindow server created.window "api" Tmux.setPaneTitle server created.pane "editor-ready" added <- Tmux.createWindow server (Creation.NextFree created.session) Creation.defaultWindowOptions {Creation.windowLabel = Just "worker", Creation.windowProcess = options.process} let workerSlot = Creation.WindowSlot added.session added.windowIndex secondLink = Creation.WindowSlot added.session extraIndex split <- Tmux.splitPane server workerSlot added.pane Creation.defaultSplitOptions { Creation.direction = Creation.LeftRight , Creation.size = Creation.SplitPercent 25 , Creation.splitProcess = Creation.ShellCommand "stty -echo; printf 'input-ready\\n'; exec /bin/cat -" } Tmux.linkWindow server workerSlot secondLink Tmux.selectWindowLink server secondLink Tmux.selectExactPane server secondLink split.pane Tmux.selectLayout server secondLink Tmux.Tiled linked <- Tmux.capture server linkedSlots <- require "Worker links" (Tmux.linksForSession linked added.session) let workerIndices = [Tmux.windowIndexValue link.index | link <- linkedSlots, link.window == added.window] Tmux.unlinkWindow server secondLink _ <- awaitText server split.pane Tmux.defaultPaneWait "input-ready" baseline <- Tmux.capturePane server split.pane Tmux.defaultPaneCapture Tmux.sendKeys server split.pane (Tmux.LiteralText "Enter") Tmux.sendKeys server split.pane (Tmux.NamedKeys ("Enter" :| [])) screen <- awaitText server split.pane Tmux.defaultPaneWait {Tmux.waitMatch = Tmux.FreshSince baseline} "Enter" after <- Tmux.capture server duplicate <- Tmux.tryTmux (Tmux.createSession server options) rejected <- case duplicate of Left (ExceptionWithContext context (Tmux.MutationRejected _)) -> do receipts <- traverse Recovery.creationReceipt (Recovery.creationRecoveries context) unless (all isNothing receipts) (die "Duplicate creation returned another session") pure True Left problem -> rethrowIO problem Right _ -> die "Duplicate creation unexpectedly succeeded" Tmux.killSession server created.session let disposable = options {Creation.name = Nothing} windowOwner <- Tmux.createSession server disposable Tmux.killWindow server windowOwner.window paneOwner <- Tmux.createSession server disposable Tmux.killPane server paneOwner.pane remaining <- Tmux.capture server Tmux.killServer server pure (created, added, split, before, after, screen, workerIndices, rejected, remaining) case result of Left problem -> rethrowIO problem Right (created, added, split, before, after, screen, workerIndices, rejected, remaining) -> do session <- require "Created session" (Tmux.lookupSession after created.session) window <- require "Created window" (Tmux.lookupWindow after created.window) pane <- require "Created pane" (Tmux.lookupPane after created.pane) worker <- require "New window" (Tmux.lookupWindow after added.window) panes <- require "New window panes" (Tmux.panesForWindow after added.window) splitPane <- require "Split pane" (Tmux.lookupPane after split.pane) links <- require "Created session links" (Tmux.linksForSession after created.session) label <- require "Window label" (Tmux.labelText window.name) workerLabel <- require "New window label" (Tmux.labelText worker.name) putStrLn $ "created session: " <> Text.unpack session.name putStrLn "session environment: empty, removed, absent" putStrLn $ "initial window: " <> Text.unpack label putStrLn $ "pane title: " <> Text.unpack pane.title putStrLn $ "same created targets: " <> show (session.ref == created.session && window.ref == created.window && pane.ref == created.pane) putStrLn $ "captured sessions: " <> show (length (Tmux.sessions before)) <> " -> " <> show (length (Tmux.sessions after)) putStrLn $ "created session links: " <> show (length links) putStrLn $ "new window: " <> Text.unpack workerLabel putStrLn $ "new window panes: " <> show (length panes) putStrLn $ "same worker window at indices: " <> show workerIndices putStrLn $ "split exact target: " <> show (splitPane.window == added.window && split.session == added.session && split.windowIndex == added.windowIndex) putStrLn $ "captured pane rows match height: " <> show (toInteger (length (Text.lines (Tmux.paneText screen))) == Tmux.dimensionValue splitPane.height) putStrLn "captured pane text" putStr (Text.unpack (Text.unlines (filter (not . Text.null) (Text.lines (Tmux.paneText screen))))) putStrLn $ "duplicate name rejected: " <> show rejected putStrLn $ "original sessions after removal: " <> show (map (.ref) (Tmux.sessions remaining) == map (.ref) (Tmux.sessions before)) putStrLn "server retired"
awaitText :: Tmux.Server -> Tmux.PaneRef -> Tmux.PaneWaitOptions -> Text.Text -> IO Tmux.PaneTextawaitText server pane options expected = Tmux.waitForText server pane options expected >>= \case Tmux.PaneTextMatched screen -> pure screen Tmux.PaneTextTimedOut _ _ -> die "Timed out waiting for pane text" Tmux.PaneTextExited _ -> die "Pane exited before producing text"
require :: Show problem => String -> Either problem a -> IO arequire context = either (die . ((context <> ": ") <>) . show) pureProgram output:
history-limit kind: NumberKindafter-rename-window scopes: [SessionScope]created session: created-workspacesession environment: empty, removed, absentinitial window: apipane title: editor-readysame created targets: Truecaptured sessions: 0 -> 1created session links: 2new window: workernew window panes: 2same worker window at indices: [1,4]split exact target: Truecaptured pane rows match height: Truecaptured pane textinput-readyEnterduplicate name rejected: Trueoriginal sessions after removal: Trueserver retiredLink, select and arrange windows
linkWindow takes a source slot and a vacant
destination slot. The new slot points to the same physical window and starts
unselected. Use selectWindowLink to activate a
specific session/index, even when the same window appears at two indices.
selectExactPane takes a window slot and an exact
pane reference. The pane must belong to that slot’s current window.
selectLayout applies one of the five layout names
shared by tmux 3.3a and 3.8. It affects the physical window seen through every
link and can unzoom it. Capture again to read the active pane and geometry.
unlinkWindow removes only the named link and
refuses to remove the last one. Native session groups can propagate link
changes to their members. Use the With variants for explicit deadlines.
Remove a target
Use killSession for a session,
killWindow for a physical window and all its
links, and killPane for a pane. tmux closes a
window when its last pane is removed, and a session when its last window
closes. Capture again to see what remains.
The With variants take MutationOptions.
A missing native target fails with MutationRejected. A foreign or offline
reference fails with InvalidTarget before dispatch. Command completion
confirms the removal response. Observe descendant process exit separately.
Use killServer to retire the daemon and await its exit. The example calls
it after the last capture so its detached result stays available.
Explicit server retirement
Options and process modes
Start from defaultSessionOptions. Import
LibTmux.Creation qualified for option updates, as the program does; several
observation types also have fields named name, width and height.
Session and window names and the working directory stay literal through tmux
format parsing. Checked dimensions must fit the native command’s range.
Environment entries override session variables; tmux’s configured inheritance
and update-environment rules still apply. Relative paths use the calling
client’s working directory. The library captures that OS context in IO as
part of the command deadline. Options omit
Show
because argv and environment values can contain credentials.
InitialProcess distinguishes native modes:
DefaultProcessuses tmux’s configured default command or default shell.ShellCommandpasses command bytes to tmux’s native creation handler. Session creation runs even an empty script through the default shell. An empty split command instead follows tmux’s processless-pane behavior.DirectCommandexecutes the program with a nonempty list of arguments. tmux treats a single argv entry as shell input, so the type requires at least one argument after the executable.
Session creation always requests an initial process. Call it inside an owned
or borrowed Server scope. Open a server
Window placement and pane targets
createWindow takes a
WindowPlacement and WindowOptions. Start from
defaultWindowOptions and choose NextFree session, At slot, Before slot
or After slot. A WindowSlot session index means the current occupant of
that position at execution time. It does not freeze a captured window identity.
On a vacant relative slot, tmux creates at that index without shifting links.
After refuses index 2147483647 before dispatch because no larger signed
window index exists.
The default RefuseOccupied policy reports a native rejection when an exact
slot is occupied. ReplaceOccupied removes that link before creating a new
window. Other links can keep the old physical window alive. Insertion can
shift indices and replacement can remove a link before a later failure; neither
mode promises rollback. Session groups can propagate these effects.
windowLabel and windowDirectory stay literal. Window creation inherits the
target session’s geometry; windowEnvironment and splitEnvironment override
only the new pane’s environment. windowProcess and splitProcess choose its
native initial process.
selectWindow = True requests selection in the target session. Replacing its current
link can force selection even when the option is false. Reuse by name remains
outside this API because native reuse can return no creation receipt.
splitPane takes a WindowSlot, an explicit
PaneRef and SplitOptions. The pane must belong to that slot’s physical
window when tmux executes the command. This also distinguishes two indices
linked to the same physical window. A missing pane or changed slot occupant
fails without choosing another active pane.
Start from defaultSplitOptions. LeftRight splits across columns;
TopBottom splits across rows. before puts the new pane left or above the
anchor; fullWindow spans the perpendicular window dimension. SplitCells
and SplitPercent request the new pane’s extent. Percentages are 1 through 100;
cells and link indices fit tmux’s signed 32-bit domain. Native border and
layout constraints can clamp or refuse a requested size. Capture again to
read actual dimensions. selectPane requests the new pane as active; zoom
zooms the window or keeps it zoomed. Floating-pane extensions remain separate.
Receipts and fresh observations
CreatedEntities is a historical receipt. It contains
opaque references from this Server generation, the execution-time window index
and the daemon PID reported with them. It is not a snapshot or a liveness check.
CreatedSession, CreatedWindow and CreatedPane name the results of each
operation. Only the session command creates all three entities; window creation
retains its existing parent session, and a split retains its existing window.
An after-hook can rename or destroy a target before creation returns. A receipt
also does not prove that the initial process successfully executed or that a
hook’s background work finished.
capture and the indexed
lookupSession, lookupWindow and lookupPane
read those identities as observed in the supplied new snapshot. A deleted
target returns a pure lookup error. References from an earlier generation
cannot target a reopened handle. A stale or deleted reference never silently
selects a replacement by name.
Failure, deadlines and recovery
createSessionWith, createWindowWith and splitPaneWith
accept MutationOptions.
Its positive timeout, at most ten seconds, covers context acquisition,
admission and the complete command response; the default is one second.
It does not bound the lifetime of the created entities or their processes.
Invalid options raise InvalidMutationArgument before admission. Encoded
command arguments fit a 16,364-byte message, including terminators and literal
text escaping. The client invocation also counts toward the argv limit.
The caller’s environment shares the native launch budget with argv: at most
4,096 entries and 64 KiB, including paths, terminators and pointer storage.
Exceeding that budget raises InvalidConfiguration before admission. Neither
refusal retains a creation operation or includes environment values.
tmux also validates its own input rules and current server state. A duplicate
session name raises
MutationRejected; a successful primary creation followed by hook failure
raises MutationAppliedWithErrors. Neither failure implies rollback of
independent hook effects. Timeout, cancellation and incomplete responses can
leave the command’s outcome unknown. Creation never automatically retries.
tryTmux preserves library errors and their exception
context. It lets asynchronous cancellation propagate. When catching cancellation
at an application boundary, retain the context from tryWithContext too.
Use creationRecoveries to find the correlated
operation owner rather than searching all Runtime operations.
For each owner, creationOperation and creationGeneration identify the
request. creationReceipt reads any complete IDs already observed without
contacting tmux. Nothing
does not prove that creation had no effect.
inspectCreationRecovery distinguishes logical outcome from native cleanup.
awaitCreationRecovery joins the original child and decoder;
resumeCreationRecovery resumes existing cleanup. Neither repeats creation.
If logical uncertainty remains, detachCreationRecovery explicitly relinquishes
that responsibility while preserving physical cleanup owners. It does not kill
the created entities. Handle any scope-level cleanup owners separately through
cleanupRecoveries. The
shared Runtime guide explains ownership across handles;
the mutation guide describes the common outcome vocabulary.