worldwideportal/src/command_handler.rs

43 lines
1.4 KiB
Rust
Raw Normal View History

use itertools::join;
use crate::{
echo_to_term_frame, lua_state::LuaState, parsing::parse_commands, GlobalCell, TermFrame,
};
fn reentrant_command_handler(
lua_state: &mut LuaState,
globals: &GlobalCell,
term_frame: &TermFrame,
command_in: &str,
) {
web_sys::console::log_1(&"Inside command handler".into());
echo_to_term_frame(globals, term_frame, "Hello World!\n").unwrap_or(());
for command in parse_commands(command_in).commands {
match command.split_out_command() {
None => (),
Some((cmd, rest)) => {
if cmd == "##" {
match lua_state.execute(&join(rest.arguments.iter(), " ")) {
Ok(msg) => echo_to_term_frame(globals, term_frame, &msg).unwrap_or(()),
Err(msg) => echo_to_term_frame(globals, term_frame, &msg).unwrap_or(()),
}
}
}
}
}
}
pub fn command_handler(globals: &GlobalCell, term_frame: &TermFrame, command_in: &str) {
match globals.lua_engine.try_borrow_mut() {
Err(_) => echo_to_term_frame(
globals,
term_frame,
"Attempt to re-enter command handler during processing.\n",
)
.unwrap_or(()), // Ignore error handling error.
Ok(mut lua_state_m) => {
reentrant_command_handler(&mut lua_state_m, globals, term_frame, command_in)
}
}
}