A tool for automating terminal applications in Unix.
Using the library you can:
It was inspired by philippkeller/rexpect and pexpect.
It supports async
calls. To enable them you must turn on an async
feature.
```rust use expectrl::{spawn, Regex, Eof, WaitStatus};
fn main() { let mut p = spawn("ftp speedtest.tele2.net").unwrap(); p.expect(Regex("Name \(.*\):")).unwrap(); p.sendline("anonymous").unwrap(); p.expect("Password").unwrap(); p.sendline("test").unwrap(); p.expect("ftp>").unwrap(); p.sendline("cd upload").unwrap(); p.expect("successfully changed.\r\nftp>").unwrap(); p.sendline("pwd").unwrap(); p.expect(Regex("[0-9]+ \"/upload\"")).unwrap(); p.sendline("exit").unwrap(); p.expect(Eof).unwrap(); asserteq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0)); } ```
async
feature```rust use expectrl::{repl::spawnbash, Regex, Error, ControlCode}; use futureslite::io::AsyncBufReadExt;
fn main() -> Result<(), Error> { let mut p = spawn_bash().await?;
p.send_line("hostname").await?;
let mut hostname = String::new();
p.read_line(&mut hostname).await?;
p.expect_prompt().await?; // go sure `hostname` is really done
println!("Current hostname: {:?}", hostname);
Ok(())
} ```
One frequent bitfall with sending signals is that you need
to somehow ensure that the program has fully loaded, otherwise they
goes into nowhere. There are 2 handy function execute
for this purpouse:
execute
- does a command and ensures that the prompt is shown again.expect_prompt
- ensures that the prompt is shown.```rust use expectrl::{repl::spawn_bash, Error, ControlCode};
fn main() -> Result<(), Error> { let mut p = spawnbash()?; p.sendline("ping 8.8.8.8")?; p.expect("bytes of data")?; p.sendcontrol(ControlCode::Substitute)?; // CTRLZ p.expectprompt()?; // bash writes 'ping 8.8.8.8' to stdout again to state which job was put into background p.sendline("bg")?; p.expect("ping 8.8.8.8")?; p.expectprompt()?; p.sendline("sleep 0.5")?; p.expectprompt()?; // bash writes 'ping 8.8.8.8' to stdout again to state which job was put into foreground p.sendline("fg")?; p.expect("ping 8.8.8.8")?; p.send_control(ControlCode::EndOfText)?; p.expect("packet loss")?;
Ok(())
} ```
For more examples, check the examples directory.
It will be fair to say that without it there would be no expectrl
.
async
support.Licensed under MIT License