Added a file for zlua and made a new function

This commit is contained in:
GhostedGaming 2024-12-23 23:14:11 -05:00 committed by BitSyndicate
parent 173da4c14a
commit 7e940200c6
4 changed files with 106 additions and 93 deletions

View file

@ -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.")
}
}

View file

@ -1,13 +1,15 @@
use commands::{
ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand, PanicCommmand, ZLua,
ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand, PanicCommmand
};
use handler::{COMMAND_MANAGER, Category};
use zlua::ZLua;
use crate::commands;
pub mod commands;
pub mod handler;
pub mod input;
pub mod zlua;
pub fn setup() {
commands!(
@ -16,7 +18,7 @@ pub fn setup() {
ExitCommand,
CounterCommand,
PanicCommmand,
ZLua
zlua::ZLua
);
let cat = Category::new("cr", "Core", "Core commands");
COMMAND_MANAGER.write().add_category(cat.clone());

View file

@ -0,0 +1,98 @@
use mlua::{Lua, MultiValue, Number};
use rustyline::{error::ReadlineError, DefaultEditor};
use crate::core::repl::handler::Command;
#[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 add = lua.create_function(|_, (number1,number2):(i32,i32)|{
let result = number1 + number2;
println!("{result}");
Ok(())
})?;
let is_equal = 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("isequal", is_equal)?;
let log = lua.create_function(|_, (msg,): (String,)| {
println!("{}", msg);
Ok(())
})?;
globals.set("log", log)?;
let mut editor = DefaultEditor::new().expect("Failed to create editor");
globals.set("add", add)?;
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.")
}
}

View file

@ -1,6 +1,10 @@
use colored::Colorize;
pub fn print_splash() {
println!
("# #
# Welcome to the Zenyx terminal #
# #");
println!(
"{}",
format!(