# Swift workspace builder examples

Source: https://libtmux.org/en/swift/latest/workspace/internals/examples/

> Internal examples for building and inspecting workspaces through the Swift API.

The port's [`ExampleCode`](https://github.com/libtmux/libtmux-swift/blob/f02a4668570e1cc5198c941413750e021f42c214/Examples/Package.swift) package contains functions for describing a workspace
in Swift, building it, and reading JSON or YAML. Its tests compile and call
those functions.

## Describe and build

The example's YAML reader requires the dependency's `YAMLWorkspaces` trait.
That trait enables the API inside the dependency; it does not define the same
compilation condition in a consumer package.

```swift file="Examples/Sources/ExampleCode/Workspaces.swift"
// The examples in the README's `WorkspaceBuilder` section.

import Foundation
import LibTmux
import TmuxWorkspace

public func describeAWorkspaceInSwift() -> Workspace {
    let workspace = Workspace(
        sessionName: "work",
        windows: [
            WindowPlan(
                windowName: "editor",
                layout: "even-horizontal",
                panes: [PanePlan(), PanePlan()]
            ),
            WindowPlan(
                windowName: "logs",
                panes: [PanePlan(shellCommands: ["tail -f /tmp/build.log"])]
            ),
        ]
    )
    return workspace
}

public func buildItOnAServer(_ server: Server, _ workspace: Workspace) async throws -> Session {
    let session = try await WorkspaceBuilder.build(workspace, on: server)
    print(session.name, session.windowCount)
    return session
}

public func readAWorkspaceWrittenAsJSON(_ json: Data) throws -> Workspace {
    try Workspace.decode(json: json)
}

// Deliberately NOT wrapped in `#if YAMLWorkspaces`. A trait defines its
// compilation condition only inside the package that declares it, so that guard
// in a consumer package is always false and deletes the example — loudly if a
// test calls it, silently if nothing does. The symbol is nonetheless here,
// because the dependency was resolved with the trait on.
public func readATmuxpFile(_ text: String) throws -> Workspace {
    try Workspace.decode(yaml: text)
}
```

The log pane expects a log file at the configured path. Change that command
to match the application before using the description as a launcher. The
returned session is a captured value; request a fresh server snapshot to
inspect membership after all windows have been created.

The [guide](../guides/) provides an isolated executable and cleanup. These
helper functions accept a caller-owned server and keep the workspace running
for that caller to use.

## Verification

From a prepared source checkout, run the example package:

```console
$ swift test --package-path Examples
```

The port also checks correspondence between these source functions and its
README examples. Source inclusion in this page keeps the excerpt current;
it does not run the Swift tests during site rendering.

[Example source](https://github.com/libtmux/libtmux-swift/blob/f02a4668570e1cc5198c941413750e021f42c214/Examples/Sources/ExampleCode/Workspaces.swift)
