forked from nonsensical-dev/zenyx-engine
Added a file for zlua and made a new function
This commit is contained in:
parent
3766da6bcf
commit
067048cf54
4 changed files with 106 additions and 93 deletions
|
@ -293,94 +293,3 @@ fn eval(input: String) -> Result<Vec<(String, Option<Vec<String>>)>, anyhow::Err
|
|||
}
|
||||
Ok(evaluted)
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct ZLua;
|
||||
|
||||
impl Command for ZLua {
|
||||
fn execute(&self, args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||
let time = chrono::Local::now().format("%H:%M:%S.%3f").to_string();
|
||||
let prompt = format!("[{}/{}] {}", time, "ZLUA", ">>\t");
|
||||
let lua = Lua::new();
|
||||
let globals = lua.globals();
|
||||
let sum = lua.create_function(|_, (list1, list2): (i32, i32)| {
|
||||
// This function just checks whether two string lists are equal, and in an inefficient way.
|
||||
// Lua callbacks return `mlua::Result`, an Ok value is a normal return, and an Err return
|
||||
// turns into a Lua 'error'. Again, any type that is convertible to Lua may be returned.
|
||||
Ok(list1 == list2)
|
||||
})?;
|
||||
globals.set("sum", sum)?;
|
||||
let log = lua.create_function(|_, (msg,): (String,)| {
|
||||
println!("{}", msg);
|
||||
Ok(())
|
||||
})?;
|
||||
globals.set("log", log)?;
|
||||
let mut editor = DefaultEditor::new().expect("Failed to create editor");
|
||||
|
||||
loop {
|
||||
let mut prompt = &prompt;
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
match editor.readline(prompt) {
|
||||
Ok(input) => line.push_str(&input),
|
||||
Err(ReadlineError::Interrupted) => {
|
||||
println!("Exiting ZLUA shell...");
|
||||
return Ok(());
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
|
||||
match lua.load(&line).eval::<MultiValue>() {
|
||||
Ok(values) => {
|
||||
editor.add_history_entry(line).unwrap();
|
||||
println!(
|
||||
"{}",
|
||||
values
|
||||
.iter()
|
||||
.map(|value| format!("{:#?}", value))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\t")
|
||||
);
|
||||
break;
|
||||
}
|
||||
Err(mlua::Error::SyntaxError {
|
||||
incomplete_input: true,
|
||||
..
|
||||
}) => {
|
||||
// continue reading input and append it to `line`
|
||||
line.push_str("\n"); // separate input lines
|
||||
prompt = prompt;
|
||||
}
|
||||
|
||||
Err(e) => {
|
||||
eprintln!("error: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn undo(&self) {}
|
||||
|
||||
fn redo(&self) {}
|
||||
|
||||
fn get_description(&self) -> String {
|
||||
String::from("Runs the ZLua interpreter")
|
||||
}
|
||||
|
||||
fn get_name(&self) -> String {
|
||||
String::from("zlua")
|
||||
}
|
||||
|
||||
fn get_help(&self) -> String {
|
||||
String::from("zlua")
|
||||
}
|
||||
|
||||
fn get_params(&self) -> String {
|
||||
String::from("No parameters required.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue