|
|
|
@ -1,38 +1,34 @@
|
|
|
|
|
use mlua::{Function, Result as lResult, Table};
|
|
|
|
|
use mlua::{UserDataFields, Lua as Luau, Result as lResult, Table, UserData};
|
|
|
|
|
use const_format::str_split;
|
|
|
|
|
use crossterm::style::Stylize;
|
|
|
|
|
|
|
|
|
|
use crate::vm::LuauVm;
|
|
|
|
|
|
|
|
|
|
macro_rules! foreground_styles_luau {
|
|
|
|
|
($self:expr, $style_table:expr, $($color:ident)+) => {
|
|
|
|
|
($luau:expr, $style_table:expr, $($color:ident)+) => {
|
|
|
|
|
$(
|
|
|
|
|
$style_table.raw_set(stringify!($color).to_ascii_uppercase(), $self.vm.create_function(|_, text: String| -> lResult<String> {
|
|
|
|
|
$style_table.raw_set(stringify!($color).to_ascii_uppercase(), $luau.create_function(|_, text: String| -> lResult<String> {
|
|
|
|
|
Ok(text.$color().to_string())
|
|
|
|
|
})?)?;
|
|
|
|
|
)+
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
macro_rules! background_styles_luau {
|
|
|
|
|
($self:expr, $style_table:expr, $($color:ident)+) => {
|
|
|
|
|
($luau:expr, $style_table:expr, $($color:ident)+) => {
|
|
|
|
|
$(
|
|
|
|
|
$style_table.raw_set(
|
|
|
|
|
str_split!(stringify!($color), "_")[1..].join("_").to_ascii_uppercase(),
|
|
|
|
|
$self.vm.create_function(|_, text: String| -> lResult<String> {
|
|
|
|
|
$luau.create_function(|_, text: String| -> lResult<String> {
|
|
|
|
|
Ok(text.$color().to_string())
|
|
|
|
|
})?)?;
|
|
|
|
|
)+
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait Colors {
|
|
|
|
|
fn background(&self, term_out_table: &Table) -> lResult<()>;
|
|
|
|
|
fn foreground(&self, term_out_table: &Table) -> lResult<()>;
|
|
|
|
|
}
|
|
|
|
|
impl Colors for LuauVm {
|
|
|
|
|
fn background(&self, term_out_table: &Table) -> lResult<()> {
|
|
|
|
|
let foreground_table = self.vm.create_table()?;
|
|
|
|
|
foreground_styles_luau!(self, foreground_table,
|
|
|
|
|
fn text_styles_funcs(luau: &Luau) -> lResult<(Table, Table)> {
|
|
|
|
|
let foreground_table = luau.create_table()?;
|
|
|
|
|
let background_table = luau.create_table()?;
|
|
|
|
|
foreground_styles_luau!(luau, foreground_table,
|
|
|
|
|
dark_grey dark_red dark_green dark_cyan
|
|
|
|
|
dark_yellow dark_magenta dark_blue
|
|
|
|
|
red grey black green yellow
|
|
|
|
@ -44,12 +40,7 @@ impl Colors for LuauVm {
|
|
|
|
|
underline_blue underline_magenta underline_cyan underline_white
|
|
|
|
|
bold
|
|
|
|
|
);
|
|
|
|
|
term_out_table.raw_set("FOREGROUND", foreground_table)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn foreground(&self, term_out_table: &Table) -> lResult<()> {
|
|
|
|
|
let background_table = self.vm.create_table()?;
|
|
|
|
|
background_styles_luau!(self, background_table,
|
|
|
|
|
background_styles_luau!(luau, background_table,
|
|
|
|
|
on_dark_grey on_dark_red on_dark_green on_dark_cyan
|
|
|
|
|
on_dark_yellow on_dark_magenta on_dark_blue
|
|
|
|
|
on_red on_grey on_black
|
|
|
|
@ -57,54 +48,46 @@ impl Colors for LuauVm {
|
|
|
|
|
on_blue on_magenta
|
|
|
|
|
on_cyan on_white
|
|
|
|
|
);
|
|
|
|
|
term_out_table.raw_set("BACKGROUND", background_table)
|
|
|
|
|
}
|
|
|
|
|
Ok((foreground_table, background_table))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait Write {
|
|
|
|
|
fn write(&self) -> lResult<Function>;
|
|
|
|
|
fn write_error(&self) -> lResult<Function>;
|
|
|
|
|
fn write_error_ln(&self) -> lResult<Function>;
|
|
|
|
|
}
|
|
|
|
|
impl Write for LuauVm {
|
|
|
|
|
fn write(&self) -> lResult<Function> {
|
|
|
|
|
self.vm.create_function(|_, s: String| -> lResult<()> {
|
|
|
|
|
fn fields_write_funcs<F: UserDataFields<Terminal>>(fields: &mut F) {
|
|
|
|
|
fields.add_field_method_get("WRITE", |luau, _| luau.create_function(|_, s: String| -> lResult<()> {
|
|
|
|
|
print!("{s}");
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
fn write_error(&self) -> lResult<Function> {
|
|
|
|
|
self.vm.create_function(|_, s: String| -> lResult<()> {
|
|
|
|
|
}));
|
|
|
|
|
fields.add_field_method_get("WRITE_ERROR", |luau, _| luau.create_function(|_, s: String| -> lResult<()> {
|
|
|
|
|
eprint!("{s}");
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
fn write_error_ln(&self) -> lResult<Function> {
|
|
|
|
|
self.vm.create_function(|_, s: String| -> lResult<()> {
|
|
|
|
|
}));
|
|
|
|
|
fields.add_field_method_get("WRITE_ERROR_LN", |luau, _| luau.create_function(|_, s: String| -> lResult<()> {
|
|
|
|
|
eprintln!("{s}");
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait Terminal {
|
|
|
|
|
fn out(&self) -> lResult<Table>;
|
|
|
|
|
struct Terminal;
|
|
|
|
|
impl UserData for Terminal {
|
|
|
|
|
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
|
|
|
|
fields_write_funcs(fields);
|
|
|
|
|
fields.add_field_method_get("OUT", |luau, _| {
|
|
|
|
|
let (foreground, background) = text_styles_funcs(luau)?;
|
|
|
|
|
let out_table = luau.create_table()?;
|
|
|
|
|
out_table.raw_set("FOREGROUND", foreground)?;
|
|
|
|
|
out_table.raw_set("BACKGROUND", background)?;
|
|
|
|
|
Ok(out_table)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait TerminalGlobal {
|
|
|
|
|
fn global_terminal(&self, luau_globals: &Table) -> lResult<()>;
|
|
|
|
|
}
|
|
|
|
|
impl Terminal for LuauVm {
|
|
|
|
|
fn out(&self) -> lResult<Table> {
|
|
|
|
|
let term_out_table = self.vm.create_table()?;
|
|
|
|
|
self.background(&term_out_table)?;
|
|
|
|
|
self.foreground(&term_out_table)?;
|
|
|
|
|
Ok(term_out_table)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TerminalGlobal for LuauVm {
|
|
|
|
|
fn global_terminal(&self, luau_globals: &Table) -> lResult<()> {
|
|
|
|
|
let term_table = self.vm.create_table()?;
|
|
|
|
|
term_table.raw_set("OUT", self.out()?)?;
|
|
|
|
|
term_table.raw_set("WRITE", self.write()?)?;
|
|
|
|
|
term_table.raw_set("WRITE_ERROR", self.write_error()?)?;
|
|
|
|
|
term_table.raw_set("WRITE_ERROR_LN", self.write_error_ln()?)?;
|
|
|
|
|
luau_globals.raw_set("TERMINAL", term_table)
|
|
|
|
|
luau_globals.raw_set("TERMINAL", Terminal)
|
|
|
|
|
}
|
|
|
|
|
}
|