interact/
interact.rs

1//! To run an example run `cargo run --example interact`.
2
3use expectrl::{interact::InteractOptions, spawn, stream::stdin::Stdin};
4use std::io::stdout;
5
6#[cfg(unix)]
7const SHELL: &str = "sh";
8
9#[cfg(windows)]
10const SHELL: &str = "powershell";
11
12#[cfg(not(all(windows, feature = "polling")))]
13#[cfg(not(feature = "async"))]
14fn main() {
15    let mut sh = spawn(SHELL).expect("Error while spawning sh");
16
17    println!("Now you're in interacting mode");
18    println!("To return control back to main type CTRL-] combination");
19
20    let mut stdin = Stdin::open().expect("Failed to create stdin");
21
22    sh.interact(&mut stdin, stdout())
23        .spawn(&mut InteractOptions::default())
24        .expect("Failed to start interact");
25
26    stdin.close().expect("Failed to close a stdin");
27
28    println!("Exiting");
29}
30
31#[cfg(feature = "async")]
32fn main() {
33    futures_lite::future::block_on(async {
34        let mut sh = spawn(SHELL).expect("Error while spawning sh");
35
36        println!("Now you're in interacting mode");
37        println!("To return control back to main type CTRL-] combination");
38
39        let mut stdin = Stdin::open().expect("Failed to create stdin");
40
41        sh.interact(&mut stdin, stdout())
42            .spawn(&mut InteractOptions::default())
43            .await
44            .expect("Failed to start interact");
45
46        stdin.close().expect("Failed to close a stdin");
47
48        println!("Exiting");
49    });
50}
51
52#[cfg(all(windows, feature = "polling", not(feature = "async")))]
53fn main() {}