Module session

Module session 

Source
Expand description

Session module for managing spawned process interactions.

This module provides the core session types and functionality for interacting with spawned processes, including the session handle, builder, lifecycle management, and screen buffer integration.

§Overview

The Session type is the main entry point for interacting with terminal applications. It provides methods for:

§Examples

§Basic Usage

use rust_expect::Session;

#[tokio::main]
async fn main() -> Result<(), rust_expect::ExpectError> {
    // Spawn a bash shell
    let mut session = Session::spawn("/bin/bash", &[]).await?;

    // Wait for the prompt
    session.expect("$ ").await?;

    // Send a command
    session.send_line("echo 'Hello, World!'").await?;

    // Expect the output
    session.expect("Hello, World!").await?;

    // Clean exit
    session.send_line("exit").await?;
    Ok(())
}

§Using the Builder

use rust_expect::SessionBuilder;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), rust_expect::ExpectError> {
    let mut session = SessionBuilder::new()
        .command("/bin/bash")
        .args(&["-l"])
        .timeout(Duration::from_secs(30))
        .dimensions(120, 40)
        .env("TERM", "xterm-256color")
        .spawn()
        .await?;

    session.expect("$ ").await?;
    Ok(())
}

§Multi-Pattern Matching

use rust_expect::{Session, Pattern, PatternSet};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), rust_expect::ExpectError> {
    let mut session = Session::spawn("/bin/bash", &[]).await?;

    // Create a pattern set with multiple options
    let mut patterns = PatternSet::new();
    patterns
        .add(Pattern::literal("$ "))
        .add(Pattern::literal("# "))
        .add(Pattern::timeout(Duration::from_secs(5)));

    // Expect any of the patterns
    let result = session.expect_any(&patterns).await?;
    println!("Matched: {}", result.matched);
    Ok(())
}

Structs§

Cell
A single cell in the screen buffer.
CellAttributes
Text attributes for a cell.
LifecycleManager
Manager for session lifecycle events.
Position
Screen position (row, column).
QuickSession
Quick session configuration for common use cases.
Region
A rectangular region of the screen.
ScreenBuffer
A simple screen buffer for terminal content.
Session
A session handle for interacting with a spawned process.
SessionBuilder
Builder for creating session configurations.
ShutdownConfig
Configuration for session shutdown.

Enums§

Color
Color representation.
LifecycleEvent
Lifecycle events that can occur during a session.
ShutdownStrategy
Shutdown strategy for closing a session.
Signal
Signals that can be sent to a process.

Traits§

SessionExt
Extension trait for session operations.

Type Aliases§

LifecycleCallback
Callback type for lifecycle events.