From f8f482f6b08d3752a680abcb9cb3c58989888f63 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 14 Oct 2020 22:31:57 +0300 Subject: Add ! builtin to display the last status --- src/builtins.rs | 21 +++++++++++---------- src/main.rs | 12 +++++++++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/builtins.rs b/src/builtins.rs index ac83efa..bcc14f1 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -1,12 +1,13 @@ -use std::path::Path; +use std::io::{Error, ErrorKind}; +use std::path::{Path, PathBuf}; use std::process::Command; -pub fn cd( - args: &[&str], - home: &std::path::PathBuf, -) -> Result { +pub fn cd(args: &[&str], home: &PathBuf) -> Result { if args.len() > 1 { - return Err("Too many arguments passed to cd".to_string()); + return Err(Error::new( + ErrorKind::InvalidInput, + "Too many arguments passed to cd", + )); } if args.len() == 0 { @@ -20,13 +21,13 @@ pub fn cd( match std::env::set_current_dir(&root) { Ok(_) => Ok(root.to_path_buf()), - Err(e) => Err(format!("Could not set current dir: {}", e)), + Err(e) => Err(e), } } -pub fn run(command: &str, args: &[&str]) { - let mut child = Command::new(command).args(args).spawn().unwrap(); - child.wait().unwrap(); +pub fn run(command: &str, args: &[&str]) -> Result { + let mut child = Command::new(command).args(args).spawn()?; + child.wait() } /* match command { diff --git a/src/main.rs b/src/main.rs index aa6998d..24aa9b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ fn main() { let prompt = prompt::Prompt::new().unwrap(); let mut cwd = std::env::current_dir().unwrap(); + let mut status = None; //: std::process::ExitStatus; // `()` can be used when no completer is required let mut rl = Editor::<()>::new(); @@ -26,6 +27,7 @@ fn main() { rl.add_history_entry(line.as_str()); let commands = parser::parse(&line); match commands[0] { + "!" => println!("{:?}", status), "cd" => match builtins::cd(&commands[1..], &prompt.home) { Ok(p) => cwd = p, Err(e) => eprintln!("Error: {}", e), @@ -33,9 +35,13 @@ fn main() { "exit" => { break 'repl; } - _ => { - builtins::run(commands[0], &commands[1..]); - } + _ => match builtins::run(commands[0], &commands[1..]) { + Ok(s) => { + println!("Ok({})", s); + status = Some(s); + }, + Err(e) => println!("Err({})", e), + }, } //println!("Line: {}", line); } -- cgit v1.2.1