Implement runCommand in Lua API

develop
lethosor 2014-06-07 20:15:49 -04:00
parent 23acf276b4
commit 2242d42c9c
1 changed files with 26 additions and 0 deletions

@ -2227,6 +2227,31 @@ static int internal_getDir(lua_State *L)
} }
return 1; return 1;
} }
static int internal_runCommand(lua_State *L)
{
luaL_checktype(L, 1, LUA_TSTRING);
std::string command = lua_tostring(L, 1);
buffered_color_ostream out;
Core::getInstance().runCommand(out, command);
auto fragments = out.fragments();
lua_newtable(L);
int i = 1;
for (auto iter = fragments.begin(); iter != fragments.end(); iter++, i++)
{
int color = iter->first;
std::string output = iter->second;
lua_createtable(L, 2, 0);
lua_pushinteger(L, color);
lua_rawseti(L, -2, 1);
lua_pushstring(L, output.c_str());
lua_rawseti(L, -2, 2);
lua_rawseti(L, -2, i);
}
lua_pushvalue(L, -1);
return 1;
}
static const luaL_Reg dfhack_internal_funcs[] = { static const luaL_Reg dfhack_internal_funcs[] = {
{ "getAddress", internal_getAddress }, { "getAddress", internal_getAddress },
{ "setAddress", internal_setAddress }, { "setAddress", internal_setAddress },
@ -2240,6 +2265,7 @@ static const luaL_Reg dfhack_internal_funcs[] = {
{ "memscan", internal_memscan }, { "memscan", internal_memscan },
{ "diffscan", internal_diffscan }, { "diffscan", internal_diffscan },
{ "getDir", internal_getDir }, { "getDir", internal_getDir },
{ "runCommand", internal_runCommand },
{ NULL, NULL } { NULL, NULL }
}; };