-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.qml
393 lines (352 loc) · 12.3 KB
/
main.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
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
import QtQuick
import QtQuick.Layouts
import org.kde.kirigami as Kirigami
import org.kde.plasma.plasmoid
import org.kde.plasma.core as PlasmaCore
import org.kde.plasma.components as PlasmaComponent
import org.kde.plasma.plasma5support as Plasma5Support
PlasmoidItem {
id: widget
// https://github.com/KDE/plasma-workspace/blob/master/dataengines/executable/executable.h
// https://github.com/KDE/plasma-workspace/blob/master/dataengines/executable/executable.cpp
// https://github.com/KDE/plasma-framework/blob/master/src/declarativeimports/core/datasource.h
// https://github.com/KDE/plasma-framework/blob/master/src/declarativeimports/core/datasource.cpp
// https://github.com/KDE/plasma-framework/blob/master/src/plasma/scripting/dataenginescript.cpp
Plasma5Support.DataSource {
id: executable
engine: "executable"
connectedSources: []
onNewData: (sourceName, data) => {
var exitCode = data["exit code"]
var exitStatus = data["exit status"]
var stdout = data["stdout"]
var stderr = data["stderr"]
exited(sourceName, exitCode, exitStatus, stdout, stderr)
disconnectSource(sourceName) // cmd finished
}
function exec(cmd) {
if (cmd) {
connectSource(cmd)
}
}
signal exited(string cmd, int exitCode, int exitStatus, string stdout, string stderr)
}
function performClick() {
executable.exec(plasmoid.configuration.clickCommand)
}
function performMouseWheelUp() {
executable.exec(plasmoid.configuration.mousewheelUpCommand)
}
function performMouseWheelDown() {
executable.exec(plasmoid.configuration.mousewheelDownCommand)
}
Item {
id: config
readonly property bool active: !!command
readonly property bool waitForCompletion: plasmoid.configuration.waitForCompletion
readonly property int interval: Math.max(0, plasmoid.configuration.interval)
readonly property string command: plasmoid.configuration.command || ''
readonly property string tooltipCommand: plasmoid.configuration.tooltipCommand || ''
readonly property bool clickEnabled: !!plasmoid.configuration.clickCommand
readonly property bool mousewheelEnabled: (plasmoid.configuration.mousewheelUpCommand || plasmoid.configuration.mousewheelDownCommand)
readonly property color textColor: plasmoid.configuration.textColor || Kirigami.Theme.textColor
readonly property color outlineColor: plasmoid.configuration.outlineColor || Kirigami.Theme.backgroundColor
readonly property bool showOutline: plasmoid.configuration.showOutline
onCommandChanged: widget.runCommand()
onTooltipCommandChanged: widget.runCommand()
onIntervalChanged: {
// interval=0 stops the timer even with Timer.repeat=true, so we may
// need to restart the timer. Might as well restart the interval too.
timer.restart()
}
onWaitForCompletionChanged: {
if (!waitForCompletion) {
// The timer needs to be restarted in case the timer was already
// triggered and the command is running. If we don't restart the
// timer, it'll stop forever.
timer.restart()
}
}
}
// https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
property var ansiColors: ({
30: '#000000', // Black
31: '#aa0000', // Red
32: '#00aa00', // Green
33: '#aa6500', // Yellow
34: '#0000aa', // Blue
35: '#aa00aa', // Magenta
36: '#00aaaa', // Cyan
37: '#aaaaaa', // White
90: '#656565', // Bright Black
91: '#ff6565', // Bright Red
92: '#65ff65', // Bright Green
93: '#ffff65', // Bright Yellow
94: '#6565ff', // Bright Blue
95: '#ff65ff', // Bright Magenta
96: '#65ffff', // Bright Cyan
97: '#ffffff', // Bright White
})
function resetState(state) {
var out = state.closeTags.join(' ')
state.bold = false
state.closeTags = []
return out
}
function parseAnsiCode(n, i, tokens, state) {
if (n == 0) { // Reset
return resetState(state)
} else if (n == 1) {
state.closeTags.push('</b>')
state.bold = true
return '<b>'
} else if (30 <= n && n <= 37 || 90 <= n && n <= 97) {
if (state.bold && 30 <= n && n <= 37) {
// Bold also intensifies the colors to "Bright".
// 30 => 90
n += 60
}
var hexColor = ansiColors[n]
state.closeTags.push('</font>')
return '<font color="' + hexColor + '">'
} else {
return ''
}
}
// https://stackoverflow.com/questions/4745317/converting-integers-to-hex-string-in-javascript
function formatHexInt(n) {
var num = Number(n)
if (isNaN(num)) {
return "00"
}
num = Math.max(0, Math.min(num, 255))
var str = num.toString(16)
return str.length == 1 ? '0' + str : str
}
function rgbToHex(r, g, b) {
return '#' + formatHexInt(r) + formatHexInt(g) + formatHexInt(b)
}
function parseColorMode(i, tokens) {
var colorMode = parseInt(tokens[++i], 10)
if (colorMode == 2) { // RGB
var r = parseInt(tokens[++i], 10)
var g = parseInt(tokens[++i], 10)
var b = parseInt(tokens[++i], 10)
return rgbToHex(r, g, b)
} else if (colorMode == 5) { // Preset of 256 colors
// Logic taken from Konsole
// https://invent.kde.org/utilities/konsole/-/blob/master/src/autotests/CharacterColorTest.cpp#L159
var n = parseInt(tokens[++i], 10)
if (0 <= n && n <= 7) { // Normal
var u = n + 30
return ansiColors[u]
} else if (8 <= n && n <= 15) { // Bright
var u = n - 8 + 90
return ansiColors[u]
} else if (16 <= n && n <= 231) { // 212
var u = n - 16
var r = Math.floor(((u / 36) % 6) != 0 ? (40 * ((u / 36) % 6) + 55) : 0)
var g = Math.floor(((u / 6) % 6) != 0 ? (40 * ((u / 6) % 6) + 55) : 0)
var b = Math.floor(((u / 1) % 6) != 0 ? (40 * ((u / 1) % 6) + 55) : 0)
return rgbToHex(r, g, b)
} else if (232 <= n && n <= 255) {
var gray = Math.floor((n - 232) * 10 + 8)
return rgbToHex(gray, gray, gray)
}
}
return null
}
function parseAnsiEscape(codes, state) {
var tokens = codes.split(';')
var out = ''
for (var i = 0; i < tokens.length; i++) {
tokens[i] = parseInt(tokens[i], 10)
}
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (token == 38) { // Set FG
var hexColor = parseColorMode(i, tokens)
if (hexColor) {
state.closeTags.push('</font>')
out += '<font color="' + hexColor + '">'
}
} else if (token == 48) { // Set BG
var hexColor = parseColorMode(i, tokens)
// Ignore
} else {
out += parseAnsiCode(token, i, tokens, state)
}
}
return out
}
property string outputText: ''
property string tooltipText: ''
function formatOutputText(stdout) {
var formattedText = stdout
// Newlines
if (plasmoid.configuration.replaceAllNewlines) {
formattedText = formattedText.replace(/\n/g, ' ').trim()
} else if (formattedText.length >= 1 && formattedText[formattedText.length-1] == '\n') {
formattedText = formattedText.substr(0, formattedText.length-1)
}
// Terminal Colors (Issue #7)
var state = {
html: false,
bold: false,
closeTags: [],
}
formattedText = formattedText.replace(/\033\[(\d+(;\d+)*)?m/g, function(match, p1, p2){
state.html = true
if (typeof p1 === 'string') {
return parseAnsiEscape(p1, state)
} else { // \033[m is Reset
return parseAnsiEscape('0', state)
}
})
formattedText += resetState(state)
// Format Newlines when in HTML mode
if (state.html) {
formattedText = formattedText.replace(/\n/g, '<br>')
}
return formattedText
}
Connections {
target: executable
function onExited(cmd, exitCode, exitStatus, stdout, stderr) {
if ((cmd == config.command) || (cmd == config.tooltipCommand)) {
var formattedText = formatOutputText(stdout)
// console.log('[commandoutput]', 'stdout', JSON.stringify(stdout))
// console.log('[commandoutput]', 'format', JSON.stringify(formattedText))
if (cmd == config.command) {
widget.outputText = formattedText
} else if (cmd == config.tooltipCommand) {
widget.tooltipText = formattedText
}
if (config.waitForCompletion) {
timer.restart()
}
}
}
}
function runCommand() {
// console.log('[commandoutput]', Date.now(), 'runCommand', config.command)
executable.exec(config.command)
executable.exec(config.tooltipCommand)
}
Timer {
id: timer
interval: config.interval
running: true
repeat: !config.waitForCompletion
onTriggered: widget.runCommand()
// onIntervalChanged: console.log('interval', interval)
// onRunningChanged: console.log('running', running)
// onRepeatChanged: console.log('repeat', repeat)
Component.onCompleted: {
// Run right away in case the interval is very long.
triggered()
}
}
Plasmoid.onActivated: widget.performClick()
Plasmoid.backgroundHints: plasmoid.configuration.showBackground ? PlasmaCore.Types.DefaultBackground : PlasmaCore.Types.NoBackground
fullRepresentation: Item {
id: panelItem
readonly property bool isHorizontal: plasmoid.formFactor == PlasmaCore.Types.Horizontal
readonly property bool isVertical: plasmoid.formFactor == PlasmaCore.Types.Vertical
readonly property bool isInPanel: isHorizontal || isVertical
readonly property bool isOnDesktop: !isInPanel
readonly property int itemWidth: {
if (isOnDesktop) {
return Math.ceil(output.contentWidth)
} else if (isHorizontal && plasmoid.configuration.useFixedWidth) {
return plasmoid.configuration.fixedWidth * Kirigami.Units.devicePixelRatio
} else { // isHorizontal || isVertical
return Math.ceil(output.implicitWidth)
}
}
Layout.minimumWidth: isHorizontal ? itemWidth : -1
Layout.fillWidth: isVertical
Layout.preferredWidth: itemWidth // Panel widget default
// width: itemWidth // Desktop widget default
// onItemWidthChanged: console.log('itemWidth', itemWidth, 'implicitWidth', output.implicitWidth, 'contentWidth', output.contentWidth)
readonly property int itemHeight: {
if (isOnDesktop) {
return Math.ceil(output.contentHeight)
} else if (isVertical && plasmoid.configuration.useFixedHeight) {
return plasmoid.configuration.fixedHeight * Kirigami.Units.devicePixelRatio
} else { // isHorizontal || isVertical
return Math.ceil(output.implicitHeight)
}
}
Layout.minimumHeight: isVertical ? itemHeight : -1
Layout.fillHeight: isHorizontal
Layout.preferredHeight: itemHeight // Panel widget default
// height: itemHeight // Desktop widget default
// onItemHeightChanged: console.log('itemHeight', itemHeight, 'implicitHeight', output.implicitHeight, 'contentHeight', output.contentHeight)
// Note MouseArea is below the Text so
// that we don't eat the link clicks.
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: config.clickEnabled
cursorShape: output.hoveredLink ? Qt.PointingHandCursor : Qt.ArrowCursor
onClicked: {
widget.performClick()
}
property int wheelDelta: 0
onWheel: (wheel) => {
var delta = wheel.angleDelta.y || wheel.angleDelta.x
wheelDelta += delta
// Magic number 120 for common "one click"
// See: http://qt-project.org/doc/qt-5/qml-qtquick-wheelevent.html#angleDelta-prop
while (wheelDelta >= 120) {
wheelDelta -= 120
widget.performMouseWheelUp()
}
while (wheelDelta <= -120) {
wheelDelta += 120
widget.performMouseWheelDown()
}
wheel.accepted = true
}
}
Text {
id: output
width: parent.width
height: parent.height
PlasmaCore.ToolTipArea {
anchors.fill: parent
mainText: widget.tooltipText
enabled: widget.tooltipText
}
text: widget.outputText
color: config.textColor
style: config.showOutline ? Text.Outline : Text.Normal
styleColor: config.outlineColor
linkColor: Kirigami.Theme.linkColor
onLinkActivated: Qt.openUrlExternally(link)
font.pointSize: plasmoid.configuration.fontSize
font.family: plasmoid.configuration.fontFamily || Kirigami.Theme.defaultFont.family
font.weight: plasmoid.configuration.bold ? Font.Bold : Font.Normal
font.italic: plasmoid.configuration.italic
font.underline: plasmoid.configuration.underline
fontSizeMode: Text.FixedSize
horizontalAlignment: plasmoid.configuration.textAlign
verticalAlignment: plasmoid.configuration.vertAlign
property bool isFixedWidth: {
if (plasmoid.formFactor == PlasmaCore.Types.Planar) { // Desktop Widget
return true
} else if (plasmoid.formFactor == PlasmaCore.Types.Horizontal) {
return plasmoid.configuration.useFixedWidth
} else if (plasmoid.formFactor == PlasmaCore.Types.Vertical) {
return true
} else {
return false
}
}
elide: Text.ElideRight
wrapMode: isFixedWidth ? Text.Wrap : Text.NoWrap
}
}
preferredRepresentation: fullRepresentation
}