InteractBuilder

Struct InteractBuilder 

Source
pub struct InteractBuilder<'a, T>
where T: AsyncReadExt + AsyncWriteExt + Unpin + Send + 'static,
{ /* private fields */ }
Expand description

Builder for configuring interactive sessions.

Implementations§

Source§

impl<'a, T> InteractBuilder<'a, T>
where T: AsyncReadExt + AsyncWriteExt + Unpin + Send + 'static,

Source

pub fn on_output<F>(self, pattern: impl Into<Pattern>, callback: F) -> Self
where F: Fn(&InteractContext<'_>) -> InteractAction + Send + Sync + 'static,

Register a pattern hook for output.

When the output matches the pattern, the callback is invoked.

§Example
session.interact()
    .on_output("password:", |ctx| {
        ctx.send("my_password\n")
    })
    .start()
    .await?;
Source

pub fn on_input<F>(self, pattern: impl Into<Pattern>, callback: F) -> Self
where F: Fn(&InteractContext<'_>) -> InteractAction + Send + Sync + 'static,

Register a pattern hook for input.

When the input matches the pattern, the callback is invoked.

Source

pub fn on_resize<F>(self, callback: F) -> Self
where F: Fn(&ResizeContext) -> InteractAction + Send + Sync + 'static,

Register a hook for terminal resize events.

On Unix systems, this is triggered by SIGWINCH. The callback receives the new terminal size and can optionally return an action.

§Example
session.interact()
    .on_resize(|ctx| {
        println!("Terminal resized to {}x{}", ctx.size.cols, ctx.size.rows);
        InteractAction::Continue
    })
    .start()
    .await?;
§Platform Support
  • Unix: Resize events are detected via SIGWINCH signal handling.
  • Windows: Resize detection is not currently supported; the callback will not be invoked.
Source

pub const fn with_mode(self, mode: InteractionMode) -> Self

Set the interaction mode.

Source

pub fn with_escape(self, escape: impl Into<Vec<u8>>) -> Self

Set the escape sequence to exit interact mode.

Default is Ctrl+] (0x1d).

Source

pub fn no_escape(self) -> Self

Disable the escape sequence (interact runs until pattern stops it).

Source

pub const fn with_timeout(self, timeout: Duration) -> Self

Set a timeout for the interaction.

Source

pub const fn with_buffer_size(self, size: usize) -> Self

Set the output buffer size.

Source

pub fn with_input_hook<F>(self, hook: F) -> Self
where F: Fn(&[u8]) -> Vec<u8> + Send + Sync + 'static,

Add a byte-level input hook.

Source

pub fn with_output_hook<F>(self, hook: F) -> Self
where F: Fn(&[u8]) -> Vec<u8> + Send + Sync + 'static,

Add a byte-level output hook.

Source

pub async fn start(self) -> Result<InteractResult>

Start the interactive session.

This runs the interaction loop, reading from stdin and the session, checking patterns, and invoking callbacks when matches occur.

The interaction continues until:

  • A pattern callback returns InteractAction::Stop
  • The escape sequence is detected
  • A timeout occurs (if configured)
  • EOF is reached on the session
§Errors

Returns an error if I/O fails or a pattern callback returns an error.

Auto Trait Implementations§

§

impl<'a, T> Freeze for InteractBuilder<'a, T>

§

impl<'a, T> !RefUnwindSafe for InteractBuilder<'a, T>

§

impl<'a, T> Send for InteractBuilder<'a, T>

§

impl<'a, T> Sync for InteractBuilder<'a, T>

§

impl<'a, T> Unpin for InteractBuilder<'a, T>

§

impl<'a, T> !UnwindSafe for InteractBuilder<'a, T>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V