Core LibraryExamples

Choose documentation 3

latest

Current version

latest
English

Prerelease This site documents an alpha of libtmux. Its structure, URLs and APIs are subject to change.

Edit this page on GitHub

Capture a pane

Create a private tmux server, capture the screen of a pane running /bin/cat, and keep the captured snapshot after the server has stopped.

Setup

Julia 1.13.0, Git, POSIX shell and tmux on Linux. Start in an empty directory. The following commands run in a POSIX shell.

Fetch the library revision used by this program.

Terminal window
$ git init -q source
$ git -C source fetch -q --depth=1 \
https://github.com/libtmux/libtmux-julia.git \
6f62bd72cb8d99f3759ca5e2cc3591165a03c768
$ git -C source checkout -q --detach FETCH_HEAD

Program

Save this complete program as owned_capture.jl.

owned_capture.jl
using LibTmux
function main()
endpoint = Ref{Server}()
result = with_server(; tmux=get(ENV, "LIBTMUX_TEST_TMUX", "tmux")) do server
endpoint[] = server
new_session(server; name="example", command=["/bin/cat"])
snap = snapshot(server)
pane = only(panes(snap))
screen = capture_pane(server, pane.ref)
@assert isempty(strip(screen))
@assert only(sessions(snap)).name == "example"
println("Captured ", string(pane.id), ": ", ncodeunits(screen), " screen bytes")
(; snap, screen)
end
@assert !ispath(dirname(endpoint[].socket_path))
@assert length(panes(result.snap)) == 1
result
end
main()

with_server owns the daemon and its temporary socket directory. The program asserts that the directory is gone after the block returns and that the captured snapshot still contains its pane.

The pane has no text. Its captured screen contains blank lines, so the byte count depends on its height; strip(screen) is empty.

Run

Terminal window
$ unset TMUX TMUX_PANE
$ export ENV=/dev/null BASH_ENV=/dev/null SHELL=/bin/sh
$ julia --startup-file=no --project=source owned_capture.jl

Expected output:

Captured %0: 24 screen bytes

Source and verification

The complete program comes from examples/owned_capture.jl. The verification runner executes these displayed files and setup commands unchanged, checks the output, and checks that the program stopped its private tmux server. Each example runs independently.

Esc

Type to search.

Reference preview Open reference