Compare commits
No commits in common. "797f401cf302508026aeb6fb8bdd1a0ed3fd5be8" and "f73c10d6eb99ac7c5d5d90b72ab428715e895ef5" have entirely different histories.
797f401cf3
...
f73c10d6eb
65
src/main.rs
65
src/main.rs
@ -1,73 +1,26 @@
|
|||||||
use std::{env, io::{stdin, stdout, Write}, path::Path, process::{Child, Command, Stdio}};
|
use std::{io::{stdin, stdout, Write}, 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();
|
||||||
|
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
stdin().read_line(&mut input).unwrap();
|
stdin().read_line(&mut input).unwrap();
|
||||||
|
|
||||||
// must be peekable so we know when we are on the last command
|
let mut parts = input.trim().split_whitespace();
|
||||||
let mut commands = input.trim().split(" | ").peekable();
|
|
||||||
let mut previous_command = None;
|
|
||||||
|
|
||||||
while let Some(command) = commands.next() {
|
|
||||||
|
|
||||||
let mut parts = command.trim().split_whitespace();
|
|
||||||
let command = parts.next().unwrap();
|
let command = parts.next().unwrap();
|
||||||
let args = parts;
|
let args = parts;
|
||||||
|
|
||||||
match command {
|
let mut child = Command::new(command)
|
||||||
"cd" => {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
previous_command = None;
|
|
||||||
},
|
|
||||||
"exit" => return,
|
|
||||||
command => {
|
|
||||||
let stdin = previous_command
|
|
||||||
.map_or(
|
|
||||||
Stdio::inherit(),
|
|
||||||
|output: Child| Stdio::from(output.stdout.unwrap())
|
|
||||||
);
|
|
||||||
|
|
||||||
let stdout = if commands.peek().is_some() {
|
|
||||||
// there is another command piped behind this one
|
|
||||||
// prepare to send output to the next command
|
|
||||||
Stdio::piped()
|
|
||||||
} else {
|
|
||||||
// there are no more commands piped behind this one
|
|
||||||
// send output to shell stdout
|
|
||||||
Stdio::inherit()
|
|
||||||
};
|
|
||||||
|
|
||||||
let output = Command::new(command)
|
|
||||||
.args(args)
|
.args(args)
|
||||||
.stdin(stdin)
|
.spawn()
|
||||||
.stdout(stdout)
|
.unwrap();
|
||||||
.spawn();
|
|
||||||
|
|
||||||
match output {
|
// don't accept another command until this one completes
|
||||||
Ok(output) => { previous_command = Some(output); },
|
child.wait();
|
||||||
Err(e) => {
|
|
||||||
previous_command = None;
|
|
||||||
eprintln!("{}", e);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(mut final_command) = previous_command {
|
|
||||||
// block until the final command has finished
|
|
||||||
final_command.wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user