Add PWD variable and pwd command
This commit is contained in:
parent
6dc07fbc90
commit
0f2eb8413e
19
src/main.rs
19
src/main.rs
|
@ -5,8 +5,8 @@ use std::process::{Child, Command, Stdio};
|
|||
|
||||
|
||||
fn main(){
|
||||
let mut STATUS=0;
|
||||
|
||||
let mut status=0;
|
||||
let mut pwd = env::current_dir().unwrap();
|
||||
loop {
|
||||
// use the `>` character as the prompt
|
||||
// need to explicitly flush this to ensure it prints before read_line
|
||||
|
@ -22,6 +22,7 @@ fn main(){
|
|||
let mut previous_command = None;
|
||||
|
||||
while let Some(command) = commands.next() {
|
||||
// Other shell variables that can be changed
|
||||
|
||||
// everything after the first whitespace character is interpreted as args to the command
|
||||
let mut parts = command.trim().split_whitespace();
|
||||
|
@ -30,15 +31,17 @@ fn main(){
|
|||
|
||||
match command {
|
||||
"true" | ":" => {
|
||||
STATUS = 0;
|
||||
status = 0;
|
||||
},
|
||||
"false" => {
|
||||
STATUS = 1;
|
||||
status = 1;
|
||||
},
|
||||
"echo" => {
|
||||
let args = args.peekable().peek().map_or("", |x| *x);
|
||||
if args.clone() == "$STATUS" {
|
||||
println!("{}", STATUS);
|
||||
if args.clone() == "$?" {
|
||||
println!("{}", status);
|
||||
} else if args.clone() == "$PWD" {
|
||||
println!("{}", pwd.display());
|
||||
} else {
|
||||
println!("{}", args);
|
||||
}
|
||||
|
@ -48,12 +51,16 @@ fn main(){
|
|||
let home_dir = env::var("HOME").unwrap();
|
||||
let new_dir = args.peekable().peek().map_or(home_dir, |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) {
|
||||
eprintln!("{}", e);
|
||||
}
|
||||
|
||||
previous_command = None;
|
||||
},
|
||||
"pwd" => {
|
||||
println!("{}", pwd.display());
|
||||
}
|
||||
"exit" => return,
|
||||
command => {
|
||||
let stdin = previous_command
|
||||
|
|
Loading…
Reference in New Issue