-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCommandRunner.qml
More file actions
169 lines (144 loc) · 5.08 KB
/
CommandRunner.qml
File metadata and controls
169 lines (144 loc) · 5.08 KB
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
import QtQuick
import Quickshell
import Quickshell.Io
import qs.Services
QtObject {
id: root
property var pluginService: null
property string trigger: ">"
property var commandHistory: []
property int maxHistoryItems: 20
signal itemsChanged
Component.onCompleted: {
if (!pluginService)
return;
trigger = pluginService.loadPluginData("commandRunner", "trigger", ">");
commandHistory = pluginService.loadPluginData("commandRunner", "history", []);
maxHistoryItems = pluginService.loadPluginData("commandRunner", "maxHistoryItems", 20);
}
function getItems(query) {
const items = [];
if (query && query.trim().length > 0) {
const command = query.trim();
// Use _preScored to ensure DMS preserves our item ordering
items.push({
name: "Run: " + command,
icon: "material:terminal",
comment: "Execute command in terminal",
action: "run:" + command,
categories: ["Command Runner"],
_preScored: 1000
});
items.push({
name: "Run in background: " + command,
icon: "material:step_over",
comment: "Execute command silently in background",
action: "background:" + command,
categories: ["Command Runner"],
_preScored: 900
});
items.push({
name: "Copy: " + command,
icon: "material:content_copy",
comment: "Copy command to clipboard",
action: "copy:" + command,
categories: ["Command Runner"],
_preScored: 800
});
}
if (commandHistory.length > 0) {
const filteredHistory = query ? commandHistory.filter(cmd => cmd.toLowerCase().includes(query.toLowerCase())) : commandHistory;
for (let i = 0; i < Math.min(10, filteredHistory.length); i++) {
const cmd = filteredHistory[i];
items.push({
name: cmd,
icon: "material:history",
comment: "Run from history",
action: "run:" + cmd,
categories: ["Command Runner"],
_preScored: 100 - i
});
}
}
return items;
}
function executeItem(item) {
if (!item?.action)
return;
const actionParts = item.action.split(":");
const actionType = actionParts[0];
const command = actionParts.slice(1).join(":");
switch (actionType) {
case "noop":
return;
case "copy":
copyToClipboard(command);
break;
case "run":
runCommand(command);
break;
case "background":
runBackground(command);
break;
default:
showToast("Unknown action: " + actionType);
}
}
function copyToClipboard(text) {
Quickshell.execDetached(["sh", "-c", "echo -n '" + text + "' | wl-copy"]);
showToast("Copied to clipboard: " + text);
}
function runCommand(command) {
addToHistory(command);
const terminal = getTerminalCommand();
const wrappedCommand = command + "; echo '\nPress Enter to close...'; read";
Quickshell.execDetached([terminal.cmd, terminal.execFlag, "sh", "-c", wrappedCommand]);
showToast("Running in " + terminal.cmd + ": " + command);
}
function runBackground(command) {
addToHistory(command);
Quickshell.execDetached(["sh", "-c", command]);
showToast("Running in background: " + command);
}
function showToast(message) {
if (typeof ToastService !== "undefined") {
ToastService.showInfo("Command Runner", message);
}
}
function getTerminalCommand() {
if (pluginService) {
const terminal = pluginService.loadPluginData("commandRunner", "terminal", "kitty");
const execFlag = pluginService.loadPluginData("commandRunner", "execFlag", "-e");
if (terminal && execFlag) {
return {
cmd: terminal,
execFlag: execFlag
};
}
}
return {
cmd: "kitty",
execFlag: "-e"
};
}
function addToHistory(command) {
const index = commandHistory.indexOf(command);
if (index > -1) {
commandHistory.splice(index, 1);
}
commandHistory.unshift(command);
if (commandHistory.length > maxHistoryItems) {
commandHistory = commandHistory.slice(0, maxHistoryItems);
}
if (pluginService) {
pluginService.savePluginData("commandRunner", "history", commandHistory);
}
itemsChanged();
}
onTriggerChanged: {
if (!pluginService)
return;
pluginService.savePluginData("commandRunner", "trigger", trigger);
itemsChanged();
}
}