forked from nonsensical-dev/zenyx-engine
parent
0e255ea359
commit
f286a2b624
17 changed files with 405 additions and 106 deletions
|
@ -4,9 +4,8 @@ use anyhow::anyhow;
|
|||
use parking_lot::RwLock;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::core::repl::handler::COMMAND_MANAGER;
|
||||
|
||||
use super::{handler::Command, input::tokenize};
|
||||
use crate::core::repl::handler::COMMAND_MANAGER;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct HelpCommand;
|
||||
|
@ -19,7 +18,7 @@ impl Command for HelpCommand {
|
|||
for (_, command) in manager.get_commands() {
|
||||
println!(
|
||||
"Command: {}\n\tDescription: {}\n\tParameters: {}\n\tHelp: {}\n",
|
||||
command.get_name(),
|
||||
command.get_name().to_lowercase(),
|
||||
command.get_description(),
|
||||
command.get_params(),
|
||||
command.get_help()
|
||||
|
@ -62,7 +61,9 @@ impl Command for ClearCommand {
|
|||
fn execute(&self, _args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||
println!("Clearing screen..., running command");
|
||||
let _result = if cfg!(target_os = "windows") {
|
||||
std::process::Command::new("cmd").args(["/c", "cls"]).spawn()
|
||||
std::process::Command::new("cmd")
|
||||
.args(["/c", "cls"])
|
||||
.spawn()
|
||||
} else {
|
||||
std::process::Command::new("clear").spawn()
|
||||
};
|
||||
|
@ -97,14 +98,13 @@ impl Command for ExitCommand {
|
|||
fn execute(&self, args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||
match args {
|
||||
Some(args) => {
|
||||
|
||||
let exit_code = args[0].parse()?;
|
||||
std::process::exit(exit_code);
|
||||
// Ok(())
|
||||
},
|
||||
}
|
||||
None => {
|
||||
std::process::exit(0);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,27 +136,33 @@ impl Command for ExitCommand {
|
|||
pub struct ExecFile;
|
||||
|
||||
impl Command for ExecFile {
|
||||
fn execute(&self, args: Option<Vec<String>>) -> Result<(),anyhow::Error> {
|
||||
fn execute(&self, args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||
match args {
|
||||
Some(args) => {
|
||||
|
||||
let file_path = PathBuf::from_str(&args[0])?;
|
||||
if file_path.extension().is_some() && file_path.extension().unwrap() != "zensh" {
|
||||
return Err(anyhow!("Selected file was not a zensh file"));
|
||||
} else {
|
||||
let zscript = fs::read_to_string(file_path)?;
|
||||
if let Ok(command) = eval(zscript) {
|
||||
println!("{:#?}",command);
|
||||
for (cmd_name,cmd_args) in command {
|
||||
COMMAND_MANAGER.read().execute(&cmd_name, cmd_args)?
|
||||
println!("{:#?}", command);
|
||||
for (cmd_name, cmd_args) in command {
|
||||
match COMMAND_MANAGER.read().execute(&cmd_name, cmd_args) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
println!(
|
||||
"Error executing command returned an error: {}. Aborting script",
|
||||
e
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
None => {
|
||||
Err(anyhow!("Not enough argumentss"))
|
||||
},
|
||||
}
|
||||
None => Err(anyhow!("Not enough argumentss")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,37 +187,6 @@ impl Command for ExecFile {
|
|||
}
|
||||
}
|
||||
|
||||
fn eval(input: String) -> Result<Vec<(String,Option<Vec<String>>)>, anyhow::Error> {
|
||||
if input.trim().is_empty() {
|
||||
return Err(anyhow!("Input was empty"));
|
||||
}
|
||||
|
||||
let pattern = Regex::new(r"[;|\n]").unwrap();
|
||||
let commands: Vec<&str> = pattern.split(&input).collect();
|
||||
let mut evaluted = vec![];
|
||||
|
||||
for command in commands {
|
||||
let command = command.trim();
|
||||
if command.is_empty() {
|
||||
println!("Empty command, skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
let tokens = tokenize(command);
|
||||
if tokens.is_empty() {
|
||||
println!("Empty command, skipping.");
|
||||
continue;
|
||||
}
|
||||
let cmd_name = &tokens[0];
|
||||
let args: Option<Vec<String>> = if tokens.len() > 1 {
|
||||
Some(tokens[1..].iter().map(|s| s.to_string()).collect())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
evaluted.push((cmd_name.to_owned(),args));
|
||||
}
|
||||
Ok(evaluted)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct CounterCommand {
|
||||
|
@ -250,4 +225,73 @@ impl Command for CounterCommand {
|
|||
fn get_name(&self) -> String {
|
||||
String::from("count")
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct PanicCommmand;
|
||||
impl Command for PanicCommmand {
|
||||
fn execute(&self, args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||
if args.is_some() {
|
||||
let panic_msg = &args.unwrap()[0];
|
||||
panic!("{}", panic_msg)
|
||||
}
|
||||
let option: Option<i32> = None;
|
||||
println!("Unwrapping None: {}", option.unwrap());
|
||||
panic!("Panic command was called")
|
||||
}
|
||||
|
||||
fn undo(&self) {}
|
||||
|
||||
fn redo(&self) {}
|
||||
|
||||
fn get_description(&self) -> String {
|
||||
String::from("causes a panic with your provided message")
|
||||
}
|
||||
|
||||
fn get_name(&self) -> String {
|
||||
String::from("panic")
|
||||
}
|
||||
|
||||
fn get_help(&self) -> String {
|
||||
String::from("")
|
||||
}
|
||||
|
||||
fn get_params(&self) -> String {
|
||||
String::from("optional: panic msg")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
fn eval(input: String) -> Result<Vec<(String,Option<Vec<String>>)>, anyhow::Error> {
|
||||
if input.trim().is_empty() {
|
||||
return Err(anyhow!("Input was empty"));
|
||||
}
|
||||
|
||||
let pattern = Regex::new(r"[;|\n]").unwrap();
|
||||
let commands: Vec<&str> = pattern.split(&input).collect();
|
||||
let mut evaluted = vec![];
|
||||
|
||||
for command in commands {
|
||||
let command = command.trim();
|
||||
if command.is_empty() {
|
||||
println!("Empty command, skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
let tokens = tokenize(command);
|
||||
if tokens.is_empty() {
|
||||
println!("Empty command, skipping.");
|
||||
continue;
|
||||
}
|
||||
let cmd_name = &tokens[0];
|
||||
let args: Option<Vec<String>> = if tokens.len() > 1 {
|
||||
Some(tokens[1..].iter().map(|s| s.to_string()).collect())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
evaluted.push((cmd_name.to_owned(),args));
|
||||
}
|
||||
Ok(evaluted)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue