Files
LLeMbas-TTY/lua-api-crates/logging/src/lib.rs
T
Wez Furlong 2f14d640e8 config: split out lua functions into their own crates
This shaves off some build time and allows more parallism in the build.
2022-05-19 06:48:09 -07:00

68 lines
1.8 KiB
Rust

use config::lua::get_or_create_module;
use config::lua::mlua::{Lua, Value, Variadic};
use luahelper::ValuePrinter;
pub fn register(lua: &Lua) -> anyhow::Result<()> {
let wezterm_mod = get_or_create_module(lua, "wezterm")?;
wezterm_mod.set(
"log_error",
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::error!("lua: {}", output);
Ok(())
})?,
)?;
wezterm_mod.set(
"log_info",
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::info!("lua: {}", output);
Ok(())
})?,
)?;
wezterm_mod.set(
"log_warn",
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::warn!("lua: {}", output);
Ok(())
})?,
)?;
lua.globals().set(
"print",
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::info!("lua: {}", output);
Ok(())
})?,
)?;
Ok(())
}
fn print_helper(args: Variadic<Value>) -> String {
let mut output = String::new();
for (idx, item) in args.into_iter().enumerate() {
if idx > 0 {
output.push(' ');
}
match item {
Value::String(s) => match s.to_str() {
Ok(s) => output.push_str(s),
Err(_) => {
let item = String::from_utf8_lossy(s.as_bytes());
output.push_str(&item);
}
},
item @ _ => {
let item = format!("{:#?}", ValuePrinter(item));
output.push_str(&item);
}
}
}
output
}