2024-08-16 21:39:39 +10:00
|
|
|
use crate::{
|
2024-09-07 22:05:01 +10:00
|
|
|
echo_to_term_frame, lua_engine::LuaState, parsing::parse_commands, GlobalMemoCell, TermFrame,
|
2024-08-16 21:39:39 +10:00
|
|
|
};
|
2024-09-17 22:14:07 +10:00
|
|
|
use itertools::{join, Itertools};
|
2024-08-16 21:39:39 +10:00
|
|
|
|
2024-08-23 23:37:58 +10:00
|
|
|
pub fn debrace(inp: &str) -> &str {
|
2024-08-17 23:19:41 +10:00
|
|
|
let v = inp.trim();
|
2024-09-20 22:59:00 +10:00
|
|
|
if v.starts_with('{') && v.ends_with('}') {
|
2024-08-17 23:19:41 +10:00
|
|
|
&v[1..(v.len() - 1)]
|
|
|
|
} else {
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:39:39 +10:00
|
|
|
fn reentrant_command_handler(
|
|
|
|
lua_state: &mut LuaState,
|
2024-08-23 23:37:58 +10:00
|
|
|
globals: &GlobalMemoCell,
|
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-17 21:36:15 +10:00
|
|
|
lua_state.set_current_frame(term_frame);
|
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)) => {
|
2024-08-18 20:58:50 +10:00
|
|
|
if let ("#", command_rest) = cmd.split_at(1) {
|
2024-08-17 23:19:41 +10:00
|
|
|
if cmd == "##" {
|
2024-09-20 22:59:00 +10:00
|
|
|
match lua_state.execute(&rest.forget_guards().to_string()) {
|
2024-08-17 23:19:41 +10:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(msg) => {
|
|
|
|
echo_to_term_frame(globals, term_frame, &format!("{}\r\n", msg))
|
|
|
|
.unwrap_or(())
|
|
|
|
}
|
|
|
|
}
|
2024-08-18 20:58:50 +10:00
|
|
|
} else if let Ok(repeat_count) = command_rest.parse::<u16>() {
|
|
|
|
for _ in 0..repeat_count {
|
|
|
|
reentrant_command_handler(
|
|
|
|
lua_state,
|
|
|
|
globals,
|
|
|
|
term_frame,
|
|
|
|
&join(rest.arguments.iter(), " "),
|
|
|
|
);
|
|
|
|
}
|
2024-08-17 23:19:41 +10:00
|
|
|
} else {
|
2024-09-20 22:59:00 +10:00
|
|
|
match lua_state.execute_command(command_rest, &rest) {
|
2024-08-17 23:19:41 +10:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(msg) => {
|
|
|
|
echo_to_term_frame(globals, term_frame, &format!("{}\r\n", msg))
|
|
|
|
.unwrap_or(())
|
|
|
|
}
|
2024-08-17 16:42:01 +10:00
|
|
|
}
|
2024-08-16 21:39:39 +10:00
|
|
|
}
|
2024-09-17 22:14:07 +10:00
|
|
|
} else {
|
|
|
|
// A normal command. Try dispatching it to the frame object in Lua...
|
|
|
|
match lua_state
|
|
|
|
.dispatch_normal_command(term_frame, &command.arguments.iter().join(" "))
|
|
|
|
{
|
|
|
|
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-23 23:37:58 +10:00
|
|
|
pub fn command_handler(globals: &GlobalMemoCell, term_frame: &TermFrame, command_in: &str) {
|
2024-08-16 23:22:21 +10:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|