1use expectrl::repl::ReplSession;
2use std::io::Result;
3
4#[cfg(all(unix, not(feature = "async")))]
5fn main() -> Result<()> {
6 let mut p = expectrl::spawn("sh")?;
7 p.get_process_mut().set_echo(true, None)?;
8
9 let mut shell = ReplSession::new(p, String::from("sh-5.1$"), Some(String::from("exit")), true);
10
11 shell.expect_prompt()?;
12
13 let output = exec(&mut shell, "echo Hello World")?;
14 println!("{:?}", output);
15
16 let output = exec(&mut shell, "echo '2 + 3' | bc")?;
17 println!("{:?}", output);
18
19 Ok(())
20}
21
22#[cfg(all(unix, not(feature = "async")))]
23fn exec(shell: &mut ReplSession, cmd: &str) -> Result<String> {
24 let buf = shell.execute(cmd)?;
25 let mut string = String::from_utf8_lossy(&buf).into_owned();
26 string = string.replace("\r\n\u{1b}[?2004l\r", "");
27 string = string.replace("\r\n\u{1b}[?2004h", "");
28
29 Ok(string)
30}
31
32#[cfg(all(unix, feature = "async"))]
33fn main() -> Result<()> {
34 futures_lite::future::block_on(async {
35 let mut p = expectrl::spawn("sh")?;
36 p.get_process_mut().set_echo(true, None)?;
37
38 let mut shell =
39 ReplSession::new(p, String::from("sh-5.1$"), Some(String::from("exit")), true);
40
41 shell.expect_prompt().await?;
42
43 let output = exec(&mut shell, "echo Hello World").await?;
44 println!("{:?}", output);
45
46 let output = exec(&mut shell, "echo '2 + 3' | bc").await?;
47 println!("{:?}", output);
48
49 Ok(())
50 })
51}
52
53#[cfg(all(unix, feature = "async"))]
54async fn exec(shell: &mut ReplSession, cmd: &str) -> Result<String> {
55 let buf = shell.execute(cmd).await?;
56 let mut string = String::from_utf8_lossy(&buf).into_owned();
57 string = string.replace("\r\n\u{1b}[?2004l\r", "");
58 string = string.replace("\r\n\u{1b}[?2004h", "");
59
60 Ok(string)
61}
62
63#[cfg(windows)]
64fn main() {
65 panic!("An example doesn't supported on windows")
66}