Skip to content

Commit 8f22484

Browse files
committed
Enhance: add sysBufferShrinkEvent for when a console's buffer is trimmed
This ought to help if any line number indexes are being used to access the buffer underlying a console as then they can be revised downwards to match the number of lines removed from the buffer/console concerned. This commit also improves the UI for the setConsoleBufferSize(...) Lua API function to bring it up to our current design style. It will now return true on sucess or nil + an error message otherwise. Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
1 parent 5ce765c commit 8f22484

5 files changed

Lines changed: 48 additions & 44 deletions

File tree

src/TBuffer.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "mudlet.h"
2626
#include "TConsole.h"
27+
#include "TEvent.h"
2728

2829
#include "pre_guard.h"
2930
#include <QTextBoundaryFinder>
@@ -701,7 +702,7 @@ TChar::TChar(const TChar& copy)
701702
const QString timeStampFormat = QStringLiteral("hh:mm:ss.zzz ");
702703
const QString blankTimeStamp = QStringLiteral("------------ ");
703704

704-
TBuffer::TBuffer(Host* pH)
705+
TBuffer::TBuffer(Host* pH, TConsole* pConsole)
705706
: mLinkID(0)
706707
, mLinesLimit(10000)
707708
, mBatchDeleteSize(1000)
@@ -720,6 +721,7 @@ TBuffer::TBuffer(Host* pH)
720721
, mOpenMainQuote()
721722
, mMXP_SEND_NO_REF_MODE(false)
722723
, mEchoingText(false)
724+
, mpConsole(pConsole)
723725
, mGotESC(false)
724726
, mGotCSI(false)
725727
, mGotOSC(false)
@@ -780,14 +782,8 @@ TBuffer::TBuffer(Host* pH)
780782

781783
void TBuffer::setBufferSize(int s, int batch)
782784
{
783-
if (s < 100) {
784-
s = 100;
785-
}
786-
if (batch >= s) {
787-
batch = s / 10;
788-
}
789-
mLinesLimit = s;
790-
mBatchDeleteSize = batch;
785+
mLinesLimit = qMin(100, s);
786+
mBatchDeleteSize = (batch >= mLinesLimit) ? (mLinesLimit / 10) : batch;
791787
}
792788

793789
void TBuffer::updateColors()
@@ -3854,6 +3850,18 @@ void TBuffer::shrinkBuffer()
38543850
buffer.pop_front();
38553851
mCursorY--;
38563852
}
3853+
3854+
if (mpConsole->getType() & (TConsole::MainConsole|TConsole::UserWindow|TConsole::SubConsole)) {
3855+
// Signal to lua subsystem that indexes into the Console will need adjusting
3856+
TEvent bufferShrinkEvent{};
3857+
bufferShrinkEvent.mArgumentList.append(QLatin1String("sysBufferShrinkEvent"));
3858+
bufferShrinkEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
3859+
bufferShrinkEvent.mArgumentList.append(mpConsole->mConsoleName);
3860+
bufferShrinkEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
3861+
bufferShrinkEvent.mArgumentList.append(QString::number(mBatchDeleteSize));
3862+
bufferShrinkEvent.mArgumentTypeList.append(ARGUMENT_TYPE_NUMBER);
3863+
mpHost->raiseEvent(bufferShrinkEvent);
3864+
}
38573865
}
38583866

38593867
bool TBuffer::deleteLines(int from, int to)

src/TBuffer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <string>
4444

4545
class Host;
46+
class TConsole;
4647

4748
class QTextCodec;
4849

@@ -143,7 +144,7 @@ class TBuffer
143144

144145

145146
public:
146-
TBuffer(Host* pH);
147+
TBuffer(Host* pH, TConsole* pConsole = nullptr);
147148
QPoint insert(QPoint&, const QString& text, int, int, int, int, int, int, bool bold, bool italics, bool underline, bool strikeout);
148149
bool insertInLine(QPoint& cursor, const QString& what, TChar& format);
149150
void expandLine(int y, int count, TChar&);
@@ -276,6 +277,8 @@ class TBuffer
276277

277278
static const int scmMaxLinks = 2000;
278279

280+
const TConsole* mpConsole;
281+
279282
// First stage in decoding SGR/OCS sequences - set true when we see the
280283
// ASCII ESC character:
281284
bool mGotESC;

