@@ -16,7 +16,6 @@ pub struct UartConsole {
1616 exit_failure : Option < Regex > ,
1717 pub timestamp : bool ,
1818 buffer : String ,
19- newline : bool ,
2019}
2120
2221#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
@@ -40,32 +39,27 @@ impl UartConsole {
4039 exit_failure,
4140 timestamp : true ,
4241 buffer : String :: new ( ) ,
43- newline : true ,
4442 }
4543 }
4644
4745 // 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 >
46+ pub fn interact < T > ( & mut self , device : & T , quiet : bool ) -> Result < ExitStatus >
4947 where
5048 T : ConsoleDevice + ?Sized ,
5149 {
52- crate :: util:: runtime:: block_on ( self . interact_async ( device, stdout ) )
50+ crate :: util:: runtime:: block_on ( self . interact_async ( device, quiet ) )
5351 }
5452
5553 // Runs an interactive console until CTRL_C is received. Uses `mio` library to simultaneously
5654 // 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 >
55+ pub async fn interact_async < T > ( & mut self , device : & T , quiet : bool ) -> Result < ExitStatus >
6256 where
6357 T : ConsoleDevice + ?Sized ,
6458 {
6559 let timeout = self . timeout ;
6660 let rx = async {
6761 loop {
68- self . uart_read ( device, & mut stdout ) . await ?;
62+ self . uart_read ( device, quiet ) . await ?;
6963 if self
7064 . exit_success
7165 . as_ref ( )
@@ -115,35 +109,34 @@ impl UartConsole {
115109 }
116110
117111 // 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 < ( ) >
112+ async fn uart_read < T > ( & mut self , device : & T , quiet : bool ) -> Result < ( ) >
119113 where
120114 T : ConsoleDevice + ?Sized ,
121115 {
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 {
132- if self . timestamp && self . newline {
116+ let mut ch = 0 ;
117+
118+ // Read one byte at a time to avoid the risk of consuming output past a match.
119+ let len =
120+ std:: future:: poll_fn ( |cx| device. poll_read ( cx, std:: slice:: from_mut ( & mut ch) ) ) . await ?;
121+
122+ if len == 0 {
123+ return Ok ( ( ) ) ;
124+ }
125+
126+ if !quiet {
127+ let mut stdout = std:: io:: stdout ( ) . lock ( ) ;
128+ stdout. write_all ( std:: slice:: from_ref ( & ch) ) ?;
129+
130+ if self . timestamp && ch == b'\n' {
133131 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 ;
132+ stdout. write_fmt ( format_args ! ( "[{} console]" , t) ) ?;
138133 }
139- self . newline = buf[ i] == b'\n' ;
140- stdout
141- . as_mut ( )
142- . map_or ( Ok ( ( ) ) , |out| out. write_all ( & buf[ i..i + 1 ] ) ) ?;
134+
135+ stdout. flush ( ) ?;
143136 }
144- stdout . as_mut ( ) . map_or ( Ok ( ( ) ) , |out| out . flush ( ) ) ? ;
137+
145138 if self . uses_regex ( ) {
146- self . append_buffer ( & buf [ ..len ] ) ;
139+ self . append_buffer ( std :: slice :: from_ref ( & ch ) ) ;
147140 }
148141 Ok ( ( ) )
149142 }
@@ -170,8 +163,7 @@ impl UartConsole {
170163 T : ConsoleDevice + ?Sized ,
171164 {
172165 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) ) ?;
166+ let result = console. interact ( device, false ) ?;
175167 println ! ( ) ;
176168 match result {
177169 ExitStatus :: ExitSuccess => {
0 commit comments