Use the Swift workspace builder
TmuxWorkspace 0.1.1 · Source
Start with the complete runnable example. It includes the SwiftPM manifest, a pinned library dependency, imports, an executable entry point, a private tmux server, and cleanup.
Describe the layout¶
A Workspace contains ordered windows. Each WindowPlan describes a window’s
name, layout and panes. Its PanePlan values describe commands to send and an
optional starting directory.
The example keeps panes open with /bin/cat, so it needs no editor, application
or log file. Replace those choices with commands appropriate for the workspace
before using it as an application launcher.
Build on a running server¶
WorkspaceBuilder.build(_:on:) checks the server’s sessions before creating
its workspace. Start a private bootstrap session first, or pass a running
server whose lifetime the application owns.
The builder rejects an existing session with the requested name. After a
creation failure, it attempts to remove the session it created. Inspect
WorkspaceBuilderError.rollbackFailed when both the operation and its rollback
fail; that error preserves both causes.
The example’s final cleanup stops its whole private server, including the bootstrap session. An application that wants the workspace to remain open should retain its selected server and stop it when the application is done.
Inspect the completed layout¶
Request a fresh Server.snapshot() after building. The returned session value
was captured when the first window was created; it does not become a live view
as later windows are added.
Use the snapshot’s windows(of:) and panes(of:) methods to inspect membership.
The complete example asserts the final window and pane counts before printing
its result.
Read configuration¶
Workspace.decode(json:) reads Foundation Data. The accepted model uses
session_name, windows, window_name and panes; unknown keys are ignored.
YAML input additionally requires the dependency’s YAMLWorkspaces trait and
Workspace.decode(yaml:). Enable that trait on the package dependency when
using YAML. A dependency trait does not define the same compilation condition
inside a consumer package, so do not wrap the consumer call in an
#if YAMLWorkspaces guard.
Swift values and JSON need no YAML dependency. The workspace model and build contract describe the accepted fields and failure behavior.