@@ -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) ]
8689fn main ( ) {
87- panic ! ( "An example doesn't supported on windows" )
90+ panic ! ( "An example is not supported on windows" )
8891}
0 commit comments