MCPcopy Create free account
hub / github.com/NetHack/NetHack / nhl_menu

Function nhl_menu

src/nhlua.c:720–806  ·  view source on GitHub ↗

selected = menu("prompt",default,pickX,{"a"="option a", "b"="option b",...}) pickX = 0,1,2, or "none", "one", "any" (PICK_X in code) selected = menu("prompt", default, pickX, {{key:"a", text:"option a"},{key:"b", text:"option b"},... }) */

Source from the content-addressed store, hash-verified

718 {{key:"a", text:"option a"},{key:"b", text:"option b"},... })
719 */
720staticfn int
721nhl_menu(lua_State *L)
722{
723 static const char *const pickX[] = { "none", "one", "any" }; /* PICK_x */
724 int argc = lua_gettop(L);
725 const char *prompt;
726 const char *defval = "";
727 int pick = PICK_ONE, pick_cnt;
728 winid tmpwin;
729 anything any;
730 menu_item *picks = (menu_item *) 0;
731 int clr = NO_COLOR;
732
733 if (argc < 2 || argc > 4) {
734 nhl_error(L, "Wrong args");
735 /*NOTREACHED*/
736 return 0;
737 }
738
739 prompt = luaL_checkstring(L, 1);
740 if (lua_isstring(L, 2))
741 defval = luaL_checkstring(L, 2);
742 if (lua_isstring(L, 3))
743 pick = luaL_checkoption(L, 3, "one", pickX);
744 luaL_checktype(L, argc, LUA_TTABLE);
745
746 tmpwin = create_nhwindow(NHW_MENU);
747 start_menu(tmpwin, MENU_BEHAVE_STANDARD);
748
749 lua_pushnil(L); /* first key */
750 while (lua_next(L, argc) != 0) {
751 const char *str = "";
752 const char *key = "";
753
754 /* key @ index -2, value @ index -1 */
755 if (lua_istable(L, -1)) {
756 lua_pushliteral(L, "key");
757 lua_gettable(L, -2);
758 key = lua_tostring(L, -1);
759 lua_pop(L, 1);
760
761 lua_pushliteral(L, "text");
762 lua_gettable(L, -2);
763 str = lua_tostring(L, -1);
764 lua_pop(L, 1);
765
766 /* TODO: glyph, attr, accel, group accel (all optional) */
767 } else if (lua_isstring(L, -1)) {
768 str = luaL_checkstring(L, -1);
769 key = luaL_checkstring(L, -2);
770 }
771
772 any = cg.zeroany;
773 if (*key)
774 any.a_char = key[0];
775 add_menu(tmpwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr, str,
776 (*defval && *key && defval[0] == key[0])
777 ? MENU_ITEMFLAGS_SELECTED

Callers

nothing calls this directly

Calls 3

nhl_errorFunction · 0.85
add_menuFunction · 0.85
select_menuFunction · 0.85

Tested by

no test coverage detected