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.

method libtmux-rs latest

ServerBuilder.socket_path

Select an explicit tmux socket path, passed to tmux as -S.

Source and package
Qualified name
libtmux::server::builder::ServerBuilder::socket_path
Module
server.builder
Declared in
ServerBuilder
Package
libtmux
Source
crates/libtmux/src/server/builder.rs
#[must_use = "use the returned builder to retain the socket path" ] pub fn socket_path( mut self, path: impl Into<PathBuf> ) -> Self

Description#

Relative paths are joined to the working directory captured by ServerBuilder::build(). Naming a socket here leaves LIBTMUX_SOCKET_PATH and LIBTMUX_SOCKET unread.

Examples#

let server = libtmux::Server::builder()
.socket_path("/tmp/libtmux-rs-test/explicit-example.sock")
.build()?;
assert!(server.socket_path().is_absolute());

Construct a private server. Construct a handle without starting tmux, then create a session on its own socket. Use Rust 1.85 or newer with Cargo, and tmux 3.3a or newer, on a Unix system. In an empty directory, create a project and add the published crates:

Terminal window
$ cargo init --name libtmux-api-example &&
cargo add libtmux@0.1.2 tempfile@3.20 &&
cargo add tokio@1.40 --features macros,rt,time

Replace src/main.rs with this complete program. It owns a private tmux socket, applies a ten-second task deadline, and reports both task and cleanup errors. Server::kill() stops its daemon; Server::shutdown() closes its client executor. Successful cleanup removes the temporary directory even when the task fails. View source

//! Construct a server handle, then start its private daemon.
use std::error::Error;
use std::time::Duration;
use libtmux::{NewSessionOptions, Server};
type ExampleError = Box<dyn Error>;
fn check(condition: bool, message: &str) -> Result<(), ExampleError> {
if condition {
Ok(())
} else {
Err(message.into())
}
}
async fn demonstrate(server: &Server) -> Result<(), ExampleError> {
check(!server.is_alive().await, "construction started a daemon")?;
check(
server.config_file() == Some(std::path::Path::new("/dev/null")),
"wrong config path",
)?;
let session = server
.new_session(NewSessionOptions::new("work").command("cat"))
.await?;
check(
server.is_alive().await,
"new_session did not start a daemon",
)?;
check(session.name().as_bytes() == b"work", "wrong session name")?;
println!("construction: private daemon started by new_session");
Ok(())
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), ExampleError> {
let root = std::path::Path::new("/tmp/libtmux-rs-dev");
std::fs::create_dir_all(root)?;
let directory = tempfile::Builder::new()
.prefix("api-construction-")
.tempdir_in(root)?;
std::fs::write(
directory.path().join("owner"),
std::process::id().to_string(),
)?;
let server = Server::builder()
.socket_path(directory.path().join("tmux.sock"))
.config_file("/dev/null")
.default_timeout(Duration::from_secs(5))
.build()?;
let outcome = tokio::time::timeout(Duration::from_secs(10), demonstrate(&server)).await;
// Stop the owned daemon before closing the client executor.
let killed = server.kill().await;
let closed = server.shutdown().await;
let cleanup_failed = killed.is_err() || closed.is_err();
let mut failures = Vec::new();
match outcome {
Ok(Ok(())) => {}
Ok(Err(error)) => failures.push(format!("example failed: {error}")),
Err(error) => failures.push(format!("example deadline: {error}")),
}
if let Err(error) = killed {
failures.push(format!("daemon cleanup: {error}"));
}
if let Err(error) = closed {
failures.push(format!("executor cleanup: {error}"));
}
if cleanup_failed {
let retained = directory.keep();
failures.push(format!("inspect retained directory {}", retained.display()));
} else if let Err(error) = directory.close() {
failures.push(format!("directory cleanup: {error}"));
}
if failures.is_empty() {
Ok(())
} else {
Err(failures.join("; ").into())
}
}

Run the consumer. Success exits with status 0; setup, operation, deadline or cleanup failures exit unsuccessfully and print diagnostics to stderr:

Terminal window
$ cargo run --quiet

Expected output:

construction: private daemon started by new_session
Parameters
Esc

Type to search.

Reference previewOpen reference