2024-08-16 21:39:39 +10:00
|
|
|
use itertools::join;
|
|
|
|
|
|
|
|
use crate::{
|
2024-08-16 23:22:21 +10:00
|
|
|
echo_to_term_frame, lua_state::LuaState, parsing::parse_commands, GlobalCell, TermFrame,
|
2024-08-16 21:39:39 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
fn reentrant_command_handler(
|
|
|
|
lua_state: &mut LuaState,
|
2024-08-16 23:22:21 +10:00
|
|
|
globals: &GlobalCell,
|
2024-08-16 21:39:39 +10:00
|
|
|
term_frame: &TermFrame,
|
|
|
|
command_in: &str,
|
|
|
|
) {
|
2024-08-17 16:42:01 +10:00
|
|
|
echo_to_term_frame(globals, term_frame, "\r").unwrap_or(());
|
2024-08-16 21:39:39 +10:00
|
|
|
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(), " ")) {
|
2024-08-17 16:42:01 +10:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(msg) => {
|
|
|
|
echo_to_term_frame(globals, term_frame, &format!("{}\r\n", msg))
|
|
|
|
.unwrap_or(())
|
|
|
|
}
|
2024-08-16 21:39:39 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 23:22:21 +10:00
|
|
|
pub fn command_handler(globals: &GlobalCell, term_frame: &TermFrame, command_in: &str) {
|
|
|
|
match globals.lua_engine.try_borrow_mut() {
|
2024-08-16 21:39:39 +10:00
|
|
|
Err(_) => echo_to_term_frame(
|
2024-08-16 23:22:21 +10:00
|
|
|
globals,
|
2024-08-16 21:39:39 +10:00
|
|
|
term_frame,
|
2024-08-17 16:42:01 +10:00
|
|
|
"Attempt to re-enter command handler during processing.\r\n",
|
2024-08-16 21:39:39 +10:00
|
|
|
)
|
|
|
|
.unwrap_or(()), // Ignore error handling error.
|
|
|
|
Ok(mut lua_state_m) => {
|
2024-08-16 23:22:21 +10:00
|
|
|
reentrant_command_handler(&mut lua_state_m, globals, term_frame, command_in)
|
2024-08-16 21:39:39 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|