Core Library0.xExamples

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.

MoreLess about this notice
Edit this page on GitHub

Capture pane output

Needs libtmux for Rust 0.x Install

A pane runs asynchronously: sending a command does not mean its output is already on screen. Capture repeatedly until the expected line appears, with a deadline so a failed command cannot leave the program waiting forever.

This complete program creates a private tmux server, captures its output, and cleans up. Follow the setup and run instructions below. You need tmux 3.3a or newer and a Unix environment; no existing tmux session is required.

Read what’s on screen

The program sends printf with a leading newline, then waits for the complete line libtmux capture ready. The newline keeps a late shell prompt off that line. Matching the whole line avoids mistaking the echoed command for its output.

src/main.rsComplete program
use std::error::Error;
use std::time::Duration;
use libtmux::{NewSessionOptions, Server};
use tokio::time::{sleep, timeout};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let directory = tempfile::tempdir()?;
let server = Server::builder()
.socket_path(directory.path().join("tmux.sock"))
.config_file("/dev/null")
.build()?;
let captured = timeout(Duration::from_secs(5), async {
let session = server
.new_session(NewSessionOptions::new("capture").command("env ENV=/dev/null sh"))
.await?;
let panes = session.panes().await?;
let pane = panes.first().ok_or("the session has no pane")?;
pane.send_line("printf '\\nlibtmux capture ready\\n'").await?;
loop {
let lines = pane.capture().await?;
if let Some(line) = lines
.iter()
.find(|line| line.as_bytes() == b"libtmux capture ready")
{
println!("{}", line.to_string_lossy());
return Ok::<_, Box<dyn Error>>(());
}
sleep(Duration::from_millis(20)).await;
}
})
.await;
let killed = server.kill().await;
let closed = server.shutdown().await;
captured??;
killed?;
closed?;
Ok(())
}

Wait for output or completion

The program above checks the captured screen for up to five seconds. The short pause between checks limits polling; the observed output determines when the loop finishes. A tmux capture is a view of the screen and scrollback, so it can miss output that has already scrolled away. Use a stream or a completion signal for long-running commands when that distinction matters.

Capturing output covers capture options, while Sending keys explains why sending and waiting are separate operations.

Setup and run

Use an empty directory. Cargo installs libtmux 0.1.2 from crates.io.

Save the program as src/main.rs and this file as Cargo.toml. Use Rust 1.97.1 with rustup.

Cargo.toml
[package]
name = "capture-example"
version = "0.1.0"
edition = "2024"
[dependencies]
libtmux = "=0.1.2"
tempfile = "3.27.0"
tokio = { version = "1.53.1", features = ["macros", "rt-multi-thread", "time"] }
Terminal window
$ cargo +1.97.1 run

Where this comes from

This program uses libtmux 0.1.2 from crates.io. Its source is on GitHub.

Esc
Search in

Type to search.

Reference previewOpen reference