add dfhack history repository and expose to lua

develop
myk002 2022-07-15 09:18:27 -07:00
parent dd6fbd53b6
commit c9a87511bd
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 54 additions and 1 deletions

@ -524,6 +524,20 @@ Input & Output
lock. Using an explicit ``dfhack.with_suspend`` will prevent lock. Using an explicit ``dfhack.with_suspend`` will prevent
this, forcing the function to block on input with lock held. this, forcing the function to block on input with lock held.
* ``dfhack.getCommandHistory(history_id, history_filename)``
Returns the list of strings in the specified history. Intended to be used by
GUI scripts that don't have access to a console and so can't use
``dfhack.lineedit``. The ``history_id`` parameter is some unique string that
the script uses to identify its command history, such as the script's name. If
this is the first time the history with the given ``history_id`` is being
accessed, it is initialized from the given file.
* ``dfhack.addCommandToHistory(history_id, history_filename, command)``
Adds a command to the specified history and saves the updated history to the
specified file.
* ``dfhack.interpreter([prompt[,history_filename[,env]]])`` * ``dfhack.interpreter([prompt[,history_filename[,env]]])``
Starts an interactive lua interpreter, using the specified prompt Starts an interactive lua interpreter, using the specified prompt
@ -837,6 +851,7 @@ can be omitted.
* ``dfhack.getGitXmlExpectedCommit()`` * ``dfhack.getGitXmlExpectedCommit()``
* ``dfhack.gitXmlMatch()`` * ``dfhack.gitXmlMatch()``
* ``dfhack.isRelease()`` * ``dfhack.isRelease()``
* ``dfhack.isPrerelease()``
Return information about the DFHack build in use. Return information about the DFHack build in use.
@ -890,7 +905,6 @@ can be omitted.
from ``dfhack.TranslateName()``), use ``print(dfhack.df2console(text))`` to from ``dfhack.TranslateName()``), use ``print(dfhack.df2console(text))`` to
ensure proper display on all platforms. ensure proper display on all platforms.
* ``dfhack.utf2df(string)`` * ``dfhack.utf2df(string)``
Convert a string from UTF-8 to DF's CP437 encoding. Convert a string from UTF-8 to DF's CP437 encoding.

@ -1369,6 +1369,38 @@ static void OpenRandom(lua_State *state)
lua_pop(state, 1); lua_pop(state, 1);
} }
/*********************************
* Commandline history repository *
**********************************/
static std::map<std::string, CommandHistory> commandHistories;
static CommandHistory * ensureCommandHistory(std::string id,
std::string src_file) {
if (!commandHistories.count(id)) {
commandHistories[id].load(src_file.c_str());
}
return &commandHistories[id];
}
static int getCommandHistory(lua_State *state)
{
std::string id = lua_tostring(state, 1);
std::string src_file = lua_tostring(state, 2);
std::vector<std::string> entries;
ensureCommandHistory(id, src_file)->getEntries(entries);
Lua::PushVector(state, entries);
return 1;
}
static void addCommandToHistory(std::string id, std::string src_file,
std::string command) {
CommandHistory *history = ensureCommandHistory(id, src_file);
history->add(command);
history->save(src_file.c_str());
}
/************************ /************************
* Wrappers for C++ API * * Wrappers for C++ API *
************************/ ************************/
@ -1460,6 +1492,12 @@ static const LuaWrapper::FunctionReg dfhack_module[] = {
WRAP_VERSION_FUNC(gitXmlMatch, git_xml_match), WRAP_VERSION_FUNC(gitXmlMatch, git_xml_match),
WRAP_VERSION_FUNC(isRelease, is_release), WRAP_VERSION_FUNC(isRelease, is_release),
WRAP_VERSION_FUNC(isPrerelease, is_prerelease), WRAP_VERSION_FUNC(isPrerelease, is_prerelease),
WRAP(addCommandToHistory),
{ NULL, NULL }
};
static const luaL_Reg dfhack_funcs[] = {
{ "getCommandHistory", getCommandHistory },
{ NULL, NULL } { NULL, NULL }
}; };
@ -3199,6 +3237,7 @@ void OpenDFHackApi(lua_State *state)
OpenRandom(state); OpenRandom(state);
LuaWrapper::SetFunctionWrappers(state, dfhack_module); LuaWrapper::SetFunctionWrappers(state, dfhack_module);
luaL_setfuncs(state, dfhack_funcs, 0);
OpenModule(state, "gui", dfhack_gui_module, dfhack_gui_funcs); OpenModule(state, "gui", dfhack_gui_module, dfhack_gui_funcs);
OpenModule(state, "job", dfhack_job_module, dfhack_job_funcs); OpenModule(state, "job", dfhack_job_module, dfhack_job_funcs);
OpenModule(state, "units", dfhack_units_module, dfhack_units_funcs); OpenModule(state, "units", dfhack_units_module, dfhack_units_funcs);