From c101a8c2abf0c25fc75e4101a83ae56bc2a9b789 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Fri, 16 Feb 2024 22:36:31 -0700 Subject: Capture SIGINT Added logic to capture the interrupt signal. Using a rust crate called 'ctrlc' to do the heavy lifting. Program will simply reprint the prompt on a new line if the interrup signal is detected. --- src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 42fa2a9..f78e54d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use ctrlc; use std::fs; use std::io; use std::io::Write; @@ -78,6 +79,8 @@ fn eval(paths: &[&str], prompt: &str) { } fn main() { + // Define paths + // TODO: Hardcoded path should only be the fallback let paths = [ "/bin", "/sbin", @@ -87,7 +90,16 @@ fn main() { "/usr/local/sbin", ]; + // Set the prompt let prompt = "|> "; + // Handle signals + ctrlc::set_handler(move || { + print!("\n{}", prompt); + io::stdout().flush().unwrap(); + }) + .expect("Unable to set handler"); + + // Begin evaluating commands eval(&paths, prompt); } -- cgit v1.2.3