Last active
October 6, 2020 15:46
-
-
Save smurfix/663b1004721d86aaf676bbe4c71ee303 to your computer and use it in GitHub Desktop.
Sketch: console lookup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/Host.cpp b/src/Host.cpp | |
index 73df2d40..10103f95 100644 | |
--- a/src/Host.cpp | |
+++ b/src/Host.cpp | |
@@ -2598,3 +2598,13 @@ void Host::setCompactInputLine(const bool state) | |
} | |
} | |
} | |
+ | |
+QPointer<TConsole> Host::findConsole(QString name) | |
+{ | |
+ if (name.isEmpty() or name == "main") { | |
+ return mpConsole; | |
+ } else { | |
+ return mpConsole->mSubConsoleMap.value(name); | |
+ } | |
+} | |
+ | |
diff --git a/src/Host.h b/src/Host.h | |
index cf230a9a..d9571780 100644 | |
--- a/src/Host.h | |
+++ b/src/Host.h | |
@@ -329,7 +329,7 @@ public: | |
bool debugShowAllProblemCodepoints() const { return mDebugShowAllProblemCodepoints; } | |
void setCompactInputLine(const bool state); | |
bool getCompactInputLine() const { return mCompactInputLine; } | |
- | |
+ QPointer<TConsole> findConsole(QString name); | |
cTelnet mTelnet; | |
QPointer<TConsole> mpConsole; | |
diff --git a/src/TLuaInterpreter.cpp b/src/TLuaInterpreter.cpp | |
index 7e870bb7..4d0eaf74 100644 | |
--- a/src/TLuaInterpreter.cpp | |
+++ b/src/TLuaInterpreter.cpp | |
@@ -104,6 +104,30 @@ static bool isMain(const QString& name) | |
return false; | |
} | |
+static const char *bad_window_type = "%s: bad argument #%d type (window name as string expected, got %s!"; | |
+static const char *bad_window_value = "window \"%s\" not found"; | |
+ | |
+#define WINDOW_NAME(_L, _pos) \ | |
+ ({ \ | |
+ if (!lua_isstring(_L, _pos)) { \ | |
+ lua_pushfstring(_L, bad_window_type, __FUNCTION__, _pos, luaL_typename(_L, _pos)); \ | |
+ return lua_error(_L); \ | |
+ } \ | |
+ lua_tostring(_L, _pos); \ | |
+ }) | |
+ | |
+#define CONSOLE(_L, _name) \ | |
+ ({ \ | |
+ auto console = getHostFromLua(_L).findConsole(_name); \ | |
+ if (!console) { \ | |
+ lua_pushnil(L); \ | |
+ lua_pushfstring(L, bad_window_value, _name.toUtf8().constData()); \ | |
+ return 2; \ | |
+ } \ | |
+ console; \ | |
+ }) | |
+ | |
+ | |
TLuaInterpreter::TLuaInterpreter(Host* pH, const QString& hostName, int id) : mpHost(pH), hostName(hostName), mHostID(id), purgeTimer(this) | |
{ | |
pGlobalLua = nullptr; | |
@@ -661,12 +685,7 @@ int TLuaInterpreter::selectString(lua_State* L) | |
int s = 1; | |
QString windowName; | |
if (lua_gettop(L) > 2) { | |
- if (!lua_isstring(L, s)) { | |
- lua_pushfstring(L, R"(selectString: bad argument #%d type (window name as string, is optional {defaults to "main" if omitted}, got %s!))", s, luaL_typename(L, s)); | |
- return lua_error(L); | |
- } | |
- // We cannot yet properly handle non-ASCII windows names but we will eventually! | |
- windowName = lua_tostring(L, s); | |
+ windowName = WINDOW_NAME(L, 1); | |
s++; | |
} | |
@@ -684,32 +703,21 @@ int TLuaInterpreter::selectString(lua_State* L) | |
} | |
qint64 numOfMatch = lua_tointeger(L, s); | |
- if (isMain(windowName)) { | |
- lua_pushnumber(L, host.mpConsole->select(searchText, numOfMatch)); | |
- } else { | |
- lua_pushnumber(L, mudlet::self()->selectString(&host, windowName, searchText, numOfMatch)); | |
- } | |
+ auto console = CONSOLE(L, windowName); | |
+ lua_pushnumber(L, console->select(searchText, numOfMatch)); | |
return 1; | |
} | |
// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#selectCurrentLine | |
int TLuaInterpreter::selectCurrentLine(lua_State* L) | |
{ | |
- std::string windowName; | |
+ QString windowName; | |
if (lua_gettop(L) > 0) { | |
- if (!lua_isstring(L, 1)) { | |
- lua_pushfstring(L, "selectCurrentLine: bad argument #1 type (window name as string expected, got %s!)", luaL_typename(L, 1)); | |
- return lua_error(L); | |
- } | |
- windowName = lua_tostring(L, 1); | |
+ windowName = WINDOW_NAME(L, 1); | |
} | |
- Host& host = getHostFromLua(L); | |
- if (isMain(QString::fromStdString(windowName))) { | |
- host.mpConsole->selectCurrentLine(); | |
- } else { | |
- host.mpConsole->selectCurrentLine(windowName); | |
- } | |
+ auto console = CONSOLE(L, windowName); | |
+ console->selectCurrentLine(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment