Optimize Lua's internal.runCommand() when printing directly to the console

This also makes commands run with `run_command()` detect the console properly (notably used by `df2console()`)
develop
lethosor 2020-11-20 17:57:54 -05:00
parent fb44b26b47
commit a9bb11c145
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
2 changed files with 36 additions and 26 deletions

@ -2822,13 +2822,25 @@ static int internal_diffscan(lua_State *L)
static int internal_runCommand(lua_State *L) static int internal_runCommand(lua_State *L)
{ {
buffered_color_ostream out; color_ostream *out = NULL;
std::unique_ptr<buffered_color_ostream> out_buffer;
command_result res; command_result res;
if (lua_gettop(L) == 0) if (lua_gettop(L) == 0)
{ {
lua_pushstring(L, ""); lua_pushstring(L, "");
} }
int type_1 = lua_type(L, 1); int type_1 = lua_type(L, 1);
bool use_console = lua_toboolean(L, 2);
if (use_console)
{
out = &Core::getInstance().getConsole();
}
else
{
out_buffer.reset(new buffered_color_ostream());
out = out_buffer.get();
}
if (type_1 == LUA_TTABLE) if (type_1 == LUA_TTABLE)
{ {
std::string command = ""; std::string command = "";
@ -2843,13 +2855,13 @@ static int internal_runCommand(lua_State *L)
lua_pop(L, 1); // remove value, leave key lua_pop(L, 1); // remove value, leave key
} }
CoreSuspender suspend; CoreSuspender suspend;
res = Core::getInstance().runCommand(out, command, args); res = Core::getInstance().runCommand(*out, command, args);
} }
else if (type_1 == LUA_TSTRING) else if (type_1 == LUA_TSTRING)
{ {
std::string command = lua_tostring(L, 1); std::string command = lua_tostring(L, 1);
CoreSuspender suspend; CoreSuspender suspend;
res = Core::getInstance().runCommand(out, command); res = Core::getInstance().runCommand(*out, command);
} }
else else
{ {
@ -2857,22 +2869,28 @@ static int internal_runCommand(lua_State *L)
lua_pushfstring(L, "Expected table, got %s", lua_typename(L, type_1)); lua_pushfstring(L, "Expected table, got %s", lua_typename(L, type_1));
return 2; return 2;
} }
auto fragments = out.fragments();
lua_newtable(L); lua_newtable(L);
lua_pushinteger(L, (int)res); lua_pushinteger(L, (int)res);
lua_setfield(L, -2, "status"); lua_setfield(L, -2, "status");
int i = 1;
for (auto iter = fragments.begin(); iter != fragments.end(); iter++, i++) if (out_buffer)
{ {
int color = iter->first; auto fragments = out_buffer->fragments();
std::string output = iter->second; int i = 1;
lua_createtable(L, 2, 0); for (auto iter = fragments.begin(); iter != fragments.end(); iter++, i++)
lua_pushinteger(L, color); {
lua_rawseti(L, -2, 1); int color = iter->first;
lua_pushstring(L, output.c_str()); std::string output = iter->second;
lua_rawseti(L, -2, 2); lua_createtable(L, 2, 0);
lua_rawseti(L, -2, i); 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); lua_pushvalue(L, -1);
return 1; return 1;
} }

@ -735,8 +735,7 @@ function dfhack.script_help(script_name, extension)
return help return help
end end
local function _run_command(...) local function _run_command(args, use_console)
args = {...}
if type(args[1]) == 'table' then if type(args[1]) == 'table' then
command = args[1] command = args[1]
elseif #args > 1 and type(args[2]) == 'table' then elseif #args > 1 and type(args[2]) == 'table' then
@ -750,11 +749,11 @@ local function _run_command(...)
else else
error('Invalid arguments') error('Invalid arguments')
end end
return internal.runCommand(command) return internal.runCommand(command, use_console)
end end
function dfhack.run_command_silent(...) function dfhack.run_command_silent(...)
local result = _run_command(...) local result = _run_command({...})
local output = "" local output = ""
for i, f in pairs(result) do for i, f in pairs(result) do
if type(f) == 'table' then if type(f) == 'table' then
@ -765,14 +764,7 @@ function dfhack.run_command_silent(...)
end end
function dfhack.run_command(...) function dfhack.run_command(...)
local result = _run_command(...) local result = _run_command({...}, true)
for i, f in pairs(result) do
if type(f) == 'table' then
dfhack.color(f[1])
dfhack.print(f[2])
end
end
dfhack.color(COLOR_RESET)
return result.status return result.status
end end