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.

Go API

method libtmux-go latest

Pane.Capture

Capture captures printed content from the receiver's exact linked pane. It returns a caller-owned slice. Completed stderr becomes a CommandError; any stdout tmux still printed is returned alongside it. A nonzero exit with empty stderr is not an error, as for every command (see the package documentation), and its stdout is returned.

Source and package
Qualified name
tmux.Pane.Capture
Package
tmux
Declared in
Pane
Distribution
github.com/libtmux/libtmux-go/tmux
Source
tmux/pane_capture.go
Other languages
Perlmethod Erlang PowerShell Julia Elixir Haskell Common Lisp PythonRubyLuaTypeScriptRustJavaC#C++SwiftKotlinScalaOCamlF#Clojure

Description#

This is a point-in-time capture rather than a stream; the zero request reads the visible screen. It may include a shell's echo of Pane.SendKeys input. Compare whole lines when polling; use ControlClient.NextNotification or Server.WaitFor when the application exposes a stream or explicit signal.

Invalid positions fail before execution. Transport and context failures return any partial stdout with the error.

Examples#

Capture the pane's history and visible screen as lines after sending text to cat. CaptureBoundary selects the beginning of history and the end of the screen. Poll whole lines because terminal echo may appear before the application handles input; this example checks visible text, not command completion. 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"
"slices"
"time"
"github.com/libtmux/libtmux-go/tmux"
)
// Capture the pane's history and visible screen as lines after sending text
// to cat. CaptureBoundary selects the beginning of history and the end of
// the screen. Poll whole lines because terminal echo may appear before the
// application handles input; this example checks visible text, not command
// completion.
func ExamplePane_Capture_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()
session, 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)
}
pane, err := session.ResolveActivePane(ctx)
if err != nil {
return fmt.Errorf("resolve pane: %w", err)
}
text := "api input"
if err := pane.SendKeys(ctx, tmux.SendKeysRequest{Command: &text, Literal: true}); err != nil {
return fmt.Errorf("send literal input: %w", err)
}
// Capture is a point-in-time read, so wait for a complete matching line.
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
lines, err := pane.Capture(ctx, tmux.CapturePaneRequest{
Start: tmux.CaptureBoundary, End: tmux.CaptureBoundary,
})
if err != nil {
return fmt.Errorf("capture pane: %w", err)
}
if slices.Contains(lines, "api input") {
fmt.Println("captured: api input")
break
}
select {
case <-ctx.Done():
return fmt.Errorf("wait for pane text: %w", ctx.Err())
case <-ticker.C:
}
}
return nil
}
if err := run(); err != nil {
panic(err)
}
// Output:
// captured: api input
}

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

tmux command#

The tmux commands behind Capture.

tmux C API#

The C declarations in tmux that implement these commands.

Guides#

Pages that discuss Capture.

In other ports#

Read a pane’s visible contents

Equivalents of Read a pane’s visible contents in other languages
Language Equivalent
Python libtmux.Pane.capture_pane
Ruby LibTmux::Pane#capture
Lua libtmux.Pane:capture
Erlang tmux_pane:capture/2
Haskell No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing.
TypeScript pane.Pane.capture
Rust pane.Pane.capture
Java io.github.libtmux.Pane.capture
Clojure libtmux.core/capture!
Kotlin io.github.libtmux.kotlin.Pane.capture
Scala io.github.libtmux.scaladsl.Pane.capture
C# LibTmux.Pane.CaptureAsync
F# LibTmux.FSharp.Pane.capture
C++ libtmux::Pane::capture
Swift Server.capture(_:includingHistory:)
OCaml Libtmux_unix.Client.capture_text
Perl LibTmux::Server::capture_pane Explicit Server operation. Captured entities remain local observations; use a new snapshot to observe changes.

No equivalent is recorded in PowerShell, Julia, Elixir or Common Lisp.

Esc

Type to search.

Reference previewOpen reference