pub struct CommandChain { /* private fields */ }Expand description
Several tmux commands dispatched as one tmux a \; b invocation.
tmux reads a bare ; argv element as a command boundary and a \; element
as a literal semicolon. Command lowers every trailing ; a caller
supplies, so a boundary cannot come from an argument; it comes from this
type, which owns the separator. That is what makes a chain safe to build
from untrusted values.
A chain is one dispatch: one process, one exit status, one merged stdout. tmux runs the sequence up to the first failure and drops the remainder, and the merged result is the same whichever member failed, so a chain reports one outcome rather than one per command. Use it to cut round trips when the commands succeed or fail as a unit; dispatch separately when you need to know which one failed.
§Examples
use libtmux::{Command, CommandChain};
let chain = CommandChain::new(Command::new("send-keys").arg("-t").arg("%1"))
.then(Command::new("rename-window").arg("-t").arg("@1").arg("edit"));
assert_eq!(chain.command_count(), 2);
assert_eq!(
chain.summary().to_string(),
r#""send-keys" "-t" "%1" ; "rename-window" "-t" "@1" "edit""#,
);A literal semicolon stays an argument, and renders quoted so it is not mistaken for the boundary beside it:
use libtmux::{Command, CommandChain};
let chain = CommandChain::new(Command::new("display-message").arg(";"))
.then(Command::new("list-sessions"));
assert_eq!(
chain.summary().to_string(),
r#""display-message" ";" ; "list-sessions""#,
);The first command is stored apart from the rest so “a chain is never empty” is a property of the type rather than an invariant to remember, which is what keeps every method here free of a panicking path.
Implementations§
Source§impl CommandChain
impl CommandChain
Sourcepub fn new(command: Command) -> Self
pub fn new(command: Command) -> Self
Start a chain from its first command.
Taking the first command here keeps a chain non-empty by construction: an empty argv would make tmux act as a client rather than run anything.
§Examples
use libtmux::{Command, CommandChain};
let chain = CommandChain::new(Command::new("list-sessions"));
assert_eq!(chain.command_count(), 1);Sourcepub fn then(self, command: Command) -> Self
pub fn then(self, command: Command) -> Self
Append one command, to run after the previous one succeeds.
§Examples
use libtmux::{Command, CommandChain};
let chain = CommandChain::new(Command::new("select-pane").arg("-m"))
.then(Command::new("select-pane").arg("-M"));
assert_eq!(chain.command_count(), 2);Sourcepub fn command_count(&self) -> usize
pub fn command_count(&self) -> usize
Return the number of commands in the chain, always at least one.
§Examples
use libtmux::{Command, CommandChain};
let chain = CommandChain::new(Command::new("list-panes"));
assert_eq!(chain.command_count(), 1);Sourcepub fn summary(&self) -> CommandSummary
pub fn summary(&self) -> CommandSummary
Build a bounded, sanitized summary of the whole chain.
Separators render bare and are counted as neither public nor sensitive arguments. Every other token of every member, including the subcommands after the first, counts as an argument of the summary.
§Examples
use libtmux::{Command, CommandChain};
let summary = CommandChain::new(Command::new("select-pane").arg("-m"))
.then(Command::new("select-pane").arg("-M"))
.summary();
assert_eq!(summary.to_string(), r#""select-pane" "-m" ; "select-pane" "-M""#);
assert_eq!(summary.public_argument_count(), 3);