-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathRedisConsole.qml
356 lines (291 loc) · 10.5 KB
/
RedisConsole.qml
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
import QtQuick 2.3
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import "../common"
import "../common/platformutils.js" as PlatformUtils
import "."
import rdm.models 1.0
Rectangle {
id: root
color: "#3A3A3A"
property bool cursorInEditArea: false
property string prompt
property int promptPosition
property int promptLength: prompt.length
property alias busy: textArea.readOnly
property string initText:
"<span style='color: white; font-size: 13pt;'>RESP.app Redis Console</span><br/>" +
qsTranslate("RESP","Connecting...")
function setPrompt(txt, display) {
console.log("set prompt: ", txt, display)
prompt = txt
if (display)
displayPrompt();
}
function displayPrompt() {
textArea.insert(textArea.length, prompt)
promptPosition = textArea.length - promptLength
//textArea.cursorPosition = textArea.length - 1
}
function clear() {
textArea.clear()
}
function addOutput(text, type) {
if (type == "error") {
textArea.append("<span style='color: red; font-family: "
+ appSettings.valueEditorFont + "'>"
+ qmlUtils.escapeHtmlEntities(text) + '</span>')
} else {
textArea.append("<code style='color: white;white-space: pre-wrap;font-family: "
+ appSettings.valueEditorFont + "'>"
+ qmlUtils.escapeHtmlEntities(text) + '</code>')
}
if (type == "complete" || type == "error") {
textArea.blockAllInput = false
textArea.append("<br/>")
displayPrompt()
}
}
signal execCommand(string command)
BaseConsole {
id: textArea
anchors.fill: parent
backgroundVisible: false
textColor: "yellow"
readOnly: root.promptLength == 0 || blockAllInput
textFormat: TextEdit.RichText
menu: null
property bool blockAllInput: false
property int commandStartPos: root.promptPosition + root.promptLength
function getCurrentCommand() {
return getText(commandStartPos, length)
}
Keys.onPressed: {
if (readOnly) {
console.log("Console is read-only. Ignore Key presses.")
return
}
var cursorInReadOnlyArea = cursorPosition < commandStartPos
if (event.key == Qt.Key_Backspace && cursorPosition <= commandStartPos) {
event.accepted = true
console.log("Block backspace")
return
}
if (event.key == Qt.Key_Left && cursorPosition <= commandStartPos) {
event.accepted = true
console.log("Block left arrow")
return
}
if (((event.modifiers == Qt.NoModifier) || (event.modifiers & Qt.ShiftModifier))
&& cursorInReadOnlyArea) {
cursorPosition = length
event.accepted = true
console.log("Block Input in Read-Only area")
return
}
if (event.matches(StandardKey.Undo)
&& cursorPosition == commandStartPos) {
event.accepted = true
console.log("Block Undo")
return
}
if (selectionStart < commandStartPos
&& (event.matches(StandardKey.Cut)
|| event.key == Qt.Key_Delete
|| event.key == Qt.Key_Backspace)) {
event.accepted = true
console.log("Block Cut/Delete")
return
}
if (event.matches(StandardKey.Paste)) {
event.accepted = true
console.log("Block Reach Text Input")
hiddenBuffer.text = ""
hiddenBuffer.paste()
if (cursorInReadOnlyArea)
cursorPosition = length
insert(cursorPosition, hiddenBuffer.text.trim())
return
}
if (event.key == Qt.Key_Up || event.key == Qt.Key_Down) {
var command;
if (commandsHistoryModel.historyNavigation) {
if (event.key == Qt.Key_Down) {
command = commandsHistoryModel.getNextCommand()
} else {
command = commandsHistoryModel.getPrevCommand()
}
} else {
command = commandsHistoryModel.getCurrentCommand()
}
remove(commandStartPos, length)
insert(commandStartPos, command)
event.accepted = true
return
}
if (event.key == Qt.Key_Return && cursorPosition > commandStartPos) {
var command = getText(commandStartPos, length)
blockAllInput = true
event.accepted = true
if (command.toLowerCase() === "clear") {
root.clear()
root.displayPrompt()
blockAllInput = false
} else {
root.execCommand(command)
}
commandsHistoryModel.appendCommand(command)
}
autocompleteModel.filterString = "^" + getText(commandStartPos, cursorPosition) + event.text
}
Component.onCompleted: {
textArea.text = root.initText
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
onClicked: {
menu.popup()
}
}
Menu {
id: menu
MenuItem {
text: qsTranslate("RESP","Clear")
iconSource: PlatformUtils.getThemeIcon("cleanup.svg")
onTriggered: {
root.clear()
root.displayPrompt()
}
}
}
}
ColumnLayout {
objectName: "rdm_autocomplete_results"
height: 150
width: root.width - x - 50
x: textArea.cursorRectangle? textArea.cursorRectangle.x : 0
y: textArea.cursorRectangle? textArea.cursorRectangle.y + 20 : 0
z: 255
visible: {
return cmdAutocomplete.rowCount > 0
&& autocompleteModel.filterString.length > 0
&& textArea.cursorPosition >= textArea.commandStartPos
}
TableView {
id: cmdAutocomplete
Layout.fillWidth: true
Layout.fillHeight: true
model: autocompleteModel
headerVisible: true
TableViewColumn {
title: "Command"
role: "name"
width: 120
}
TableViewColumn {
title: qsTranslate("RESP","Arguments")
role: "arguments"
width: 250
}
TableViewColumn {
title: qsTranslate("RESP","Description")
role: "summary"
width: 350
}
TableViewColumn {
title: qsTranslate("RESP","Available since")
role: "since"
width: 60
}
itemDelegate: Item {
Text {
anchors.fill: parent
color: styleData.textColor
elide: styleData.elideMode
text: styleData.value
wrapMode: Text.WrapAnywhere
maximumLineCount: 1
}
MouseArea {
enabled: styleData.column === 2 || styleData.column === 0
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
var commandName = "#"
try {
commandName = consoleAutocompleteModel.getRow(
autocompleteModel.getOriginalRowIndex(styleData.row)
)["name"]
} catch(err) {
console.log("Cannot get command name:", err)
}
if (styleData.column === 2) {
Qt.openUrlExternally("https://redis.io/commands/" + commandName)
} else {
textArea.remove(textArea.commandStartPos, textArea.cursorPosition)
textArea.insert(textArea.commandStartPos, commandName)
autocompleteModel.filterString = commandName
}
}
}
}
}
RowLayout {
Layout.minimumWidth: 150
Layout.minimumHeight: closeBtn.implicitHeight
Item {
Layout.fillWidth: true
}
Button {
id: closeBtn
text: qsTranslate("RESP","Close")
onClicked: {
autocompleteModel.filterString = ""
}
}
}
}
SortFilterProxyModel {
id: autocompleteModel
source: consoleAutocompleteModel
filterSyntax: SortFilterProxyModel.RegExp
filterCaseSensitivity: Qt.CaseInsensitive
filterRole: "name"
}
TextArea {
id: hiddenBuffer
visible: false
textFormat: TextEdit.PlainText
}
ListModel {
id: commandsHistoryModel
property int currentIndex: 0
property bool historyNavigation: false
function getCurrentCommand() {
checkCurrentPos()
var res = get(currentIndex)
historyNavigation = true
return res["cmd"]
}
function getNextCommand() {
currentIndex += 1
return getCurrentCommand()
}
function getPrevCommand() {
currentIndex -= 1
return getCurrentCommand()
}
function checkCurrentPos() {
if (currentIndex >= count)
currentIndex = commandsHistoryModel.count - 1
if (currentIndex < 0)
currentIndex = 0
}
function appendCommand(cmdStr) {
append({"cmd": cmdStr})
currentIndex = count - 1
historyNavigation = false
}
}
}