@@ -15,9 +15,10 @@ use std::{
1515 fs:: { self , File } ,
1616 io:: { self , Read , Write } ,
1717 path:: PathBuf ,
18- sync:: mpsc:: { channel, SendError , Sender , TryRecvError } ,
18+ process:: exit,
19+ sync:: mpsc:: { channel, Sender , TryRecvError } ,
1920 thread:: JoinHandle ,
20- time:: Duration ,
21+ time:: { Duration , Instant } ,
2122} ;
2223use tracing:: { debug, error, info, instrument, trace, warn} ;
2324
@@ -205,17 +206,23 @@ impl Repl {
205206 }
206207
207208 let mut prompt = true ;
209+ let mut command_written = true ;
210+ let mut last_read = Instant :: now ( ) ;
208211 debug ! ( "Starting user loop" ) ;
209212 ' user_loop: loop {
210213 self . inst . set_nonblocking ( true ) ?;
211214 std:: thread:: sleep ( Duration :: from_micros ( 1 ) ) ;
212- let mut read_buf: Vec < u8 > = vec ! [ 0 ; 1024 ] ;
213- let read_size = self . inst . read ( & mut read_buf) ?;
214- let read_buf: Vec < u8 > = read_buf[ ..read_size] . into ( ) ;
215- prompt = self . handle_data ( & read_buf, prompt, & mut prev_state, & mut state) ?;
215+ if command_written || last_read. elapsed ( ) >= Duration :: from_secs ( 3 ) {
216+ let mut read_buf: Vec < u8 > = vec ! [ 0 ; 1024 ] ;
217+ let read_size = self . inst . read ( & mut read_buf) ?;
218+ let read_buf: Vec < u8 > = read_buf[ ..read_size] . into ( ) ;
219+ prompt = self . handle_data ( & read_buf, prompt, & mut prev_state, & mut state) ?;
220+ last_read = Instant :: now ( ) ;
221+ }
216222
217223 if prompt {
218224 prompt = false ;
225+ command_written = false ;
219226 Self :: print_flush ( & "\n TSP> " . blue ( ) ) ?;
220227 }
221228 match loop_in. try_recv ( ) {
@@ -224,6 +231,7 @@ impl Repl {
224231 match msg {
225232 Request :: Tsp ( tsp) => {
226233 self . inst . write_all ( format ! ( "{tsp}\n " ) . as_bytes ( ) ) ?;
234+ command_written = true ;
227235 prev_state = None ;
228236 }
229237 Request :: GetError => {
@@ -268,6 +276,7 @@ impl Repl {
268276 }
269277 }
270278 prompt = false ;
279+ command_written = true ;
271280 }
272281 Request :: TspLinkNodes { json_file } => {
273282 self . set_lang_config_path ( json_file. to_string_lossy ( ) . to_string ( ) ) ;
@@ -293,6 +302,7 @@ impl Repl {
293302 // lose runtime state, so we can't save the previous
294303 // setting, so we just hardcode it to enabled here.
295304 self . inst . write_all ( b"localnode.prompts=1\n " ) ?;
305+ command_written = true ;
296306 }
297307 Request :: Exit => {
298308 info ! ( "Exiting..." ) ;
@@ -301,6 +311,7 @@ impl Repl {
301311 Request :: Reset => {
302312 self . inst . as_mut ( ) . reset ( ) ?;
303313 prompt = true ;
314+ command_written = true ;
304315 }
305316 Request :: Help { sub_cmd } => {
306317 prompt = true ;
@@ -714,9 +725,9 @@ impl Repl {
714725 let mut input = String :: new ( ) ;
715726 let _ = std:: io:: stdin ( ) . read_line ( & mut input) ?;
716727 let req = Self :: parse_user_commands ( & input) ?;
717- match out. send ( req. clone ( ) ) {
718- Ok ( ( ) ) => { }
719- Err ( SendError ( _ ) ) => break ' input_loop ,
728+ if out. send ( req. clone ( ) ) . is_err ( ) {
729+ error ! ( "User input thread could not send to Receiver. Closing!" ) ;
730+ exit ( 1 ) ;
720731 }
721732 // This `if` statement seeks to fix the NOTE above about not exiting.
722733 // It feels a little awkward, but should be effective.
0 commit comments