Lua runCommand improvements

* Return error codes (e.g. CR_FAILURE) when a command fails instead of output
* Make dfhack.runCommand() take a list of arguments as well
develop
lethosor 2014-06-10 13:41:01 -04:00
parent e4876ed752
commit 143b1e3469
2 changed files with 27 additions and 5 deletions

@ -2230,10 +2230,16 @@ static int internal_getDir(lua_State *L)
static int internal_runCommand(lua_State *L) static int internal_runCommand(lua_State *L)
{ {
CoreSuspender suspend;
luaL_checktype(L, 1, LUA_TSTRING); luaL_checktype(L, 1, LUA_TSTRING);
std::string command = lua_tostring(L, 1); std::string command = lua_tostring(L, 1);
buffered_color_ostream out; buffered_color_ostream out;
Core::getInstance().runCommand(out, command); command_result res = Core::getInstance().runCommand(out, command);
if (res != CR_OK)
{
lua_pushinteger(L, (int)res);
return 1;
}
auto fragments = out.fragments(); auto fragments = out.fragments();
lua_newtable(L); lua_newtable(L);
int i = 1; int i = 1;

@ -10,6 +10,14 @@ local dfhack = dfhack
local base_env = dfhack.BASE_G local base_env = dfhack.BASE_G
local _ENV = base_env local _ENV = base_env
CR_LINK_FAILURE = -3
CR_NEEDS_CONSOLE = -2
CR_NOT_IMPLEMENTED = -1
CR_OK = 0
CR_FAILURE = 1
CR_WRONG_USAGE = 2
CR_NOT_FOUND = 3
-- Console color constants -- Console color constants
COLOR_RESET = -1 COLOR_RESET = -1
@ -358,11 +366,19 @@ function dfhack.run_script(name,...)
return f(...) return f(...)
end end
function dfhack.run_command(command, ...) function dfhack.run_command(...)
command = command .. ' ' .. table.concat({...}, ' ') args = {...}
fragments = internal.runCommand(command) if type(args[1]) == 'table' then
command = table.concat(args[1], ' ')
else
command = table.concat(args, ' ')
end
result = internal.runCommand(command)
if type(result) == 'number' then
return result
end
output = "" output = ""
for i, f in pairs(fragments) do for i, f in pairs(result) do
output = output .. f[2] output = output .. f[2]
end end
return output return output