Lua pushkey and getlin

Allow lua nh.pushkey to push multiple keys,
make getlin return the keys in the command queue.
This commit is contained in:
Pasi Kallinen
2026-01-22 18:16:51 +02:00
parent b9f8f845fc
commit 49a87bd09c
3 changed files with 35 additions and 1 deletions

View File

@@ -1381,7 +1381,10 @@ nhl_pushkey(lua_State *L)
if (argc == 1) {
const char *key = luaL_checkstring(L, 1);
cmdq_add_key(CQ_CANNED, key[0]);
while (*key) {
cmdq_add_key(CQ_CANNED, *key);
key++;
}
}
return 0;

View File

@@ -1867,6 +1867,27 @@ void
getlin(const char *query, char *bufp)
{
boolean old_bot_disabled = gb.bot_disabled;
char *obufp = bufp;
boolean got_cmdq = FALSE;
struct _cmd_queue *cmdq = NULL;
while ((cmdq = cmdq_pop()) != 0) {
if (cmdq->typ == CMDQ_KEY) {
got_cmdq = TRUE;
*bufp = (cmdq->key != '\n') ? cmdq->key : '\0';
bufp++;
if (cmdq->key == '\n')
break;
} else {
break;
}
}
if (got_cmdq) {
*bufp = '\0';
pline("%s %s", query, obufp);
return;
}
program_state.in_getlin = 1;
gb.bot_disabled = TRUE;

View File

@@ -121,3 +121,13 @@ for func, fval in pairs(tests) do
end
end
end
function test_getlin()
nh.pushkey("AbC");
local str = nh.getlin("What?");
if str ~= "AbC" then
error("nh.getlin fail, got \"" .. str .. "\"");
end
end
test_getlin();