From c433dd2c400d9f2e8421f074be1b287d27b709df Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Sat, 30 Mar 2024 23:36:58 -0600 Subject: Use $PS1 for the prompt Instead of passing a hard-coded value for the prompt, use $PS1. The default is '|> ', set in dist/etc/dwvshrc. --- dist/etc/dwvshrc | 4 ++++ src/main.rs | 42 +++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/dist/etc/dwvshrc b/dist/etc/dwvshrc index efb3f3d..7f7314e 100644 --- a/dist/etc/dwvshrc +++ b/dist/etc/dwvshrc @@ -1,3 +1,7 @@ #!/usr/bin/env dwvsh +# Default path export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin + +# Default prompt +export PS1="|> " diff --git a/src/main.rs b/src/main.rs index c17f959..e0d0ed1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::env; use std::io::{self, Write}; use std::sync::{Arc, Mutex}; mod path; @@ -9,30 +10,36 @@ use compose::Environment; /// Starts the main shell loop /// /// # Arguments -/// * `prompt` - A string slice indicating the shell's prompt -/// * `at_prompt` - A mutex, indicating whether or not user is at the prompt +/// * `away` - A mutex, indicating whether or not user is at the prompt +/// * `env` - The global shell state /// /// # Examples /// ``` /// fn main() { -/// let prompt = "|> "; -/// let mut at_prompt = Arc::new(Mutex::new(false)); +/// let mut env = compose::env(); +/// let mut away = Arc::new(Mutex::new(false)); /// ... -/// repl(prompt, &mut at_prompt); +/// repl(&mut away, &mut env); /// } /// ``` -fn repl(prompt: &str, at_prompt: &mut Arc>, env: &mut Environment) { +fn repl(away: &mut Arc>, env: &mut Environment) { // Initial path refresh on startup env.bins = path::refresh(); // Main shell loop loop { + // Get the prompt + let prompt = match env::var("PS1") { + Ok(val) => val, + Err(_) => String::from("|> "), + }; + // Output the prompt print!("{}", prompt); io::stdout().flush().unwrap(); // At the prompt - *at_prompt.lock().unwrap() = true; + *away.lock().unwrap() = false; // Wait for user input let mut poetry = String::new(); @@ -55,7 +62,7 @@ fn repl(prompt: &str, at_prompt: &mut Arc>, env: &mut Environment) { } // Not at the prompt - *at_prompt.lock().unwrap() = false; + *away.lock().unwrap() = true; // Parse the poem let poem = Poem::read(poetry); @@ -82,24 +89,25 @@ fn main() { // Compose the environment for dwvsh let mut env = compose::env(); - // Set the prompt - let prompt = "|> "; - let mut at_prompt = Arc::new(Mutex::new(false)); - // Handle signals + let mut away = Arc::new(Mutex::new(true)); unsafe { - let at_prompt = Arc::clone(&at_prompt); + let away = Arc::clone(&away); signal_hook::low_level::register(signal_hook::consts::SIGINT, move || { - if *at_prompt.lock().unwrap() { + if *away.lock().unwrap() { + println!(); + } else { + let prompt = match env::var("PS1") { + Ok(val) => val, + Err(_) => String::from("|> "), + }; print!("\n{}", prompt); io::stdout().flush().unwrap(); - } else { - println!(); } }) .unwrap(); }; // Begin evaluating commands - repl(prompt, &mut at_prompt, &mut env); + repl(&mut away, &mut env); } -- cgit v1.2.3