Skip to main content

CommandChain

Struct CommandChain 

Source
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

Source

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);
Source

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);
Source

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);
Source

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);

Trait Implementations§

Source§

impl Clone for CommandChain

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CommandChain

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more