minor changes
This commit is contained in:
parent
feaa65e21a
commit
b79d749ae4
129
src/main.rs
129
src/main.rs
|
@ -1,68 +1,81 @@
|
||||||
// Rash
|
use std::{
|
||||||
//Rusty bASH
|
env,
|
||||||
// Really Awesome SHell
|
io::{stdin, stdout, Write},
|
||||||
use std::io::Write;
|
path::Path,
|
||||||
use std::io::{stdin, stdout};
|
process::{Child, Command, Stdio},
|
||||||
|
};
|
||||||
|
|
||||||
use toml::Value;
|
|
||||||
|
|
||||||
const VALUE: &str = "
|
|
||||||
cursor='> '
|
|
||||||
";
|
|
||||||
|
|
||||||
const VERSION: &str = "0.1.0";
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let value = VALUE.parse::<Value>().unwrap();
|
|
||||||
let motd = "frick";
|
|
||||||
let terminal_cursor = value["cursor"].as_str();
|
|
||||||
|
|
||||||
println!("{}", value);
|
|
||||||
|
|
||||||
clear_term();
|
|
||||||
println!("{}", motd);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
print!("{}", terminal_cursor.unwrap());
|
// use the `>` character as the prompt
|
||||||
let _err = stdout().flush(); // Excplicitly flush to ensure > gets printed
|
// need to explicitly flush this to ensure it prints before read_line
|
||||||
// handle the _err
|
|
||||||
|
print!("~> ");
|
||||||
|
stdout().flush().unwrap();
|
||||||
|
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
stdin().read_line(&mut input).unwrap(); // read_line leaves a trailing newline, which trim removes
|
stdin().read_line(&mut input).unwrap();
|
||||||
let command = input.trim();
|
|
||||||
match command {
|
// read_line leaves a trailing newline, which trim removes
|
||||||
"rush" => {
|
// this needs to be peekable so we can determine when we are on the last command
|
||||||
println!("Version: {}", VERSION);
|
let mut commands = input.trim().split(" | ").peekable();
|
||||||
} // Maybe parse via clap
|
let mut previous_command = None;
|
||||||
"clear()" | "clean()" => {
|
|
||||||
clear_term();
|
while let Some(command) = commands.next() {
|
||||||
}
|
// everything after the first whitespace character is interpreted as args to the command
|
||||||
"random()" => {
|
let mut parts = command.trim().split_whitespace();
|
||||||
println!("1"); // this has been decided as the cryptographically secure random
|
let command = parts.next().unwrap();
|
||||||
}
|
let args = parts;
|
||||||
"script()" => {
|
|
||||||
let mut script = String::new();
|
match command {
|
||||||
loop {
|
"cd" => {
|
||||||
let mut line = String::new();
|
// default to '/' as new directory if one was not provided
|
||||||
stdin().read_line(&mut line).unwrap(); // read_line leaves a trailing
|
let new_dir = args.peekable().peek().map_or("/", |x| *x);
|
||||||
line = line.trim().to_string();
|
let root = Path::new(new_dir);
|
||||||
if line == "end()" {
|
if let Err(e) = env::set_current_dir(&root) {
|
||||||
println!("{}", script);
|
eprintln!("{}", e);
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
// append line to script
|
|
||||||
script = format!("{}\n{}", script, line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
.stdin(stdin)
|
||||||
|
.stdout(stdout)
|
||||||
|
.spawn();
|
||||||
|
|
||||||
|
match output {
|
||||||
|
Ok(output) => {
|
||||||
|
previous_command = Some(output);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
previous_command = None;
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"ls" => {
|
}
|
||||||
println!("bonk");
|
|
||||||
}
|
if let Some(mut final_command) = previous_command {
|
||||||
_ => {
|
// block until the final command has finished
|
||||||
println!("Command {} not found", command);
|
final_command.wait().unwrap();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear_term() {
|
|
||||||
print!("\x1B[2J\x1B[1;1H");
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue