feat: add support for 'cd' command in REPL loop
This commit is contained in:
parent
f73c10d6eb
commit
3ed8234852
18
src/main.rs
18
src/main.rs
@ -1,9 +1,7 @@
|
|||||||
use std::{io::{stdin, stdout, Write}, process::Command};
|
use std::{env, io::{stdin, stdout, Write}, path::Path, process::Command};
|
||||||
|
|
||||||
fn main(){
|
fn main(){
|
||||||
loop {
|
loop {
|
||||||
// use the `>` character as the prompt
|
|
||||||
// need to explicitly flush this to ensure it prints before read_line
|
|
||||||
print!("> ");
|
print!("> ");
|
||||||
stdout().flush();
|
stdout().flush();
|
||||||
|
|
||||||
@ -14,13 +12,23 @@ fn main(){
|
|||||||
let command = parts.next().unwrap();
|
let command = parts.next().unwrap();
|
||||||
let args = parts;
|
let args = parts;
|
||||||
|
|
||||||
|
match command {
|
||||||
|
"cd" => {
|
||||||
|
// default to '/' as new directory if one was not provided
|
||||||
|
let new_dir = args.peekable().peek().map_or("/", |x| *x);
|
||||||
|
let root = Path::new(new_dir);
|
||||||
|
if let Err(e) = env::set_current_dir(&root) {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
command => {
|
||||||
let mut child = Command::new(command)
|
let mut child = Command::new(command)
|
||||||
.args(args)
|
.args(args)
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// don't accept another command until this one completes
|
|
||||||
child.wait();
|
child.wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user