Skip to content

Commit c7c1ac6

Browse files
committed
Update README.md
1 parent e544454 commit c7c1ac6

3 files changed

Lines changed: 57 additions & 45 deletions

File tree

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Add `expectrl` to your Cargo.toml.
2525
```toml
2626
# Cargo.toml
2727
[dependencies]
28-
expectrl = "0.7"
28+
expectrl = "0.8"
2929
```
3030

3131
An example where the program simulates a used interacting with `ftp`.
@@ -65,27 +65,29 @@ use expectrl::{
6565
fn main() -> Result<(), Error> {
6666
let mut p = expectrl::spawn("ftp bks4-speedtest-1.tele2.net")?;
6767

68-
let mut auth = false;
69-
let mut login_lookup = Lookup::new();
68+
let mut search = Lookup::new();
7069
let mut stdin = Stdin::open()?;
7170

72-
InteractSession::new(&mut p, &mut stdin, stdout(), &mut auth)
73-
.set_output_action(move |ctx| {
74-
if login_lookup
75-
.on(ctx.buf, ctx.eof, "Login successful")?
76-
.is_some()
77-
{
78-
**ctx.state = true;
71+
let authenticated = {
72+
let mut session = InteractSession::new(&mut p, &mut stdin, stdout(), false);
73+
session.set_output_action(move |ctx| {
74+
let found = search.on(ctx.buf, ctx.eof, "Login successful")?;
75+
if found.is_some() {
76+
*ctx.state = true;
7977
return Ok(true);
8078
}
8179

8280
Ok(false)
83-
})
84-
.spawn()?;
81+
});
82+
83+
session.spawn()?;
84+
85+
session.into_state()
86+
};
8587

8688
stdin.close()?;
8789

88-
if !auth {
90+
if !authenticated {
8991
println!("An authentication was not passed");
9092
return Ok(());
9193
}

examples/bash.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ fn main() {
1414

1515
// case 2: wait until done, only extract a few infos
1616
p.send_line("wc /etc/passwd").unwrap();
17-
// `exp_regex` returns both string-before-match and match itself, discard first
18-
let lines = p.expect(Regex("[0-9]+")).unwrap();
19-
let words = p.expect(Regex("[0-9]+")).unwrap();
20-
let bytes = p.expect(Regex("[0-9]+")).unwrap();
17+
// `expect` returns both string-before-match and match itself, discard first
18+
let found = p.expect(Regex("([0-9]+).*([0-9]+).*([0-9]+)")).unwrap();
19+
let lines = String::from_utf8_lossy(&found[1]);
20+
let words = String::from_utf8_lossy(&found[2]);
21+
let chars = String::from_utf8_lossy(&found[3]);
2122
p.expect_prompt().unwrap(); // go sure `wc` is really done
2223
println!(
2324
"/etc/passwd has {} lines, {} words, {} chars",
24-
String::from_utf8_lossy(&lines[0]),
25-
String::from_utf8_lossy(&words[0]),
26-
String::from_utf8_lossy(&bytes[0]),
25+
lines, words, chars,
2726
);
2827

2928
// case 3: read while program is still executing
@@ -43,7 +42,7 @@ fn main() {
4342
use expectrl::AsyncExpect;
4443
use futures_lite::io::AsyncBufReadExt;
4544

46-
futures_lite::future::block_on(async {
45+
let f = async {
4746
let mut p = spawn_bash().await.unwrap();
4847

4948
// case 1: wait until program is done
@@ -55,16 +54,18 @@ fn main() {
5554

5655
// case 2: wait until done, only extract a few infos
5756
p.send_line("wc /etc/passwd").await.unwrap();
58-
// `exp_regex` returns both string-before-match and match itself, discard first
59-
let lines = p.expect(Regex("[0-9]+")).await.unwrap();
60-
let words = p.expect(Regex("[0-9]+")).await.unwrap();
61-
let bytes = p.expect(Regex("[0-9]+")).await.unwrap();
57+
// `expect` returns both string-before-match and match itself, discard first
58+
let found = p
59+
.expect(Regex("([0-9]+).*([0-9]+).*([0-9]+)"))
60+
.await
61+
.unwrap();
62+
let lines = String::from_utf8_lossy(&found[1]);
63+
let words = String::from_utf8_lossy(&found[2]);
64+
let chars = String::from_utf8_lossy(&found[3]);
6265
p.expect_prompt().await.unwrap(); // go sure `wc` is really done
6366
println!(
6467
"/etc/passwd has {} lines, {} words, {} chars",
65-
String::from_utf8_lossy(lines.get(0).unwrap()),
66-
String::from_utf8_lossy(words.get(0).unwrap()),
67-
String::from_utf8_lossy(bytes.get(0).unwrap()),
68+
lines, words, chars,
6869
);
6970

7071
// case 3: read while program is still executing
@@ -79,10 +80,12 @@ fn main() {
7980
}
8081

8182
p.send(ControlCode::EOT).await.unwrap();
82-
})
83+
};
84+
85+
futures_lite::future::block_on(f);
8386
}
8487

8588
#[cfg(windows)]
8689
fn main() {
87-
panic!("An example doesn't supported on windows")
90+
panic!("An example is not supported on windows")
8891
}

examples/check.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use expectrl::{check, spawn, Error, Expect};
22

33
#[cfg(not(feature = "async"))]
44
fn main() {
5-
let mut p = spawn("python ./tests/source/ansi.py").expect("Can't spawn a session");
5+
let mut p = spawn("python ./tests/source/ansi.py").unwrap();
66

77
loop {
8-
match check!(
8+
let result = check! {
99
&mut p,
1010
_ = "Password: " => {
1111
println!("Set password to SECURE_PASSWORD");
@@ -15,9 +15,12 @@ fn main() {
1515
println!("Stop processing");
1616
p.send_line("n").unwrap();
1717
},
18-
) {
18+
};
19+
20+
match result {
21+
Ok(_) => {}
1922
Err(Error::Eof) => break,
20-
result => result.unwrap(),
23+
Err(_) => result.unwrap(),
2124
};
2225
}
2326
}
@@ -26,26 +29,30 @@ fn main() {
2629
fn main() {
2730
use expectrl::AsyncExpect;
2831

29-
futures_lite::future::block_on(async {
30-
let mut session = spawn("python ./tests/source/ansi.py").expect("Can't spawn a session");
32+
let f = async {
33+
let mut session = spawn("python ./tests/source/ansi.py").unwrap();
3134

3235
loop {
33-
match check!(
34-
&mut session,
36+
let result = check! {
37+
&mut p,
3538
_ = "Password: " => {
3639
println!("Set password to SECURE_PASSWORD");
37-
session.send_line("SECURE_PASSWORD").await.unwrap();
40+
p.send_line("SECURE_PASSWORD").await.unwrap();
3841
},
3942
_ = "Continue [y/n]:" => {
4043
println!("Stop processing");
41-
session.send_line("n").await.unwrap();
44+
p.send_line("n").await.unwrap();
4245
},
43-
)
44-
.await
45-
{
46+
}
47+
.await;
48+
49+
match result {
50+
Ok(_) => {}
4651
Err(Error::Eof) => break,
47-
result => result.unwrap(),
52+
Err(_) => result.unwrap(),
4853
};
4954
}
50-
})
55+
};
56+
57+
futures_lite::future::block_on(f);
5158
}

0 commit comments

Comments
 (0)