@@ -45,27 +45,23 @@ impl UartConsole {
4545 }
4646
4747 // Runs an interactive console until CTRL_C is received.
48- pub fn interact < T > ( & mut self , device : & T , stdout : Option < & mut dyn Write > ) -> Result < ExitStatus >
48+ pub fn interact < T > ( & mut self , device : & T , quiet : bool ) -> Result < ExitStatus >
4949 where
5050 T : ConsoleDevice + ?Sized ,
5151 {
52- crate :: util:: runtime:: block_on ( self . interact_async ( device, stdout ) )
52+ crate :: util:: runtime:: block_on ( self . interact_async ( device, quiet ) )
5353 }
5454
5555 // Runs an interactive console until CTRL_C is received. Uses `mio` library to simultaneously
5656 // wait for data from UART or from stdin, without need for timeouts and repeated calls.
57- pub async fn interact_async < T > (
58- & mut self ,
59- device : & T ,
60- mut stdout : Option < & mut dyn Write > ,
61- ) -> Result < ExitStatus >
57+ pub async fn interact_async < T > ( & mut self , device : & T , quiet : bool ) -> Result < ExitStatus >
6258 where
6359 T : ConsoleDevice + ?Sized ,
6460 {
6561 let timeout = self . timeout ;
6662 let rx = async {
6763 loop {
68- self . uart_read ( device, & mut stdout ) . await ?;
64+ self . uart_read ( device, quiet ) . await ?;
6965 if self
7066 . exit_success
7167 . as_ref ( )
@@ -115,35 +111,35 @@ impl UartConsole {
115111 }
116112
117113 // Read from the console device and process the data read.
118- async fn uart_read < T > ( & mut self , device : & T , stdout : & mut Option < & mut dyn Write > ) -> Result < ( ) >
114+ async fn uart_read < T > ( & mut self , device : & T , quiet : bool ) -> Result < ( ) >
119115 where
120116 T : ConsoleDevice + ?Sized ,
121117 {
122- let mut buf = [ 0u8 ; 1024 ] ;
123- let effective_buf = if self . uses_regex ( ) {
124- // Read one byte at a time when matching, to avoid the risk of consuming output past a
125- // match.
126- & mut buf[ ..1 ]
127- } else {
128- & mut buf
129- } ;
130- let len = std:: future:: poll_fn ( |cx| device. poll_read ( cx, effective_buf) ) . await ?;
131- for i in 0 ..len {
118+ let mut ch = 0 ;
119+
120+ // Read one byte at a time to avoid the risk of consuming output past a match.
121+ let len =
122+ std:: future:: poll_fn ( |cx| device. poll_read ( cx, std:: slice:: from_mut ( & mut ch) ) ) . await ?;
123+
124+ if len == 0 {
125+ return Ok ( ( ) ) ;
126+ }
127+
128+ if !quiet {
129+ let mut stdout = std:: io:: stdout ( ) . lock ( ) ;
130+
132131 if self . timestamp && self . newline {
133132 let t = humantime:: format_rfc3339_millis ( SystemTime :: now ( ) ) ;
134- stdout. as_mut ( ) . map_or ( Ok ( ( ) ) , |out| {
135- out. write_fmt ( format_args ! ( "[{} console]" , t) )
136- } ) ?;
137- self . newline = false ;
133+ stdout. write_fmt ( format_args ! ( "[{} console]" , t) ) ?;
138134 }
139- self . newline = buf [ i ] == b'\n' ;
140- stdout
141- . as_mut ( )
142- . map_or ( Ok ( ( ) ) , |out| out . write_all ( & buf [ i..i + 1 ] ) ) ?;
135+ self . newline = ch == b'\n' ;
136+
137+ stdout . write_all ( std :: slice :: from_ref ( & ch ) ) ? ;
138+ stdout . flush ( ) ?;
143139 }
144- stdout . as_mut ( ) . map_or ( Ok ( ( ) ) , |out| out . flush ( ) ) ? ;
140+
145141 if self . uses_regex ( ) {
146- self . append_buffer ( & buf [ ..len ] ) ;
142+ self . append_buffer ( std :: slice :: from_ref ( & ch ) ) ;
147143 }
148144 Ok ( ( ) )
149145 }
@@ -170,8 +166,7 @@ impl UartConsole {
170166 T : ConsoleDevice + ?Sized ,
171167 {
172168 let mut console = UartConsole :: new ( Some ( timeout) , Some ( Regex :: new ( rx) ?) , None ) ;
173- let mut stdout = std:: io:: stdout ( ) ;
174- let result = console. interact ( device, Some ( & mut stdout) ) ?;
169+ let result = console. interact ( device, false ) ?;
175170 println ! ( ) ;
176171 match result {
177172 ExitStatus :: ExitSuccess => {
0 commit comments