Skip to content

Commit 8423d98

Browse files
authored
✨ Enables user restart by entering rs in stdin (#12) (#17)
* ✨ Enables user restart by entering rs in stdin Users can restart their program by typing rs and pressing enter in the terminal. * 🐛 Kill child process before restarting. * 🚨 Removed Clippy warnings
1 parent dfd2cb9 commit 8423d98

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

src/exec.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
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;
34
use std::sync::Arc;
45
use std::thread;
5-
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
66

77
pub use crate::cli::Cli;
88
pub 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
})

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn main() {
3131
}
3232
};
3333

34-
watcher::start(cli_args.clone(), lightmon_event_sender);
34+
watcher::start(cli_args.clone(), lightmon_event_sender.clone());
3535

3636
println!("lightmon started ({} mode)", cli_args.project_language);
3737

@@ -41,14 +41,14 @@ fn main() {
4141
match lightmon_event {
4242
LightmonEvent::KillAndRestartChild => {
4343
debug!("KILL AND RESTART RECEIEVED");
44-
exec::start(cli_args.clone());
44+
exec::start(cli_args.clone(), lightmon_event_sender.clone());
4545
}
4646
LightmonEvent::KillChild => {
4747
debug!("KILL RECEIEVED");
4848
}
4949
LightmonEvent::InitExec => {
5050
debug!("INIT EXEC RECEIVED");
51-
exec::start(cli_args.clone());
51+
exec::start(cli_args.clone(), lightmon_event_sender.clone());
5252
}
5353
}
5454
}

0 commit comments

Comments
 (0)