From 718f45492a4b2c31a67458c13c4cd4b3268703bc Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Thu, 29 Feb 2024 01:40:27 -0700 Subject: 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. --- src/main.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/main.rs') 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>) { // Initial path refresh on startup let mut bins: Vec = 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); } -- cgit v1.2.3