rust_expect/
session.rs

1//! Session module for managing spawned process interactions.
2//!
3//! This module provides the core session types and functionality for
4//! interacting with spawned processes, including the session handle,
5//! builder, lifecycle management, and screen buffer integration.
6//!
7//! # Overview
8//!
9//! The [`Session`] type is the main entry point for interacting with
10//! terminal applications. It provides methods for:
11//!
12//! - Spawning processes with [`Session::spawn`]
13//! - Sending input with [`Session::send`], [`Session::send_line`]
14//! - Expecting output with [`Session::expect`], [`Session::expect_any`]
15//! - Running dialogs with [`Session::run_dialog`]
16//!
17//! # Examples
18//!
19//! ## Basic Usage
20//!
21//! ```ignore
22//! use rust_expect::Session;
23//!
24//! #[tokio::main]
25//! async fn main() -> Result<(), rust_expect::ExpectError> {
26//!     // Spawn a bash shell
27//!     let mut session = Session::spawn("/bin/bash", &[]).await?;
28//!
29//!     // Wait for the prompt
30//!     session.expect("$ ").await?;
31//!
32//!     // Send a command
33//!     session.send_line("echo 'Hello, World!'").await?;
34//!
35//!     // Expect the output
36//!     session.expect("Hello, World!").await?;
37//!
38//!     // Clean exit
39//!     session.send_line("exit").await?;
40//!     Ok(())
41//! }
42//! ```
43//!
44//! ## Using the Builder
45//!
46//! ```ignore
47//! use rust_expect::SessionBuilder;
48//! use std::time::Duration;
49//!
50//! #[tokio::main]
51//! async fn main() -> Result<(), rust_expect::ExpectError> {
52//!     let mut session = SessionBuilder::new()
53//!         .command("/bin/bash")
54//!         .args(&["-l"])
55//!         .timeout(Duration::from_secs(30))
56//!         .dimensions(120, 40)
57//!         .env("TERM", "xterm-256color")
58//!         .spawn()
59//!         .await?;
60//!
61//!     session.expect("$ ").await?;
62//!     Ok(())
63//! }
64//! ```
65//!
66//! ## Multi-Pattern Matching
67//!
68//! ```ignore
69//! use rust_expect::{Session, Pattern, PatternSet};
70//! use std::time::Duration;
71//!
72//! #[tokio::main]
73//! async fn main() -> Result<(), rust_expect::ExpectError> {
74//!     let mut session = Session::spawn("/bin/bash", &[]).await?;
75//!
76//!     // Create a pattern set with multiple options
77//!     let mut patterns = PatternSet::new();
78//!     patterns
79//!         .add(Pattern::literal("$ "))
80//!         .add(Pattern::literal("# "))
81//!         .add(Pattern::timeout(Duration::from_secs(5)));
82//!
83//!     // Expect any of the patterns
84//!     let result = session.expect_any(&patterns).await?;
85//!     println!("Matched: {}", result.matched);
86//!     Ok(())
87//! }
88//! ```
89
90mod builder;
91mod handle;
92mod lifecycle;
93mod screen;
94
95pub use builder::{QuickSession, SessionBuilder};
96pub use handle::{Session, SessionExt};
97pub use lifecycle::{
98    LifecycleCallback, LifecycleEvent, LifecycleManager, ShutdownConfig, ShutdownStrategy, Signal,
99};
100pub use screen::{Cell, CellAttributes, Color, Position, Region, ScreenBuffer};