expect_line/
expect_line.rs

1use expectrl::{self, Any, Eof, Expect};
2
3#[cfg(not(feature = "async"))]
4fn main() {
5    let mut p = expectrl::spawn("ls -al").expect("Can't spawn a session");
6
7    loop {
8        let m = p
9            .expect(Any::boxed(vec![
10                Box::new("\r"),
11                Box::new("\n"),
12                Box::new(Eof),
13            ]))
14            .expect("Expect failed");
15
16        println!("{:?}", String::from_utf8_lossy(m.as_bytes()));
17
18        let is_eof = m[0].is_empty();
19        if is_eof {
20            break;
21        }
22
23        if m[0] == [b'\n'] {
24            continue;
25        }
26
27        println!("{:?}", String::from_utf8_lossy(&m[0]));
28    }
29}
30
31#[cfg(feature = "async")]
32fn main() {
33    futures_lite::future::block_on(async {
34        use expectrl::AsyncExpect;
35
36        let mut session = expectrl::spawn("ls -al").expect("Can't spawn a session");
37
38        loop {
39            let m = session
40                .expect(Any::boxed(vec![
41                    Box::new("\r"),
42                    Box::new("\n"),
43                    Box::new(Eof),
44                ]))
45                .await
46                .expect("Expect failed");
47
48            let is_eof = m.get(0).unwrap().is_empty();
49            if is_eof {
50                break;
51            }
52
53            if m.get(0).unwrap() == [b'\n'] {
54                continue;
55            }
56
57            println!("{:?}", String::from_utf8_lossy(m.get(0).unwrap()));
58        }
59    })
60}