diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 53 |
1 files changed, 34 insertions, 19 deletions
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<String> = 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) |