Files
rusty-path-of-building/src/pob/api/console.rs
T
meehl cc6de120f2 Refactor context handling and redraw requests
Remove unsafe pointer shenanigans and give Lua instance ownership over
`Context`. Event handlers now directly mutate Context instead of using a
shared app state which is then hooked up to Context via unsafe pointers.
The removal of a shared state means stages (previously called "modes")
are now responsible for keeping their own internal state.

Additionally, clean up draw request handling and module hierarchy.
2026-08-23 23:55:43 +02:00

57 lines
1.7 KiB
Rust

use mlua::{Function, Lua, MultiValue, Result as LuaResult, Table, Value};
use std::io::{Write, stdout};
pub fn console_printf(l: &Lua, (fmt, args): (String, MultiValue)) -> LuaResult<()> {
// uses lua's builtin string.format function
let string_module: Table = l.globals().get("string")?;
let format_func: Function = string_module.get("format")?;
let formatted_string = format_func.call::<String>((fmt, args))?;
println!("{formatted_string}");
Ok(())
}
pub fn console_execute(_l: &Lua, _cmd: String) -> LuaResult<()> {
Ok(())
}
pub fn console_clear(_l: &Lua, _: ()) -> LuaResult<()> {
Ok(())
}
pub fn console_print_table(
_l: &Lua,
(table, no_recursive): (Table, Option<bool>),
) -> LuaResult<()> {
print_table(&table, 0, !no_recursive.unwrap_or(true))?;
Ok(())
}
fn print_table(table: &Table, indent: usize, recursive: bool) -> LuaResult<()> {
let mut lock = stdout().lock();
writeln!(lock, "{{")?;
for pair in table.pairs::<Value, Value>() {
let inner_ind = indent + 2;
let (key, value) = pair?;
if key.is_string() {
write!(lock, "{0:>1$}\"{2}\" = ", "", inner_ind, key.to_string()?)?;
} else {
write!(lock, "{0:>1$}{2} = ", "", inner_ind, key.to_string()?)?;
}
if value.is_table() {
if recursive {
print_table(value.as_table().unwrap(), indent + 2, recursive)?;
} else {
writeln!(lock, "{}", value.to_string()?)?;
}
} else if value.is_string() {
writeln!(lock, "\"{}\"", value.to_string()?)?;
} else {
writeln!(lock, "{}", value.to_string()?)?;
}
}
writeln!(lock, "{0:>1$}}}", "", indent)?;
Ok(())
}