NewWindowOptions
Options for creating a window in a session.
Source and package
- Qualified name
-
libtmux::session:: New Window Options - Module
- session
- Package
- libtmux
- Source
- crates/libtmux/src/session.rs
-
Description#
A bare name is accepted wherever this is:
session.new_window("editor").Examples#
use libtmux::NewWindowOptions;let guard = libtmux::test::TestServer::new().await?;let session = guard.server().new_session("work").await?;let window = session.new_window(NewWindowOptions::new("editor").environment("EDITOR", "vi")).await?;assert_eq!(window.name().to_string_lossy(), "editor");// A bare name works too, because `new_window` takes anything that converts.session.new_window("logs").await?;assert_eq!(session.windows().await?.len(), 3);guard.shutdown().await?;Create windows and split panes. Create a session and a second window, then split from a window and from a pane. 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,timeReplace
src/main.rswith 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//! Create a session and window, then split a window and a pane.use std::error::Error;use std::time::Duration;use libtmux::{NewSessionOptions, NewWindowOptions, Server, SplitDirection, SplitOptions};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> {let session = server.new_session(NewSessionOptions::new("work").window_name("editor").command("cat"),).await?;let window = session.new_window(NewWindowOptions::new("logs").command("cat")).await?;let pane = window.split(SplitOptions::new(SplitDirection::Below).command("cat")).await?;pane.split(SplitOptions::new(SplitDirection::Right).command("cat")).await?;check(session.windows().await?.len() == 2,"expected two session windows",)?;check(window.panes().await?.len() == 3,"expected three panes in split window",)?;check(server.panes().await?.len() == 4,"expected four panes on server",)?;println!("creation: 2 windows, 4 panes");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-creation-").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 --quietExpected output:
creation: 2 windows, 4 panes
Members9#
- command method Run a command instead of the default shell.
- environment method Set an environment variable for the process the new window starts.
- index method Place the window at a window index rather than the first free one.
- new method Describe a window with the given name, sent literally.
- placement method Insert relative to the index rather than at it.
- replace_existing method Replace whatever already occupies the target index.
- select method Make the new window active in its session.
- start_directory method Set the window's working directory.
- unnamed method Describe a window with no name, letting tmux choose one.