summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorRory Dudley2024-02-29 01:40:27 -0700
committerRory Dudley2024-02-29 01:40:27 -0700
commit718f45492a4b2c31a67458c13c4cd4b3268703bc (patch)
treed577bc14368319adac8bf24e0a757b5eccd24df8 /src/main.rs
parent2be80340afbc457f22f8c4cc441ef572b0acfda1 (diff)
downloaddwarvish-718f45492a4b2c31a67458c13c4cd4b3268703bc.tar.gz
Fix handling of SIGINT
Keep track of a new atomic variable: at_prompt, which is set to true just before blocking on io::stdin.read_line, and set to false just calling Poem::read. Additionally, for background tasks, there is a new ps macro called btask, which changes the process group of commands that are forked into the background, so that they don't receive SIGINT from the keyboard.
Notes
Notes: Changing the process group on the Command is done via CommandExt. More details here: https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html#tymethod.process_group
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 3cb2a74..04d58eb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,6 +3,7 @@ use recite::path::prefresh;
use recite::Poem;
use std::io::{self, Write};
use std::path::Path;
+use std::sync::{Arc, Mutex};
/// Starts the main shell loop
///
@@ -20,7 +21,7 @@ use std::path::Path;
/// repl(&path, prompt);
/// }
/// ```
-fn repl(path: &Vec<&Path>, prompt: &str) {
+fn repl(path: &Vec<&Path>, prompt: &str, at_prompt: &mut Arc<Mutex<bool>>) {
// Initial path refresh on startup
let mut bins: Vec<String> = prefresh(path);
@@ -30,6 +31,9 @@ fn repl(path: &Vec<&Path>, prompt: &str) {
print!("{}", prompt);
io::stdout().flush().unwrap();
+ // At the prompt
+ *at_prompt.lock().unwrap() = true;
+
// Wait for user input
let mut poetry = String::new();
let bytes = io::stdin()
@@ -50,6 +54,9 @@ fn repl(path: &Vec<&Path>, prompt: &str) {
continue;
}
+ // Not at the prompt
+ *at_prompt.lock().unwrap() = false;
+
// Parse a poem
let poem = Poem::read(poetry);
match poem {
@@ -80,16 +87,22 @@ fn main() {
// Set the prompt
let prompt = "|> ";
+ let mut at_prompt = Arc::new(Mutex::new(false));
// Handle signals
unsafe {
+ let at_prompt = Arc::clone(&at_prompt);
signal_hook::low_level::register(signal_hook::consts::SIGINT, move || {
- print!("\n{}", prompt);
- io::stdout().flush().unwrap();
+ if *at_prompt.lock().unwrap() {
+ print!("\n{}", prompt);
+ io::stdout().flush().unwrap();
+ } else {
+ println!();
+ }
})
.unwrap();
};
// Begin evaluating commands
- repl(&path, prompt);
+ repl(&path, prompt, &mut at_prompt);
}