-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReplWidget.cpp
179 lines (152 loc) · 3.86 KB
/
ReplWidget.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
#include "ReplWidget.h"
#include <iostream>
ReplWidget::ReplWidget(QWidget *parent) : QPlainTextEdit(parent),
userPrompt(QString("> ")),
locked(false),
historySkip(false)
{
historyUp.clear();
historyDown.clear();
setLineWrapMode(NoWrap);
insertPlainText(userPrompt);
}
/** Filter all key events. The keys are filtered and handled manually
* in order to create a typical shell-like behaviour. For example
* Up and Down arrows don't move the cursor, but allow the user to
* browse the last commands that there launched.
*/
void ReplWidget::keyPressEvent(QKeyEvent *e) {
// locked State: a command has been submitted but no result
// has been received yet.
if(locked) return;
switch(e->key()) {
case Qt::Key_Return:
handleEnter();
break;
case Qt::Key_Backspace:
handleLeft(e);
break;
case Qt::Key_Up:
handleHistoryUp();
break;
case Qt::Key_Down:
handleHistoryDown();
break;
case Qt::Key_Left:
handleLeft(e);
break;
case Qt::Key_Home:
handleHome();
break;
default:
QPlainTextEdit::keyPressEvent(e);
break;
}
}
// Enter key pressed
void ReplWidget::handleEnter() {
QString cmd = getCommand();
if(0 < cmd.length()) {
while(historyDown.count() > 0) {
historyUp.push(historyDown.pop());
}
historyUp.push(cmd);
}
moveToEndOfLine();
if(cmd.length() > 0) {
locked = true;
setFocus();
insertPlainText("\n");
emit command(cmd);
} else {
insertPlainText("\n");
insertPlainText(userPrompt);
ensureCursorVisible();
}
}
// Result received
void ReplWidget::result(QString result) {
insertPlainText(result);
insertPlainText("\n");
insertPlainText(userPrompt);
ensureCursorVisible();
locked = false;
}
// Append line but do not display prompt afterwards
void ReplWidget::append(QString text) {
insertPlainText(text);
insertPlainText("\n");
ensureCursorVisible();
}
// Arrow up pressed
void ReplWidget::handleHistoryUp() {
if(0 < historyUp.count()) {
QString cmd = historyUp.pop();
historyDown.push(cmd);
clearLine();
insertPlainText(cmd);
}
historySkip = true;
}
// Arrow down pressed
void ReplWidget::handleHistoryDown() {
if(0 < historyDown.count() && historySkip) {
historyUp.push(historyDown.pop());
historySkip = false;
}
if(0 < historyDown.count()) {
QString cmd = historyDown.pop();
historyUp.push(cmd);
clearLine();
insertPlainText(cmd);
} else {
clearLine();
}
}
void ReplWidget::clearLine() {
QTextCursor c = this->textCursor();
c.select(QTextCursor::LineUnderCursor);
c.removeSelectedText();
this->insertPlainText(userPrompt);
}
// Select and return the user-input (exclude the prompt)
QString ReplWidget::getCommand() const {
QTextCursor c = this->textCursor();
c.select(QTextCursor::LineUnderCursor);
QString text = c.selectedText();
text.remove(0, userPrompt.length());
return text;
}
void ReplWidget::moveToEndOfLine() {
QPlainTextEdit::moveCursor(QTextCursor::EndOfLine);
}
// The text cursor is not allowed to move beyond the
// prompt
void ReplWidget::handleLeft(QKeyEvent *event) {
if(getIndex(textCursor()) > userPrompt.length()) {
QPlainTextEdit::keyPressEvent(event);
}
}
// Home (pos1) key pressed
void ReplWidget::handleHome() {
QTextCursor c = textCursor();
c.movePosition(QTextCursor::StartOfLine);
c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, userPrompt.length());
setTextCursor(c);
}
// Solution for getting x and y position of the cursor. Found
// them in the Qt mailing list
int ReplWidget::getIndex (const QTextCursor &crQTextCursor ) {
QTextBlock b;
int column = 1;
b = crQTextCursor.block();
column = crQTextCursor.position() - b.position();
return column;
}
void ReplWidget::setPrompt(const QString &prompt) {
userPrompt = prompt;
clearLine();
}
QString ReplWidget::prompt() const {
return userPrompt;
}