| 1194 | } |
| 1195 | |
| 1196 | bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state, DFHack::Lua::init_fn init) |
| 1197 | { |
| 1198 | if (!lua_checkstack(state, 20)) |
| 1199 | return false; |
| 1200 | |
| 1201 | lua_State *thread; |
| 1202 | int rv; |
| 1203 | std::string prompt; |
| 1204 | std::string histfile; |
| 1205 | |
| 1206 | DFHack::CommandHistory hist; |
| 1207 | std::string histname; |
| 1208 | |
| 1209 | { |
| 1210 | CoreSuspender suspend; |
| 1211 | |
| 1212 | int base = lua_gettop(state); |
| 1213 | |
| 1214 | if (!init(out, state)) |
| 1215 | { |
| 1216 | lua_settop(state, base); |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
| 1220 | // If not interactive, run without coroutine and bail out |
| 1221 | if (!out.is_console()) |
| 1222 | return SafeCall(out, state, lua_gettop(state)-base-1, 0); |
| 1223 | |
| 1224 | lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_QUERY_COROTABLE_TOKEN); |
| 1225 | lua_pushvalue(state, base+1); |
| 1226 | lua_remove(state, base+1); |
| 1227 | thread = Lua::NewCoroutine(state); |
| 1228 | lua_rawsetp(state, -2, thread); |
| 1229 | lua_pop(state, 1); |
| 1230 | |
| 1231 | rv = resume_query_loop(out, thread, state, lua_gettop(state)-base, prompt, histfile); |
| 1232 | } |
| 1233 | |
| 1234 | Console &con = static_cast<Console&>(out); |
| 1235 | |
| 1236 | while (rv == LUA_YIELD) |
| 1237 | { |
| 1238 | if (histfile != histname) |
| 1239 | { |
| 1240 | if (!histname.empty()) |
| 1241 | hist.save(std::filesystem::path{ histname }); |
| 1242 | |
| 1243 | hist.clear(); |
| 1244 | histname = histfile; |
| 1245 | |
| 1246 | if (!histname.empty()) |
| 1247 | hist.load(std::filesystem::path{ histname }); |
| 1248 | } |
| 1249 | |
| 1250 | if (prompt.empty()) |
| 1251 | prompt = ">> "; |
| 1252 | |
| 1253 | std::string curline; |
nothing calls this directly
no test coverage detected