Core Library0.xAPI Reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

function libtmux-go latest

NewServer

NewServer validates and snapshots one tmux configuration without starting tmux. Empty socket selectors use the endpoint the frozen environment selects, as ServerOptions describes.

Source and package
Qualified name
tmux.NewServer
Package
tmux
Distribution
github.com/libtmux/libtmux-go/tmux
Source
tmux/server_options.go

Examples#

Construct a server handle for a private socket and check that construction does not start tmux. Creating the first session starts the daemon. The temporary server is stopped even when an operation fails. Use Go 1.26 or newer and tmux 3.3a or newer on a Unix system, with tmux and cat on PATH. In a new directory, create a module and add the library from the Go module proxy:

Terminal window
$ go mod init example.com/libtmux-api && \
go get github.com/libtmux/libtmux-go@latest

Save this complete example as example_test.go. It creates a private temporary socket and stops its server before removing the directory. Cleanup failure fails the example and retains the socket directory. View source

package tmux_test
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/libtmux/libtmux-go/tmux"
)
// Construct a server handle for a private socket and check that construction
// does not start tmux. Creating the first session starts the daemon.
// The temporary server is stopped even when an operation fails.
func ExampleNewServer_complete() {
run := func() (runErr error) {
directory, err := os.MkdirTemp("", "libtmux-go-api-")
if err != nil {
return fmt.Errorf("create socket directory: %w", err)
}
server, err := tmux.NewServer(tmux.ServerOptions{
SocketPath: filepath.Join(directory, "tmux.sock"),
ConfigFile: "/dev/null",
})
if err != nil {
return errors.Join(err, os.RemoveAll(directory))
}
defer func() {
// Cleanup has its own deadline because the operation may have timed out.
cleanup, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Kill(cleanup); err != nil && !errors.Is(err, tmux.ErrNoServer) {
runErr = errors.Join(runErr, fmt.Errorf("stop server at %s: %w", directory, err))
return
}
runErr = errors.Join(runErr, os.RemoveAll(directory))
}()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.CheckAlive(ctx); !errors.Is(err, tmux.ErrNoServer) {
return errors.Join(errors.New("expected no daemon before session creation"), err)
}
fmt.Println("construction started tmux: false")
_, err = server.NewSession(ctx, tmux.NewSessionRequest{
Name: "work", WindowName: "editor", Command: "cat", Width: 100, Height: 30,
})
if err != nil {
return fmt.Errorf("create session: %w", err)
}
if err := server.CheckAlive(ctx); err != nil {
return fmt.Errorf("check created server: %w", err)
}
fmt.Println("session started tmux: true")
return nil
}
if err := run(); err != nil {
panic(err)
}
// Output:
// construction started tmux: false
// session started tmux: true
}

Run the saved file. Go checks the Output comment and fails on different output or an operation or cleanup error:

Terminal window
$ go test -count=1 -v example_test.go
Parameters

Guides#

Pages that discuss NewServer.

Esc

Type to search.

Reference previewOpen reference