From c117482d4f4e9270dbf50b792ba0e43227a74a5e Mon Sep 17 00:00:00 2001 From: Condorra Date: Sun, 18 Aug 2024 20:58:50 +1000 Subject: [PATCH] Add repeat functionality --- src/command_handler.rs | 14 +++++++++++--- src/lua_state.rs | 7 +++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/command_handler.rs b/src/command_handler.rs index 43f6e0a..882838d 100644 --- a/src/command_handler.rs +++ b/src/command_handler.rs @@ -25,7 +25,7 @@ fn reentrant_command_handler( match command.split_out_command() { None => (), Some((cmd, rest)) => { - if cmd.starts_with('#') { + if let ("#", command_rest) = cmd.split_at(1) { if cmd == "##" { match lua_state.execute(debrace(&join(rest.arguments.iter(), " "))) { Ok(()) => (), @@ -34,9 +34,17 @@ fn reentrant_command_handler( .unwrap_or(()) } } + } else if let Ok(repeat_count) = command_rest.parse::() { + for _ in 0..repeat_count { + reentrant_command_handler( + lua_state, + globals, + term_frame, + &join(rest.arguments.iter(), " "), + ); + } } else { - let cmd = &cmd[1..]; - match lua_state.execute_command(cmd, &rest.arguments) { + match lua_state.execute_command(command_rest, &rest.arguments) { Ok(()) => (), Err(msg) => { echo_to_term_frame(globals, term_frame, &format!("{}\r\n", msg)) diff --git a/src/lua_state.rs b/src/lua_state.rs index a9bc73b..abf5800 100644 --- a/src/lua_state.rs +++ b/src/lua_state.rs @@ -58,8 +58,11 @@ impl LuaState { .try_enter(|ctx| { let commands = Table::from_value(ctx, ctx.get_global(ctx.intern_static(b"commands")))?; - let command_fn = - Function::from_value(ctx, commands.get(ctx, ctx.intern(command.as_bytes())))?; + let command_value: Value = commands.get(ctx, ctx.intern(command.as_bytes())); + if command_value.is_nil() { + Err(anyhow::Error::msg("Unknown command"))?; + } + let command_fn = Function::from_value(ctx, command_value)?; ctx.fetch(&self.exec).restart( ctx, command_fn,