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:
- Spawning processes with
Session::spawn - Sending input with
Session::send,Session::send_line - Expecting output with
Session::expect,Session::expect_any - Running dialogs with
Session::run_dialog
§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.
- Cell
Attributes - Text attributes for a cell.
- Lifecycle
Manager - Manager for session lifecycle events.
- Position
- Screen position (row, column).
- Quick
Session - Quick session configuration for common use cases.
- Region
- A rectangular region of the screen.
- Screen
Buffer - A simple screen buffer for terminal content.
- Session
- A session handle for interacting with a spawned process.
- Session
Builder - Builder for creating session configurations.
- Shutdown
Config - Configuration for session shutdown.
Enums§
- Color
- Color representation.
- Lifecycle
Event - Lifecycle events that can occur during a session.
- Shutdown
Strategy - Shutdown strategy for closing a session.
- Signal
- Signals that can be sent to a process.
Traits§
- Session
Ext - Extension trait for session operations.
Type Aliases§
- Lifecycle
Callback - Callback type for lifecycle events.