Add some variables and fix echo: now can display more words and some other variables

This commit is contained in:
g1n 2021-07-21 11:21:42 +00:00
parent 40f8120357
commit e647443ed7
1 changed files with 25 additions and 9 deletions

View File

@ -6,6 +6,8 @@ use std::process::{Child, Command, Stdio};
fn main(){
let mut status=0;
let user = env::var("USER").unwrap();
let home_dir = format!("{}{}", "/home/", user);
let mut pwd = env::current_dir().unwrap();
loop {
// use the `>` character as the prompt
@ -37,19 +39,33 @@ fn main(){
status = 1;
},
"echo" => {
let args = args.peekable().peek().map_or("", |x| *x);
if args.clone() == "$?" {
println!("{}", status);
} else if args.clone() == "$PWD" {
println!("{}", pwd.display());
} else {
println!("{}", args);
let args: Vec<&str> = args/*.peekable().peek().map_or("", |x| *x).split(" ")*/.collect();
for arg in args {
match arg {
"$?" => {
print!("{}", status);
},
"$PWD" => {
print!("{}", pwd.display());
},
"$USER" => {
print!("{}", user);
},
"$HOME" => {
print!("{}", home_dir);
},
arg => {
print!("{}", arg);
},
}
print!(" ");
}
stdout().flush().unwrap();
println!();
},
"cd" => {
// default to '/' as new directory if one was not provided
let home_dir = env::var("HOME").unwrap();
let new_dir = args.peekable().peek().map_or(home_dir, |x| (*x).to_string());
let new_dir = args.peekable().peek().map_or(home_dir.clone(), |x| (*x).to_string());
let root = Path::new(&new_dir);
pwd=root.clone().to_path_buf();
if let Err(e) = env::set_current_dir(&root) {