expectrl/interact/mod.rs
1//! This module contains a routines for running and utilizing an interacting session with a [`Session`].
2//!
3#![cfg_attr(all(unix, not(feature = "async")), doc = "```no_run")]
4#![cfg_attr(not(all(unix, not(feature = "async"))), doc = "```ignore")]
5//! use expectrl::{
6//! interact::actions::lookup::Lookup,
7//! spawn, stream::stdin::Stdin, Regex
8//! };
9//!
10//! #[derive(Debug)]
11//! enum Answer {
12//! Yes,
13//! No,
14//! Unrecognized,
15//! }
16//!
17//! let mut session = spawn("cat").expect("Can't spawn a session");
18//!
19//! let mut input_action = Lookup::new();
20//!
21//! let mut stdin = Stdin::open().unwrap();
22//! let stdout = std::io::stdout();
23//!
24//! let mut term = session.interact(&mut stdin, stdout).with_state(Answer::Unrecognized);
25//! term.set_input_action(move |mut ctx| {
26//! let m = input_action.on(ctx.buf, ctx.eof, "yes")?;
27//! if m.is_some() {
28//! *ctx.state = Answer::Yes;
29//! };
30//!
31//! let m = input_action.on(ctx.buf, ctx.eof, "no")?;
32//! if m.is_some() {
33//! *ctx.state = Answer::No;
34//! };
35//!
36//! Ok(false)
37//! });
38//!
39//! term.spawn().expect("Failed to run an interact session");
40//!
41//! let answer = term.into_state();
42//!
43//! stdin.close().unwrap();
44//!
45//! println!("It was said {:?}", answer);
46//! ```
47//!
48//! [`Session`]: crate::session::Session
49
50pub mod actions;
51mod context;
52mod session;
53
54pub use context::Context;
55pub use session::InteractSession;