-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTerminal2.cpp
432 lines (402 loc) · 11.3 KB
/
Terminal2.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the JuaJIT BC Viewer application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "Terminal2.h"
#include "ExpressionParser.h"
#include <QStatusBar>
#include <QKeyEvent>
#include <QApplication>
#include <QClipboard>
#ifdef QT_PRINTER_LIB
#include <QPrinter>
#endif
#include <QFileDialog>
#include <QFile>
#include <QShortcut>
#include <QtDebug>
#include <QMessageBox>
#include <QFontDialog>
#include <QSettings>
#include <GuiTools/AutoMenu.h>
#include <QProcess>
#include <QDate>
#include <lua.hpp>
using namespace Lua;
static QTextCharFormat s_pf;
static QTextCharFormat s_luaf;
static QTextCharFormat s_errf;
static QTextCharFormat s_outf;
static const char* s_prompt = "Lua> ";
Terminal2::Terminal2(QWidget* parent, Lua::Engine2* l):
QTextEdit( parent ), d_lua( l ), d_specialInterpreter(true)
{
if( d_lua == 0 )
d_lua = Lua::Engine2::getInst();
Q_ASSERT( d_lua != 0 );
QFont font;
font.setPointSize(9);
#ifdef Q_WS_X11
font.setFamily("Courier");
#else
font.setStyleHint( QFont::TypeWriter );
#endif
QSettings set;
updateFont(set.value( "Terminal/Font", QVariant::fromValue( font ) ).value<QFont>());
QTextCursor cur = textCursor();
cur.insertText( prompt(), s_pf );
d_out = QTextCursor( document() );
setFocusPolicy( Qt::StrongFocus );
setReadOnly( true );
setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
Gui::AutoMenu* pop = new Gui::AutoMenu( this, true );
pop->addCommand( "Copy", this, SLOT(handleCopy()) );
pop->addCommand( "Paste", this, SLOT(handlePaste()) );
pop->addSeparator();
pop->addCommand( "Select All", this, SLOT(handleSelectAll()) );
pop->addCommand( "Clear", this, SLOT(onClear()) );
pop->addSeparator();
pop->addCommand( "Export PDF...", this, SLOT(handleExportPdf()) );
pop->addCommand( "Save Log...", this, SLOT(handleSaveAs()) );
pop->addSeparator();
pop->addCommand( "Set Font...", this, SLOT(handleSetFont()) );
new QShortcut( tr("F12"), this, SLOT(handlePrintStack()) );
connect( d_lua, SIGNAL(onNotify(int,QByteArray,int)), this, SLOT(onNotify(int,QByteArray,int)) );
printText( QString("%1 %2").arg(LUA_RELEASE).arg(LUA_COPYRIGHT) );
printText( QString("%1 -- %2. %3").arg(LUAJIT_VERSION).arg(LUAJIT_COPYRIGHT).arg(LUAJIT_URL) );
printJitInfo();
printText( tr("%3 %1 (c) 2019-%4 %2").arg(qApp->applicationVersion()).
arg(qApp->organizationName()).arg(qApp->applicationName()).arg(QDate::currentDate().year()));
}
void Terminal2::printText(const QString & str, bool err )
{
d_out.insertText( str, err ? s_errf : s_outf );
d_out.insertText( QString( QChar::ParagraphSeparator ), s_pf );
moveCursor( QTextCursor::End );
}
Terminal2::~Terminal2()
{
}
void Terminal2::keyPressEvent(QKeyEvent *e)
{
switch( e->key() )
{
case Qt::Key_Return:
case Qt::Key_Enter:
{
moveCursor( QTextCursor::End );
QTextCursor cur = textCursor();
cur.insertText( QString( QChar::ParagraphSeparator ), s_pf );
d_out.setPosition( cur.position() );
const int old = cur.position();
cur.insertText( prompt(), s_pf );
d_out.setPosition( old );
if( !d_line.isEmpty() )
d_histo.append( d_line );
d_next.clear();
if( d_specialInterpreter && d_lua->isWaiting() )
{
ExpressionParser p;
if( p.parseAndPrint( d_line.toLatin1(), d_lua, false ) )
p.executeAndPrint( d_lua );
}else
d_lua->executeCmd( d_line.toLatin1(), "Terminal" );
d_line.clear();
}
return;
case Qt::Key_Backspace:
if( !d_line.isEmpty() )
{
moveCursor( QTextCursor::End );
QTextCursor cur = textCursor();
cur.deletePreviousChar();
d_line.truncate( d_line.length() - 1 );
}
return;
case Qt::Key_Up:
if( !d_histo.isEmpty() )
{
moveCursor( QTextCursor::End );
QTextCursor cur = textCursor();
cur.setPosition( cur.position() - d_line.size(), QTextCursor::KeepAnchor );
d_next.append( d_line );
d_line = d_histo.back();
d_histo.pop_back();
cur.insertText( d_line, s_luaf );
}
break;
case Qt::Key_Down:
if( !d_next.isEmpty() )
{
moveCursor( QTextCursor::End );
QTextCursor cur = textCursor();
cur.setPosition( cur.position() - d_line.size(), QTextCursor::KeepAnchor );
d_histo.append( d_line );
d_line = d_next.back();
d_next.pop_back();
cur.insertText( d_line, s_luaf );
}
break;
}
bool ctrl = ( e->modifiers() == Qt::ControlModifier );
if( ctrl )
{
switch( e->key() )
{
case Qt::Key_V:
paste();
return;
case Qt::Key_C:
copy();
return;
}
}else if( !e->text().isEmpty() )
{
moveCursor( QTextCursor::End );
QTextCursor cur = textCursor();
cur.insertText( e->text(), s_luaf );
d_line += e->text();
}else
e->ignore();
}
void Terminal2::inputMethodEvent(QInputMethodEvent * e)
{
// Auf Linux ist diese Methode nötig für Ü, Ö, etc.
// Das ist eine sehr vereinfachte Methode. Für eine vollständige Behandlung siehe
// QTextControlPrivate::inputMethodEvent in Qt/src/qui/text/qtextcontrol.cpp
QString text = e->commitString();
if (!text.isEmpty() &&
(text.at(0).isPrint() || text.at(0) == QLatin1Char('\t')))
{
moveCursor( QTextCursor::End );
QTextCursor cur = textCursor();
cur.insertText( text, s_luaf );
d_line += text;
e->accept();
}else
e->ignore();
}
QString Terminal2::prompt() const
{
if( d_specialInterpreter && d_lua->isWaiting() )
return "Exp>";
else
return s_prompt;
}
void Terminal2::updateFont(const QFont & f)
{
s_pf.setFontFamily( f.family() );
s_pf.setFontPointSize( f.pointSizeF() );
s_pf.setFontWeight( QFont::Bold );
s_luaf.setFontFamily( f.family() );
s_luaf.setFontPointSize( f.pointSizeF() );
s_luaf.setFontWeight( QFont::Normal );
s_errf.setFontFamily( f.family() );
s_errf.setFontPointSize( f.pointSizeF() );
s_errf.setFontWeight( QFont::Normal );
s_errf.setForeground( Qt::red );
s_outf.setFontFamily( f.family() );
s_outf.setFontPointSize( f.pointSizeF() );
s_outf.setFontWeight( QFont::Normal );
s_outf.setForeground( Qt::blue );
}
void Terminal2::printJitInfo()
{
int n;
const char *s;
lua_State* L = d_lua->getCtx();
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
lua_getfield(L, -1, "jit"); /* Get jit.* module table. */
lua_remove(L, -2);
lua_getfield(L, -1, "status");
lua_remove(L, -2);
n = lua_gettop(L);
lua_call(L, 0, LUA_MULTRET);
QString str;
QTextStream out(&str);
out << (lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF");
for (n++; (s = lua_tostring(L, n)); n++) {
out << ' ' << s;
}
printText( str );
}
void Terminal2::handleStdoutErr(const QByteArray& str, bool err)
{
QByteArray& out = err ? d_stderr : d_stdout;
for( int i = 0; i < str.size(); i++ )
{
if( str[i] < ' ' )
{
switch( str[i] )
{
case 0x08: // backspace
out.chop(1);
break;
case 0x09: // TAB
out.append(0x09);
break;
case 0x0a: // LF
case 0x04: // EOT
printText( QString::fromLatin1(out), err );
out.clear();
break;
}
}else
{
out.append(str[i]);
}
}
}
void Terminal2::onNotify(int messageType, QByteArray val1, int val2)
{
switch( messageType )
{
case Engine2::Print:
printText( val1 );
break;
case Engine2::Error:
d_out.insertText( val1, s_errf );
d_out.insertText( QString( QChar::ParagraphSeparator ), s_pf );
moveCursor( QTextCursor::End );
break;
case Engine2::Cout:
case Engine2::Cerr:
handleStdoutErr( val1, messageType == Engine2::Cerr );
break;
case Engine2::LineHit:
case Engine2::BreakHit:
case Engine2::Continued:
case Engine2::Aborted:
{
moveCursor( QTextCursor::End );
moveCursor( QTextCursor::StartOfLine, QTextCursor::KeepAnchor );
QTextCursor cur = textCursor();
cur.insertText( prompt(), s_pf );
cur.movePosition(QTextCursor::StartOfLine);
d_out.setPosition( cur.position() );
d_line.clear();
}
break;
case Engine2::Finished:
foreach(const QByteArray& res, d_lua->getReturns() )
printText(res);
break;
default:
break;
}
//msg.consume();
ensureCursorVisible();
QApplication::processEvents();
}
void Terminal2::clear()
{
setPlainText( "" );
QTextCursor cur = textCursor();
cur.insertText( prompt(), s_pf );
}
void Terminal2::paste()
{
QString s = QApplication::clipboard()->text();
moveCursor( QTextCursor::End );
QTextCursor cur = textCursor();
cur.insertText( s, s_luaf );
d_line += s;
}
void Terminal2::handlePaste()
{
QClipboard* cb = QApplication::clipboard();
ENABLED_IF( !cb->text().isNull() );
paste();
}
void Terminal2::handleCopy()
{
ENABLED_IF( textCursor().hasSelection() );
copy();
}
void Terminal2::handleSelectAll()
{
ENABLED_IF( true );
selectAll();
}
void Terminal2::onClear()
{
ENABLED_IF( true );
clear();
}
void Terminal2::handleExportPdf()
{
#ifdef QT_PRINTER_LIB
ENABLED_IF( true );
QPrinter p( QPrinter::HighResolution );
p.setOutputFormat( QPrinter::PdfFormat );
p.setCreator( tr("CARA") );
p.setPrintProgram( tr("CARA") );
p.setDocName( tr("CARA Lua Terminal2 Log") );
QString path = QFileDialog::getSaveFileName( this, tr("Export to PDF" ),
QString(), "*.pdf" );
if( path.isEmpty() )
return;
p.setOutputFileName( path );
print( &p );
#endif
}
void Terminal2::handleSaveAs()
{
ENABLED_IF( true );
QString path = QFileDialog::getSaveFileName( this, tr("Export to Text" ),
QString(), "*.txt" );
if( path.isEmpty() )
return;
QFile out(path);
if( !out.open( QIODevice::WriteOnly ) )
{
QMessageBox::critical( this, tr("Export to Text"), tr("Cannot open file for writing") );
return;
}
out.write( toPlainText().toLatin1() );
}
void Terminal2::handlePrintStack()
{
// ENABLED_IF( true );
QByteArray str;
QTextStream out( &str, QIODevice::WriteOnly );
const int top = lua_gettop( d_lua->getCtx() );
out << "*** Lua Internal Stack:" << endl;
if( top == 0 )
out << "empty" << endl;
else
{
for( int n = 1; n <= top; n++ )
{
out << "* Level " << n << ": (" << d_lua->getTypeName( n ) << ") " << d_lua->getValueString( n ) << endl;
}
}
qDebug() << str;
d_lua->print( str );
}
void Terminal2::handleSetFont()
{
ENABLED_IF( true );
bool ok;
QFont res = QFontDialog::getFont( &ok, s_luaf.font(), this );
if( !ok )
return;
QSettings set;
set.setValue( "Terminal/Font", QVariant::fromValue(res) );
set.sync();
updateFont( res );
}