add support for streaming command outputs in CustomButton

This commit is contained in:
shouya
2025-10-20 21:55:32 +09:00
parent 2a47662d09
commit ea2e0df837
4 changed files with 62 additions and 17 deletions
+41 -15
View File
@@ -36,6 +36,7 @@ Item {
readonly property string rightClickExec: widgetSettings.rightClickExec || widgetMetadata.rightClickExec
readonly property string middleClickExec: widgetSettings.middleClickExec || widgetMetadata.middleClickExec
readonly property string textCommand: widgetSettings.textCommand !== undefined ? widgetSettings.textCommand : (widgetMetadata.textCommand || "")
readonly property bool textStream: widgetSettings.textStream !== undefined ? widgetSettings.textStream : (widgetMetadata.textStream || false)
readonly property int textIntervalMs: widgetSettings.textIntervalMs !== undefined ? widgetSettings.textIntervalMs : (widgetMetadata.textIntervalMs || 3000)
readonly property bool hasExec: (leftClickExec || rightClickExec || middleClickExec)
@@ -84,29 +85,45 @@ Item {
id: refreshTimer
interval: Math.max(250, textIntervalMs)
repeat: true
running: (textCommand && textCommand.length > 0)
running: !textStream && textCommand && textCommand.length > 0
triggeredOnStart: true
onTriggered: {
if (!textCommand || textCommand.length === 0)
return
if (textProc.running)
return
textProc.command = ["sh", "-lc", textCommand]
textProc.running = true
onTriggered: root.runTextCommand()
}
// Restart exited text stream commands after a delay
Timer {
id: restartTimer
interval: 1000
running: textStream && !textProc.running
onTriggered: root.runTextCommand()
}
SplitParser {
id: textStdoutSplit
onRead: (line) => _dynamicText = String(line || "").trim()
}
StdioCollector {
id: textStdoutCollect
onStreamFinished: () => {
var out = String(this.text || "").trim()
if (out.indexOf("\n") !== -1) {
out = out.split("\n")[0]
}
_dynamicText = out
}
}
Process {
id: textProc
stdout: StdioCollector {}
stdout: textStream ? textStdoutSplit : textStdoutCollect
stderr: StdioCollector {}
onExited: (exitCode, exitStatus) => {
var out = String(stdout.text || "").trim()
if (out.indexOf("\n") !== -1) {
out = out.split("\n")[0]
}
_dynamicText = out
}
if (textStream) {
Logger.w("CustomButton", `Streaming text command exited (code: ${exitCode}), restarting...`)
return
}
}
}
function onClicked() {
@@ -134,4 +151,13 @@ Item {
Logger.i("CustomButton", `Executing command: ${middleClickExec}`)
}
}
function runTextCommand() {
if (!textCommand || textCommand.length === 0)
return
if (textProc.running)
return
textProc.command = ["sh", "-lc", textCommand]
textProc.running = true
}
}