2023-02-10 11:14:28 +00:00
|
|
|
use mlua::prelude::*;
|
|
|
|
|
|
|
|
use crate::utils::{
|
|
|
|
formatting::{format_label, pretty_format_multi_value},
|
|
|
|
table::TableBuilder,
|
|
|
|
};
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
|
2023-02-10 11:14:28 +00:00
|
|
|
// HACK: We need to preserve the default behavior of the
|
|
|
|
// print and error functions, for pcall and such, which
|
|
|
|
// is really tricky to do from scratch so we will just
|
|
|
|
// proxy the default print and error functions here
|
|
|
|
TableBuilder::new(lua)?
|
|
|
|
.with_function("print", |lua, args: LuaMultiValue| {
|
|
|
|
let formatted = pretty_format_multi_value(&args)?;
|
|
|
|
let print: LuaFunction = lua.named_registry_value("print")?;
|
|
|
|
print.call(formatted)?;
|
|
|
|
Ok(())
|
|
|
|
})?
|
|
|
|
.with_function("info", |lua, args: LuaMultiValue| {
|
|
|
|
let print: LuaFunction = lua.named_registry_value("print")?;
|
|
|
|
print.call(format!(
|
|
|
|
"{}\n{}",
|
|
|
|
format_label("info"),
|
|
|
|
pretty_format_multi_value(&args)?
|
|
|
|
))?;
|
|
|
|
Ok(())
|
|
|
|
})?
|
|
|
|
.with_function("warn", |lua, args: LuaMultiValue| {
|
|
|
|
let print: LuaFunction = lua.named_registry_value("print")?;
|
|
|
|
print.call(format!(
|
|
|
|
"{}\n{}",
|
|
|
|
format_label("warn"),
|
|
|
|
pretty_format_multi_value(&args)?
|
|
|
|
))?;
|
|
|
|
Ok(())
|
|
|
|
})?
|
|
|
|
.with_function("error", |lua, (arg, level): (LuaValue, Option<u32>)| {
|
|
|
|
let error: LuaFunction = lua.named_registry_value("error")?;
|
|
|
|
let multi = arg.to_lua_multi(lua)?;
|
|
|
|
error.call((
|
|
|
|
format!(
|
|
|
|
"{}\n{}",
|
|
|
|
format_label("error"),
|
|
|
|
pretty_format_multi_value(&multi)?
|
|
|
|
),
|
|
|
|
level,
|
|
|
|
))?;
|
|
|
|
Ok(())
|
|
|
|
})?
|
|
|
|
.build_readonly()
|
|
|
|
}
|