1- use std:: io:: { self , Write } ;
2- use std:: process:: { Command , Stdio } ;
1+ use std:: io:: { self } ;
2+ use std:: process:: Command ;
3+ use std:: sync:: mpsc:: Sender ;
34use std:: sync:: Arc ;
45use std:: thread;
5- use termcolor:: { Color , ColorChoice , ColorSpec , StandardStream , WriteColor } ;
66
77pub use crate :: cli:: Cli ;
88pub use crate :: LightmonEvent ;
99
10- pub fn start ( cli_args : Arc < Cli > ) -> std:: thread:: JoinHandle < ( ) > {
10+ pub fn start (
11+ cli_args : Arc < Cli > ,
12+ lightmon_event_sender : Sender < LightmonEvent > ,
13+ ) -> std:: thread:: JoinHandle < ( ) > {
1114 thread:: spawn ( move || {
1215 debug ! ( "thread started" ) ;
1316
@@ -20,23 +23,32 @@ pub fn start(cli_args: Arc<Cli>) -> std::thread::JoinHandle<()> {
2023 cmd. arg ( argument) ;
2124 }
2225 debug ! ( "final cmd = {:?}" , cmd) ;
23- let output = cmd. output ( ) . unwrap ( ) ;
24- cmd. stdout ( Stdio :: inherit ( ) ) ;
25- cmd. stdout ( Stdio :: inherit ( ) ) ;
26- //write stdout to stdout
27- io:: stdout ( ) . write_all ( & output. stdout ) . unwrap ( ) ;
28- if !output. stderr . is_empty ( ) {
29- //if stderr
30- //change color to red
31- let mut error = StandardStream :: stderr ( ColorChoice :: Always ) ;
32- error
33- . set_color ( ColorSpec :: new ( ) . set_fg ( Some ( Color :: Red ) ) )
34- . unwrap ( ) ;
35- //write to stderr
36- io:: stderr ( ) . write_all ( & output. stderr ) . unwrap ( ) ;
37- //reset terminal output color
38- let color_reset = WriteColor :: reset ( & mut error) . unwrap ( ) ;
39- debug ! ( "reset color? {:?}" , color_reset) ;
26+ let mut child = cmd. spawn ( ) . unwrap ( ) ;
27+ loop {
28+ let mut input = String :: new ( ) ;
29+ if let Ok ( n) = io:: stdin ( ) . read_line ( & mut input) {
30+ if input. eq ( "rs\n " ) {
31+ debug ! ( "rs RECEIEVED" ) ;
32+ match lightmon_event_sender. send ( LightmonEvent :: KillAndRestartChild ) {
33+ Ok ( ( ) ) => {
34+ let child_killed = child. kill ( ) ;
35+ match child_killed {
36+ Ok ( ( ) ) => { }
37+ Err ( e) => {
38+ error ! ( "program cannot be quit and restarted: {:?}" , e) ;
39+ std:: process:: exit ( 1 ) ;
40+ }
41+ }
42+ }
43+ Err ( _) => {
44+ panic ! ( "failed to send initial lightmon event. Something went seriously wrong!" ) ;
45+ }
46+ } ;
47+ } else {
48+ debug ! ( "unknown input, bits read from input {:?}" , n) ;
49+ debug ! ( "input = {:?}" , input) ;
50+ }
51+ }
4052 }
4153 }
4254 } )
0 commit comments