aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-10-14 22:31:57 +0300
committerAqua-sama <aqua@iserlohn-fortress.net>2020-10-14 22:31:57 +0300
commitf8f482f6b08d3752a680abcb9cb3c58989888f63 (patch)
tree53709b0d93dee7cdede559f57789206e66041c38
parentCheck arguments passed to cd (diff)
downloadrshell-f8f482f6b08d3752a680abcb9cb3c58989888f63.tar.xz
Add ! builtin to display the last status
-rw-r--r--src/builtins.rs21
-rw-r--r--src/main.rs12
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<std::path::PathBuf, std::string::String> {
+pub fn cd(args: &[&str], home: &PathBuf) -> Result<PathBuf, Error> {
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<std::process::ExitStatus, Error> {
+ 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);
}