From 2e5cc53499947c32b01ea5e1787ed505bc286969 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Mon, 19 Feb 2024 01:40:58 -0700 Subject: Refresh path only if command is not found This is a modified implementation of the 'refresh path on every iteration of the loop' idea. It instead, only refreshes the path if the command is not found. After the first refresh, if the command still is not found, it throws and error. --- src/main.rs | 53 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 272eec1..4d56375 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,21 +4,11 @@ use std::io; use std::io::Write; use std::path::Path; use std::process::Command; -use std::time::SystemTime; fn eval(paths: &[&str], prompt: &str) { let mut bins: Vec = Vec::new(); loop { - let now = SystemTime::now(); - for path in paths { - let files = fs::read_dir(path).expect("Unable to read files in your path"); - for file in files { - bins.push(file.unwrap().path().display().to_string()); - } - } - println!("Refresh: {} ms", now.elapsed().unwrap().as_millis()); - // Output the prompt io::stdout().flush().unwrap(); print!("{}", prompt); @@ -46,7 +36,7 @@ fn eval(paths: &[&str], prompt: &str) { // Parse command and arguments let mut split = input.split(' '); - let mut cmd = match split.next() { + let cmd = match split.next() { Some(str) if str.trim().is_empty() => continue, Some(str) => str.trim(), None => continue, @@ -80,16 +70,41 @@ fn eval(paths: &[&str], prompt: &str) { // Check if the file exists, if given a pull or relative path // TODO: Check if file at the path is executable (i.e. +x) - if !Path::new(cmd).exists() { + let mut cmd = String::from(cmd); + if !Path::new(cmd.as_str()).exists() { + let b = bins.clone(); // Check if the command exists in $PATH if a full or relative path // was not given, or if the path does not exist - cmd = match bins.iter().find(|b| b.split("/").last().unwrap() == cmd) { - Some(cmd) => cmd, - None => { - println!("Command not found"); - continue; - } - }; + // + // If the command is not found the first time, try refreshing the + // path first, and only print an error if if it's not found after + // the path refresh + cmd = String::from( + match b + .clone() + .iter() + .find(|b| b.split("/").last().unwrap() == cmd) + { + Some(cmd) => cmd, + None => { + for path in paths { + let files = + fs::read_dir(path).expect("Unable to read files in your path"); + for file in files { + bins.push(file.unwrap().path().display().to_string()); + } + } + + match bins.iter().find(|b| b.split("/").last().unwrap() == cmd) { + Some(cmd) => cmd, + None => { + println!("dwvsh: error: command not found..."); + continue; + } + } + } + }, + ); } // Run the command (and wait for it to finish) -- cgit v1.2.3