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