src/TConsole.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ TConsole::TConsole(Host* pH, ConsoleType type, QWidget* parent)
5555
, mpHost(pH)
5656
, mpDockWidget(nullptr)
5757
, mpCommandLine(nullptr)
58-
, buffer(pH)
58+
, buffer(pH, this)
5959
, emergencyStop(new QToolButton)
6060
, layerCommandLine(nullptr)
6161
, mBgColor(QColor(Qt::black))

src/TLuaInterpreter.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,47 +2114,40 @@ int TLuaInterpreter::moveCursor(lua_State* L)
21142114
// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#setConsoleBufferSize
21152115
int TLuaInterpreter::setConsoleBufferSize(lua_State* L)
21162116
{
2117-
int s = 1;
2117+
int s = 0;
21182118
int n = lua_gettop(L);
2119-
std::string a1;
2119+
QString windowName = QStringLiteral("main");
21202120
if (n > 2) {
2121-
if (!lua_isstring(L, s)) {
2122-
lua_pushstring(L, "setConsoleBufferSize: wrong argument type");
2123-
lua_error(L);
2124-
return 1;
2125-
} else {
2126-
a1 = lua_tostring(L, s);
2127-
s++;
2121+
if (!lua_isstring(L, ++s)) {
2122+
lua_pushfstring(L, "setConsoleBufferSize: bad argument #%d type (console name as string is optional, got %s!)", s, luaL_typename(L, s));
2123+
return lua_error(L);
21282124
}
2125+
windowName = QString::fromUtf8(lua_tostring(L, s));
21292126
}
2130-
int luaFrom;
2131-
if (!lua_isnumber(L, s)) {
2132-
lua_pushstring(L, "setConsoleBufferSize: wrong argument type");
2133-
lua_error(L);
2134-
return 1;
2135-
} else {
2136-
luaFrom = lua_tointeger(L, s);
2137-
s++;
2127+
2128+
if (!lua_isnumber(L, ++s)) {
2129+
lua_pushfstring(L, "setConsoleBufferSize: bad argument #%d type (maximum line count as number expected, got %s!)", s, luaL_typename(L, s));
2130+
return lua_error(L);
21382131
}
2132+
int limit = lua_tointeger(L, s);
21392133

2140-
int luaTo;
2141-
if (!lua_isnumber(L, s)) {
2142-
lua_pushstring(L, "setConsoleBufferSize: wrong argument type");
2143-
lua_error(L);
2144-
return 1;
2145-
} else {
2146-
luaTo = lua_tointeger(L, s);
2134+
if (!lua_isnumber(L, ++s)) {
2135+
lua_pushfstring(L, "setConsoleBufferSize: bad argument #%d type (delete line count as number expected, got %s!)", s, luaL_typename(L, s));
2136+
return lua_error(L);
21472137
}
2138+
int chunkSize = lua_tointeger(L, s);
21482139

21492140
Host& host = getHostFromLua(L);
2150-
2151-
if (a1 == "main" || n < 3) {
2152-
host.mpConsole->buffer.setBufferSize(luaFrom, luaTo);
2153-
} else {
2154-
QString windowName = a1.c_str();
2155-
mudlet::self()->setConsoleBufferSize(&host, windowName, luaFrom, luaTo);
2141+
if (!windowName.compare(QLatin1String("main"))) {
2142+
host.mpConsole->buffer.setBufferSize(limit, chunkSize);
2143+
} else if (!mudlet::self()->setConsoleBufferSize(&host, windowName, limit, chunkSize)) {
2144+
lua_pushnil(L);
2145+
lua_pushfstring(L, "window \"%s\" not found", windowName.toUtf8().constData());
2146+
return 2;
21562147
}
2157-
return 0;
2148+
2149+
lua_pushboolean(L, true);
2150+
return 1;
21582151
}
21592152

21602153
// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#enableScrollBar

src/mudlet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,9 +2291,9 @@ bool mudlet::setConsoleBufferSize(Host* pHost, const QString& name, int x1, int
22912291
if (pC) {
22922292
pC->buffer.setBufferSize(x1, y1);
22932293
return true;
2294-
} else {
2295-
return false;
22962294
}
2295+
2296+
return false;
22972297
}
22982298

22992299
bool mudlet::setScrollBarVisible(Host* pHost, const QString& name, bool isVisible)

0 commit comments

Comments
 (0)