parent
40769ec693
commit
c57e5c2d49
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,6 @@ macro_rules! alias {
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
fn hamming_distance(a: &str, b: &str) -> Option<usize> {
|
||||
if a.len() != b.len() {
|
||||
return None;
|
||||
|
@ -71,7 +70,7 @@ fn check_similarity(target: &str) -> Option<String> {
|
|||
let mut best_match: Option<String> = None;
|
||||
let mut best_distance = usize::MAX;
|
||||
|
||||
for (cmd_name,_) in COMMAND_MANAGER.read().get_commands() {
|
||||
for (cmd_name, _) in COMMAND_MANAGER.read().get_commands() {
|
||||
if let Some(hamming_dist) = hamming_distance(target, cmd_name) {
|
||||
if hamming_dist <= max_hamming_distance && hamming_dist < best_distance {
|
||||
best_distance = hamming_dist;
|
||||
|
@ -92,6 +91,7 @@ fn check_similarity(target: &str) -> Option<String> {
|
|||
pub struct CommandManager {
|
||||
pub commands: HashMap<String, Box<dyn Command>>,
|
||||
pub aliases: HashMap<String, String>,
|
||||
pub categories: HashMap<String, Category>,
|
||||
}
|
||||
|
||||
impl CommandManager {
|
||||
|
@ -99,53 +99,99 @@ impl CommandManager {
|
|||
CommandManager {
|
||||
commands: HashMap::new(),
|
||||
aliases: HashMap::new(),
|
||||
categories: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_category(&mut self, category: Category) {
|
||||
self.categories.insert(category.name.clone(), category);
|
||||
}
|
||||
|
||||
pub fn get_commands(&self) -> std::collections::hash_map::Iter<'_, String, Box<dyn Command>> {
|
||||
self.commands.iter()
|
||||
}
|
||||
pub fn execute_command(&self,command: &str,args: Option<Vec<String>>) -> Result<(),anyhow::Error> {
|
||||
|
||||
pub fn execute_command(
|
||||
&self,
|
||||
command: &str,
|
||||
args: Option<Vec<String>>,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
if let Some(command) = self.commands.get(command) {
|
||||
command.execute(args)?;
|
||||
Ok(())
|
||||
} else {
|
||||
println!("Command '{}' not found.", command);
|
||||
let corrected_cmd = check_similarity(command);
|
||||
if corrected_cmd.is_some() {
|
||||
println!("Command: {} was not found. Did you mean {}?",command.red().bold(),corrected_cmd
|
||||
.expect("A command was editied during execution, something has gone seriously wrong").green().bold().italic());
|
||||
return Ok(());
|
||||
}
|
||||
Ok(())
|
||||
Err(anyhow::anyhow!("Command '{}' not found.", command))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute(&self, command: &str,args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||
pub fn execute(&self, command: &str, args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||
match self.aliases.get(command) {
|
||||
Some(command) => self.execute(command,args),
|
||||
Some(command) => self.execute(command, args),
|
||||
// check to see if we are using an alias or the command just doesnt exist
|
||||
None => {
|
||||
self.execute_command(command,args)?;
|
||||
self.execute_command(command, args)?;
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn add_command(&mut self, command: Box<dyn Command>) {
|
||||
self.commands.insert(command.get_name().to_lowercase(), command);
|
||||
self.commands
|
||||
.insert(command.get_name().to_lowercase(), command);
|
||||
}
|
||||
|
||||
pub fn add_command_with_category(&mut self, command: Box<dyn Command>, category: Category) {
|
||||
if self.categories.contains_key(&category.name) {
|
||||
let mut cmd_name = command.get_name().to_lowercase();
|
||||
cmd_name.insert_str(0, &format!("{}_", &&category.uid.to_lowercase()));
|
||||
println!("{}", cmd_name);
|
||||
self.commands.insert(cmd_name, command);
|
||||
} else {
|
||||
panic!("Category {} does not exist", category.name);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_alias(&mut self, alias: &str, command: &str) {
|
||||
self.aliases.insert(alias.to_string(), command.to_string());
|
||||
self.aliases.insert(
|
||||
alias.to_string().to_lowercase(),
|
||||
command.to_string().to_lowercase(),
|
||||
);
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Category {
|
||||
// eg: Zenyx -> Z
|
||||
// eg: core -> cr
|
||||
// eg: exitcmd -> cr_exit
|
||||
// eg: echo -> z_echo
|
||||
pub uid: String,
|
||||
// eg: Zenyx
|
||||
pub name: String,
|
||||
// eg: Zenyx internal commands
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl Category {
|
||||
pub fn new(uid: &str, name: &str, description: &str) -> Self {
|
||||
Self {
|
||||
uid: uid.to_string(),
|
||||
name: name.to_string(),
|
||||
description: description.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Command: Send + Sync {
|
||||
fn execute(&self, args: Option<Vec<String>>) -> Result<(),anyhow::Error>;
|
||||
fn execute(&self, args: Option<Vec<String>>) -> Result<(), anyhow::Error>;
|
||||
fn undo(&self);
|
||||
fn redo(&self);
|
||||
fn get_description(&self) -> String;
|
||||
fn get_name(&self) -> String;
|
||||
fn get_help(&self) -> String;
|
||||
fn get_params(&self) -> String;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -161,10 +161,10 @@ pub fn evaluate_command(input: &str) -> anyhow::Result<()> {
|
|||
} else {
|
||||
None
|
||||
};
|
||||
match COMMAND_MANAGER.read().execute(cmd_name, args) {
|
||||
Ok(_) => continue,
|
||||
Err(e) => return Err(e)
|
||||
}
|
||||
match COMMAND_MANAGER.read().execute(cmd_name, args) {
|
||||
Ok(_) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,12 +1,23 @@
|
|||
use commands::{ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand};
|
||||
use commands::{ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand, PanicCommmand};
|
||||
use handler::{COMMAND_MANAGER, Category};
|
||||
|
||||
use crate::commands;
|
||||
|
||||
pub mod commands;
|
||||
pub mod input;
|
||||
pub mod handler;
|
||||
|
||||
pub mod input;
|
||||
|
||||
pub fn setup() {
|
||||
commands!(HelpCommand,ClearCommand,ExitCommand,ExecFile,CounterCommand);
|
||||
commands!(
|
||||
HelpCommand,
|
||||
ClearCommand,
|
||||
ExitCommand,
|
||||
CounterCommand,
|
||||
PanicCommmand
|
||||
);
|
||||
let cat = Category::new("cr", "Core", "Core commands");
|
||||
COMMAND_MANAGER.write().add_category(cat.clone());
|
||||
COMMAND_MANAGER
|
||||
.write()
|
||||
.add_command_with_category(Box::new(ExecFile), cat.clone());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue