Skip to content

Commit c946952

Browse files
authored
Add basic lua console history (#692)
1 parent 91471cd commit c946952

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

src/NotepadNext/docks/LuaConsoleDock.cpp

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,6 @@ LuaConsoleDock::LuaConsoleDock(LuaState *l, QWidget *parent) :
217217
input->braceHighlight(INVALID_POSITION, INVALID_POSITION);
218218
}
219219
});
220-
221-
connect(input, &ScintillaNext::linesAdded, [=](int linesAdded) {
222-
if (input->lineCount() > 0)
223-
qInfo("lines added %d", linesAdded);
224-
});
225220
}
226221

227222
LuaConsoleDock::~LuaConsoleDock()
@@ -262,6 +257,50 @@ void LuaConsoleDock::writeErrorToOutput(const char *s)
262257
output->documentEnd();
263258
}
264259

260+
void LuaConsoleDock::historyNext()
261+
{
262+
if (history.isEmpty()) return;
263+
264+
if (currentHistoryIndex < history.size() - 1) {
265+
currentHistoryIndex++;
266+
input->setText(history[currentHistoryIndex].toLatin1().constData());
267+
}
268+
269+
input->documentEnd();
270+
input->emptyUndoBuffer();
271+
input->scrollToEnd();
272+
}
273+
274+
void LuaConsoleDock::historyPrevious()
275+
{
276+
if (currentHistoryIndex == 0) return;
277+
278+
currentHistoryIndex--;
279+
280+
input->setText(history[currentHistoryIndex].toLatin1().constData());
281+
input->documentEnd();
282+
input->emptyUndoBuffer();
283+
input->scrollToEnd();
284+
}
285+
286+
void LuaConsoleDock::historyAdd(QString line)
287+
{
288+
if (!line.isEmpty()) {
289+
if (history.isEmpty() || history.last() != line) {
290+
history.append(line);
291+
}
292+
}
293+
294+
currentHistoryIndex = history.size();
295+
}
296+
297+
void LuaConsoleDock::historyEnd()
298+
{
299+
currentHistoryIndex = history.size();
300+
input->emptyUndoBuffer(); // Empty first in case it was a mistake
301+
input->setText("");
302+
}
303+
265304
void LuaConsoleDock::runCurrentCommand()
266305
{
267306
int prevLastLine = output->lineCount();
@@ -289,7 +328,7 @@ void LuaConsoleDock::runCurrentCommand()
289328
}
290329

291330
QString text((const char *)input->characterPointer());
292-
//historyAdd(GUI::StringFromUTF8(text).c_str());
331+
historyAdd(text);
293332

294333
input->clearAll();
295334
input->emptyUndoBuffer();
@@ -307,11 +346,32 @@ bool LuaConsoleDock::eventFilter(QObject *obj, QEvent *event)
307346
runCurrentCommand();
308347
return true;
309348
}
349+
350+
if (keyEvent->key() == Qt::Key_Up)
351+
{
352+
if (input->lineCount() == 1 || input->currentPos() == input->length()) {
353+
historyPrevious();
354+
return true;
355+
}
356+
}
357+
358+
if (keyEvent->key() == Qt::Key_Down) {
359+
if (input->lineCount() == 1 || input->currentPos() == 0) {
360+
historyNext();
361+
return true;
362+
}
363+
}
364+
365+
if (keyEvent->key() == Qt::Key_Escape) {
366+
historyEnd();
367+
return true;
368+
}
310369
}
311370
else {
312371
// standard event processing
313372
return QObject::eventFilter(obj, event);
314373
}
374+
315375
return false;
316376
}
317377

src/NotepadNext/docks/LuaConsoleDock.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class LuaConsoleDock : public QDockWidget
4141
void writeErrorToOutput(const char *s);
4242
LuaState *L = Q_NULLPTR;
4343

44+
void historyNext();
45+
void historyPrevious();
46+
void historyAdd(QString line);
47+
void historyEnd();
48+
4449
public slots:
4550
void runCurrentCommand(void);
4651

@@ -53,6 +58,8 @@ public slots:
5358
ScintillaNext *output;
5459
ScintillaNext *input;
5560

61+
QStringList history;
62+
int currentHistoryIndex = 0;
5663

5764
void setupStyle(ScintillaNext *editor);
5865
};

0 commit comments

Comments
 (0)