forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTBuffer.h
336 lines (305 loc) · 10.4 KB
/
TBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#ifndef MUDLET_TBUFFER_H
#define MUDLET_TBUFFER_H
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2015, 2017-2018 by Stephen Lyons *
* - slysven@virginmedia.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "pre_guard.h"
#include <QApplication>
#include <QChar>
#include <QColor>
#include <QMap>
#include <QPoint>
#include <QPointer>
#include <QString>
#include <QStringBuilder>
#include <QStringList>
#include <QTime>
#include <QVector>
#include "post_guard.h"
#include <deque>
#include <string>
#define TCHAR_ITALICS 1
#define TCHAR_BOLD 2
#define TCHAR_UNDERLINE 4
#define TCHAR_INVERSE 8
#define TCHAR_ECHO 16
#define TCHAR_STRIKEOUT 32
// TODO: {Reserve for} use the next four bits for extensions...
// Next one is for ANSI CSI SGR Overline (53 on, 55 off)
//#define TCHAR_OVERLINE 64
// We currently use inverse for selected text, but will need reworking so that
// code fragments currently using TCHAR_INVERSE use TCHAR_SELECT instead and
// and then used TCHAR_INVERSE in new methods as per BOLD/ITALICS - at point
// when used to draw text for display we will need to XOR the two flags together...!
//#define TCHAR_SELECT 128
// Next one is for ANSI CSI SGR Slow blink < 2.5 Hz (5 on, 25 off)
//#define TCHAR_SLOWBLINK 256
// Next one is for ANSI CSI SGR Rapid blink >= 2.5 Hz (6 on, 25 off)), make it
// mutually exclusive with previous (and have priority...)
//#define TCHAR_FASTBLINK 512
// Convience for testing for both Blink types
//#define TCHAR_BLINKMASK 768
class Host;
class QTextCodec;
class TChar
{
public:
TChar();
TChar(int, int, int, int, int, int, bool, bool, bool, bool, int _link = 0);
TChar(Host*);
TChar(const TChar& copy);
bool operator==(const TChar& c);
int fgR;
int fgG;
int fgB;
int bgR;
int bgG;
int bgB;
unsigned flags;
int link;
};
const QChar cLF = QChar('\n');
const QChar cSPACE = QChar(' ');
struct TMxpElement
{
QString name;
QString href;
QString hint;
};
class TBuffer
{
// need to use tr() on encoding names in csmEncodingTable
Q_DECLARE_TR_FUNCTIONS(TBuffer)
// private - a map of computer-friendly encoding names as keys,
// values are a pair of human-friendly name + encoding data
static const QMap<QString, QPair<QString, QVector<QChar>>> csmEncodingTable;
public:
TBuffer(Host* pH);
QPoint insert(QPoint&, const QString& text, int, int, int, int, int, int, bool bold, bool italics, bool underline, bool strikeout);
bool insertInLine(QPoint& cursor, const QString& what, TChar& format);
void expandLine(int y, int count, TChar&);
int wrapLine(int startLine, int screenWidth, int indentSize, TChar& format);
void log(int, int);
int skipSpacesAtBeginOfLine(int i, int i2);
void addLink(bool, const QString& text, QStringList& command, QStringList& hint, TChar format);
QString bufferToHtml(QPoint P1, QPoint P2, bool allowedTimestamps, int spacePadding = 0);
int size() { return static_cast<int>(buffer.size()); }
QString& line(int n);
int find(int line, const QString& what, int pos);
int wrap(int);
QStringList split(int line, const QString& splitter);
QStringList split(int line, QRegularExpression splitter);
bool replaceInLine(QPoint& start, QPoint& end, const QString& with, TChar& format);
bool deleteLine(int);
bool deleteLines(int from, int to);
bool applyFormat(QPoint&, QPoint&, TChar& format);
bool applyUnderline(QPoint& P_begin, QPoint& P_end, bool bold);
bool applyBold(QPoint& P_begin, QPoint& P_end, bool bold);
bool applyLink(QPoint& P_begin, QPoint& P_end, const QString& linkText, QStringList&, QStringList&);
bool applyItalics(QPoint& P_begin, QPoint& P_end, bool bold);
bool applyStrikeOut(QPoint& P_begin, QPoint& P_end, bool strikeout);
bool applyFgColor(QPoint&, QPoint&, int, int, int);
bool applyBgColor(QPoint&, QPoint&, int, int, int);
void appendBuffer(const TBuffer& chunk);
bool moveCursor(QPoint& where);
int getLastLineNumber();
QStringList getEndLines(int);
void clear();
QPoint getEndPos();
void translateToPlainText(std::string& s, bool isFromServer=false);
void append(const QString& chunk, int sub_start, int sub_end, int, int, int, int, int, int, bool bold, bool italics, bool underline, bool strikeout, int linkID = 0);
void appendLine(const QString& chunk, int sub_start, int sub_end, int, int, int, int, int, int, bool bold, bool italics, bool underline, bool strikeout, int linkID = 0);
void setWrapAt(int i) { mWrapAt = i; }
void setWrapIndent(int i) { mWrapIndent = i; }
void updateColors();
TBuffer copy(QPoint&, QPoint&);
TBuffer cut(QPoint&, QPoint&);
void paste(QPoint&, TBuffer);
void setBufferSize(int s, int batch);
static const QList<QString> getComputerEncodingNames() { return csmEncodingTable.keys(); }
static const QList<QString> getFriendlyEncodingNames();
static const QString& getComputerEncoding(const QString& encoding);
void logRemainingOutput();
// It would have been nice to do this with Qt's signals and slots but that
// is apparently incompatible with using a default constructor - sigh!
void encodingChanged(const QString &);
std::deque<TChar> bufferLine;
std::deque<std::deque<TChar>> buffer;
QStringList timeBuffer;
QStringList lineBuffer;
QList<bool> promptBuffer;
QList<bool> dirty;
QMap<int, QStringList> mLinkStore;
QMap<int, QStringList> mHintStore;
int mLinkID;
int mLinesLimit;
int mBatchDeleteSize;
int newLines;
int mUntriggered;
int mWrapAt;
int mWrapIndent;
int speedTP;
int speedSequencer;
int speedAppend;
int mCursorY;
bool mMXP;
bool mAssemblingToken;
std::string currentToken;
int openT;
int closeT;
QMap<QString, TMxpElement> mMXP_Elements;
TMxpElement mCurrentElement;
bool mMXP_LINK_MODE;
bool mIgnoreTag;
std::string mSkip;
bool mParsingVar;
char mOpenMainQuote;
bool mMXP_SEND_NO_REF_MODE;
std::string mAssembleRef;
bool mEchoText;
private:
void shrinkBuffer();
int calcWrapPos(int line, int begin, int end);
void handleNewLine();
bool processUtf8Sequence(const std::string&, bool, size_t, size_t&, bool&);
bool processGBSequence(const std::string&, bool, bool, size_t, size_t&, bool&);
bool gotESC;
bool gotHeader;
QString code;
int codeRet;
std::string tempLine;
bool mWaitingForHighColorCode;
bool mWaitingForMillionsColorCode;
bool mIsHighOrMillionsColorMode;
bool mIsHighOrMillionsColorModeForeground;
bool mIsHighOrMillionsColorModeBackground;
bool mIsDefaultColor;
bool isUserScrollBack;
int currentFgColorProperty;
QColor mBlack;
int mBlackR;
int mBlackG;
int mBlackB;
QColor mLightBlack;
int mLightBlackR;
int mLightBlackG;
int mLightBlackB;
QColor mRed;
int mRedR;
int mRedG;
int mRedB;
QColor mLightRed;
int mLightRedR;
int mLightRedG;
int mLightRedB;
QColor mLightGreen;
int mLightGreenR;
int mLightGreenG;
int mLightGreenB;
QColor mGreen;
int mGreenR;
int mGreenG;
int mGreenB;
QColor mLightBlue;
int mLightBlueR;
int mLightBlueG;
int mLightBlueB;
QColor mBlue;
int mBlueR;
int mBlueG;
int mBlueB;
QColor mLightYellow;
int mLightYellowR;
int mLightYellowG;
int mLightYellowB;
QColor mYellow;
int mYellowR;
int mYellowG;
int mYellowB;
QColor mLightCyan;
int mLightCyanR;
int mLightCyanG;
int mLightCyanB;
QColor mCyan;
int mCyanR;
int mCyanG;
int mCyanB;
QColor mLightMagenta;
int mLightMagentaR;
int mLightMagentaG;
int mLightMagentaB;
QColor mMagenta;
int mMagentaR;
int mMagentaG;
int mMagentaB;
QColor mLightWhite;
int mLightWhiteR;
int mLightWhiteG;
int mLightWhiteB;
QColor mWhite;
int mWhiteR;
int mWhiteG;
int mWhiteB;
QColor mFgColor;
int fgColorR;
int fgColorLightR;
int fgColorG;
int fgColorLightG;
int fgColorB;
int fgColorLightB;
int bgColorR;
int bgColorG;
int bgColorB;
QColor mBgColor;
QPointer<Host> mpHost;
int maxx;
int maxy;
bool hadLF;
int mLastLine;
bool mCursorMoved;
QTime mTime;
bool mBold;
bool mItalics;
bool mUnderline;
bool mStrikeOut;
bool mFgColorCode;
bool mBgColorCode;
int mFgColorR;
int mFgColorG;
int mFgColorB;
int mBgColorR;
int mBgColorG;
int mBgColorB;
QString mMudLine;
std::deque<TChar> mMudBuffer;
int mCode[1024]; //FIXME: potential overflow bug
// Used to hold the incomplete bytes (1-3) that could be left at the end of
// a packet:
std::string mIncompleteSequenceBytes;
// keeps track of the previously logged buffer lines to ensure no log duplication
// happens when you enter a command
int lastLoggedFromLine;
int lastloggedToLine;
QString lastTextToLog;
QString mEncoding;
QTextCodec* mMainIncomingCodec;
};
#endif // MUDLET_TBUFFER_H