@sdeck/plugin-system 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controls.d.ts +36 -0
- package/dist/controls.d.ts.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1731 -0
- package/dist/index.js.map +13 -0
- package/dist/monitor.d.ts +80 -0
- package/dist/monitor.d.ts.map +1 -0
- package/dist/watchers.d.ts +34 -0
- package/dist/watchers.d.ts.map +1 -0
- package/package.json +60 -4
- package/src/controls.ts +305 -0
- package/src/index.tsx +1075 -0
- package/src/monitor.ts +432 -0
- package/src/watchers.ts +245 -0
- package/README.md +0 -3
package/src/index.tsx
ADDED
|
@@ -0,0 +1,1075 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { cpus, freemem, homedir, totalmem } from "node:os";
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
defineAction,
|
|
6
|
+
definePlugin,
|
|
7
|
+
defineTrigger,
|
|
8
|
+
type PluginContext,
|
|
9
|
+
Shared,
|
|
10
|
+
type StateDef,
|
|
11
|
+
} from "@sdeck/plugin";
|
|
12
|
+
import {
|
|
13
|
+
formatAccelerator,
|
|
14
|
+
iconLayer,
|
|
15
|
+
iconLook,
|
|
16
|
+
type Layer,
|
|
17
|
+
type Look,
|
|
18
|
+
parseAccelerator,
|
|
19
|
+
textLayer,
|
|
20
|
+
} from "@sdeck/schema";
|
|
21
|
+
import { type MediaKey, systemControls } from "./controls.ts";
|
|
22
|
+
import {
|
|
23
|
+
type Counters,
|
|
24
|
+
coreSampler,
|
|
25
|
+
cpuTemperature,
|
|
26
|
+
type Disk,
|
|
27
|
+
diskUsage,
|
|
28
|
+
type Gpu,
|
|
29
|
+
ping,
|
|
30
|
+
readGpu,
|
|
31
|
+
watchFrameRate,
|
|
32
|
+
watchNetwork,
|
|
33
|
+
} from "./monitor.ts";
|
|
34
|
+
import {
|
|
35
|
+
namePattern,
|
|
36
|
+
networkAddresses,
|
|
37
|
+
processChanges,
|
|
38
|
+
watchFolder,
|
|
39
|
+
watchForeground,
|
|
40
|
+
watchIdle,
|
|
41
|
+
} from "./watchers.ts";
|
|
42
|
+
|
|
43
|
+
export * from "./controls.ts";
|
|
44
|
+
export * from "./monitor.ts";
|
|
45
|
+
export * from "./watchers.ts";
|
|
46
|
+
|
|
47
|
+
/** Opens a URL, file, folder or app with the system's default handler. */
|
|
48
|
+
export function openWithSystem(target: string): void {
|
|
49
|
+
const [command, args] =
|
|
50
|
+
process.platform === "win32"
|
|
51
|
+
? ["rundll32", ["url.dll,FileProtocolHandler", target]]
|
|
52
|
+
: process.platform === "darwin"
|
|
53
|
+
? ["open", [target]]
|
|
54
|
+
: ["xdg-open", [target]];
|
|
55
|
+
// Anything started from here would otherwise sit in the app's folder, which on Windows stops
|
|
56
|
+
// an update from replacing it.
|
|
57
|
+
spawn(command, args, {
|
|
58
|
+
cwd: homedir(),
|
|
59
|
+
detached: true,
|
|
60
|
+
stdio: "ignore",
|
|
61
|
+
windowsHide: true,
|
|
62
|
+
}).unref();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ShellResult {
|
|
66
|
+
/** Exit code (`-1` if it could not start or was killed). */
|
|
67
|
+
code: number;
|
|
68
|
+
/** Standard output, trimmed and capped at 64 KB. */
|
|
69
|
+
output: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const OUTPUT_LIMIT = 64 * 1024;
|
|
73
|
+
|
|
74
|
+
/** Runs a shell command; resolves with its exit code and output. Aborting kills it. */
|
|
75
|
+
export function runShell(
|
|
76
|
+
command: string,
|
|
77
|
+
cwd?: string,
|
|
78
|
+
signal?: AbortSignal,
|
|
79
|
+
): Promise<ShellResult> {
|
|
80
|
+
return new Promise((resolve) => {
|
|
81
|
+
let output = "";
|
|
82
|
+
const child = spawn(command, {
|
|
83
|
+
shell: true,
|
|
84
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
85
|
+
windowsHide: true,
|
|
86
|
+
...(cwd && { cwd }),
|
|
87
|
+
...(signal && { signal }),
|
|
88
|
+
});
|
|
89
|
+
child.stdout?.setEncoding("utf8");
|
|
90
|
+
child.stdout?.on("data", (chunk: string) => {
|
|
91
|
+
if (output.length < OUTPUT_LIMIT) output += chunk;
|
|
92
|
+
});
|
|
93
|
+
const done = (code: number) => resolve({ code, output: output.slice(0, OUTPUT_LIMIT).trim() });
|
|
94
|
+
child.once("error", () => done(-1));
|
|
95
|
+
child.once("close", (code) => done(code ?? -1));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The user's image, or the action's icon, with their text under it when they gave one. */
|
|
100
|
+
const buttonLook = (icon: string, caption: string, image: string, background: string): Look =>
|
|
101
|
+
iconLook({ icon: image || icon, caption, background });
|
|
102
|
+
|
|
103
|
+
const open = defineAction({
|
|
104
|
+
id: "open",
|
|
105
|
+
permissions: ["process"],
|
|
106
|
+
name: "Open",
|
|
107
|
+
description: "Opens a website, file, folder or app.",
|
|
108
|
+
category: "System",
|
|
109
|
+
icon: "external-link",
|
|
110
|
+
settings: {
|
|
111
|
+
target: {
|
|
112
|
+
type: "text",
|
|
113
|
+
label: "URL or path",
|
|
114
|
+
placeholder: "https://example.com or C:\\Tools\\app.exe",
|
|
115
|
+
},
|
|
116
|
+
text: {
|
|
117
|
+
type: "text",
|
|
118
|
+
label: "Text",
|
|
119
|
+
placeholder: "Optional, under the icon",
|
|
120
|
+
appearance: true,
|
|
121
|
+
},
|
|
122
|
+
image: { type: "image", label: "Image", appearance: true },
|
|
123
|
+
background: { type: "color", label: "Background", default: "#0969da", appearance: true },
|
|
124
|
+
},
|
|
125
|
+
run: ({ settings, plugin }) => {
|
|
126
|
+
if (!settings.target) throw new Error("nothing to open");
|
|
127
|
+
plugin.log("open", settings.target);
|
|
128
|
+
openWithSystem(settings.target);
|
|
129
|
+
return { label: `Open ${settings.target}` };
|
|
130
|
+
},
|
|
131
|
+
look: (s) => buttonLook("lucide/external-link", s.text, s.image, s.background),
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const command = defineAction({
|
|
135
|
+
id: "command",
|
|
136
|
+
permissions: ["shell"],
|
|
137
|
+
name: "Run command",
|
|
138
|
+
description: "Runs a shell command. Fails when it exits with an error; its output is the result.",
|
|
139
|
+
category: "System",
|
|
140
|
+
icon: "terminal",
|
|
141
|
+
settings: {
|
|
142
|
+
command: { type: "text", label: "Command", placeholder: "echo hello", multiline: true },
|
|
143
|
+
cwd: { type: "text", label: "Working directory", placeholder: "Defaults to your home folder" },
|
|
144
|
+
undo: {
|
|
145
|
+
type: "text",
|
|
146
|
+
label: "Undo command",
|
|
147
|
+
description: "Run to revert it (from the history, or a toggle's second press).",
|
|
148
|
+
multiline: true,
|
|
149
|
+
},
|
|
150
|
+
text: {
|
|
151
|
+
type: "text",
|
|
152
|
+
label: "Text",
|
|
153
|
+
placeholder: "Optional, under the icon",
|
|
154
|
+
appearance: true,
|
|
155
|
+
},
|
|
156
|
+
image: { type: "image", label: "Image", appearance: true },
|
|
157
|
+
background: { type: "color", label: "Background", default: "#24292f", appearance: true },
|
|
158
|
+
},
|
|
159
|
+
run: async ({ settings, plugin, signal }) => {
|
|
160
|
+
if (!settings.command) throw new Error("no command");
|
|
161
|
+
const cwd = settings.cwd || homedir();
|
|
162
|
+
plugin.log("run", settings.command);
|
|
163
|
+
const { code, output } = await runShell(settings.command, cwd, signal);
|
|
164
|
+
if (code !== 0) throw new Error(`"${settings.command}" exited with ${code}`);
|
|
165
|
+
const undo = settings.undo;
|
|
166
|
+
return {
|
|
167
|
+
result: output,
|
|
168
|
+
label: settings.command,
|
|
169
|
+
...(undo && {
|
|
170
|
+
undo: async () => {
|
|
171
|
+
const r = await runShell(undo, cwd);
|
|
172
|
+
if (r.code !== 0) throw new Error(`"${undo}" exited with ${r.code}`);
|
|
173
|
+
},
|
|
174
|
+
}),
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
look: (s) => buttonLook("lucide/terminal", s.text, s.image, s.background),
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
/** CPU use since the previous call, 0–100. */
|
|
181
|
+
export function cpuSampler(): () => number {
|
|
182
|
+
const read = () =>
|
|
183
|
+
cpus().reduce(
|
|
184
|
+
(acc, cpu) => {
|
|
185
|
+
const t = cpu.times;
|
|
186
|
+
acc.idle += t.idle;
|
|
187
|
+
acc.total += t.user + t.nice + t.sys + t.irq + t.idle;
|
|
188
|
+
return acc;
|
|
189
|
+
},
|
|
190
|
+
{ idle: 0, total: 0 },
|
|
191
|
+
);
|
|
192
|
+
let last = read();
|
|
193
|
+
return () => {
|
|
194
|
+
const now = read();
|
|
195
|
+
const total = now.total - last.total;
|
|
196
|
+
const idle = now.idle - last.idle;
|
|
197
|
+
last = now;
|
|
198
|
+
return total > 0 ? Math.round(100 * (1 - idle / total)) : 0;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Names of the running processes (`javaw.exe`, `firefox`), lowercased and de-duplicated. */
|
|
203
|
+
export function listProcesses(): Promise<string[]> {
|
|
204
|
+
const [file, args] =
|
|
205
|
+
process.platform === "win32"
|
|
206
|
+
? ["tasklist", ["/fo", "csv", "/nh"]]
|
|
207
|
+
: ["ps", ["-A", "-o", "comm="]];
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
execFile(file, args, { windowsHide: true, maxBuffer: 8 * 1024 * 1024 }, (error, stdout) => {
|
|
210
|
+
if (error) return reject(error);
|
|
211
|
+
const names = new Set<string>();
|
|
212
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
213
|
+
const name =
|
|
214
|
+
process.platform === "win32" ? /^"([^"]+)"/.exec(line)?.[1] : basename(line.trim());
|
|
215
|
+
if (name) names.add(name.toLowerCase());
|
|
216
|
+
}
|
|
217
|
+
resolve([...names].sort());
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** `{{system.<key>}}<unit>`, or "N/A" while this computer gives no value. */
|
|
223
|
+
const reading = (key: string, unit: string) =>
|
|
224
|
+
`{{system.${key} == null ? "N/A" : round(system.${key}) + "${unit}"}}`;
|
|
225
|
+
|
|
226
|
+
/** What each single-value choice of "System stats" shows. */
|
|
227
|
+
const METRICS = {
|
|
228
|
+
cpu: { icon: "lucide/cpu", text: reading("cpu", "%"), bar: "system.cpu" },
|
|
229
|
+
memory: { icon: "lucide/memory-stick", text: reading("memory", "%"), bar: "system.memory" },
|
|
230
|
+
disk: { icon: "lucide/hard-drive", text: reading("disk", "%"), bar: "system.disk" },
|
|
231
|
+
gpu: { icon: "lucide/gpu", text: reading("gpu", "%"), bar: "system.gpu" },
|
|
232
|
+
"gpu-temp": { icon: "lucide/thermometer", text: reading("gpuTemp", "°C"), bar: "system.gpuTemp" },
|
|
233
|
+
"cpu-temp": { icon: "lucide/thermometer", text: reading("cpuTemp", "°C"), bar: "system.cpuTemp" },
|
|
234
|
+
core: {
|
|
235
|
+
icon: "lucide/zap",
|
|
236
|
+
text: "{{max(system.cpuCores ?? 0)}}%",
|
|
237
|
+
bar: "max(system.cpuCores ?? 0)",
|
|
238
|
+
},
|
|
239
|
+
ping: { icon: "lucide/activity", text: reading("ping", " ms"), bar: undefined },
|
|
240
|
+
fps: { icon: "lucide/gauge", text: reading("fps", ""), bar: undefined },
|
|
241
|
+
} as const;
|
|
242
|
+
|
|
243
|
+
const stats = defineAction({
|
|
244
|
+
id: "stats",
|
|
245
|
+
permissions: [],
|
|
246
|
+
name: "System stats",
|
|
247
|
+
description:
|
|
248
|
+
"Live CPU, memory, disk, network, GPU, ping and FPS (also usable anywhere as {{system.cpu}}).",
|
|
249
|
+
category: "Monitoring",
|
|
250
|
+
icon: "activity",
|
|
251
|
+
slots: ["key", "sensor", "window"],
|
|
252
|
+
settings: {
|
|
253
|
+
show: {
|
|
254
|
+
type: "select",
|
|
255
|
+
label: "Show",
|
|
256
|
+
default: "both",
|
|
257
|
+
options: [
|
|
258
|
+
{ value: "both", label: "CPU and memory" },
|
|
259
|
+
{ value: "cpu", label: "CPU" },
|
|
260
|
+
{ value: "core", label: "Busiest CPU core" },
|
|
261
|
+
{ value: "memory", label: "Memory" },
|
|
262
|
+
{ value: "disk", label: "Disk space used" },
|
|
263
|
+
{ value: "network", label: "Network speed" },
|
|
264
|
+
{ value: "ping", label: "Ping" },
|
|
265
|
+
{ value: "gpu", label: "GPU load (NVIDIA)" },
|
|
266
|
+
{ value: "gpu-temp", label: "GPU temperature (NVIDIA)" },
|
|
267
|
+
{ value: "cpu-temp", label: "CPU temperature" },
|
|
268
|
+
{ value: "fps", label: "Frame rate (PresentMon)" },
|
|
269
|
+
],
|
|
270
|
+
},
|
|
271
|
+
color: { type: "color", label: "Colour", default: "#58a6ff" },
|
|
272
|
+
background: { type: "color", label: "Background", default: "#0d1117" },
|
|
273
|
+
},
|
|
274
|
+
display: true,
|
|
275
|
+
look: (s): Look => {
|
|
276
|
+
const bar = (id: string, value: string): Layer => ({
|
|
277
|
+
id,
|
|
278
|
+
type: "bar",
|
|
279
|
+
value,
|
|
280
|
+
color: s.color,
|
|
281
|
+
track: "#30363d",
|
|
282
|
+
position: "bottom",
|
|
283
|
+
thickness: 6,
|
|
284
|
+
});
|
|
285
|
+
/** An icon and its value on one line. */
|
|
286
|
+
const row = (id: string, icon: string, value: string, size: number): Layer[] => [
|
|
287
|
+
iconLayer(`${id}-icon`, icon, { color: s.color, size: 22, row: id }),
|
|
288
|
+
textLayer(id, value, { color: s.color, size, row: id }),
|
|
289
|
+
];
|
|
290
|
+
if (s.show === "both") {
|
|
291
|
+
return {
|
|
292
|
+
background: s.background,
|
|
293
|
+
layers: [
|
|
294
|
+
...row("cpu", "lucide/cpu", "{{system.cpu}}%", 18),
|
|
295
|
+
...row("memory", "lucide/memory-stick", "{{system.memory}}%", 18),
|
|
296
|
+
],
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
if (s.show === "network") {
|
|
300
|
+
return {
|
|
301
|
+
background: s.background,
|
|
302
|
+
layers: [
|
|
303
|
+
...row("down", "lucide/arrow-down", "{{bytes(system.netDown ?? 0)}}", 13),
|
|
304
|
+
...row("up", "lucide/arrow-up", "{{bytes(system.netUp ?? 0)}}", 13),
|
|
305
|
+
],
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
const metric = METRICS[s.show];
|
|
309
|
+
// One line per metric so the layer ids stay stable for style rules.
|
|
310
|
+
const id = s.show;
|
|
311
|
+
return {
|
|
312
|
+
background: s.background,
|
|
313
|
+
layers: [
|
|
314
|
+
iconLayer(`${id}-icon`, metric.icon, { color: s.color, size: 40, position: "top" }),
|
|
315
|
+
textLayer(id, metric.text, { color: s.color, size: 20, position: "bottom" }),
|
|
316
|
+
...(metric.bar ? [bar(`${id}-bar`, metric.bar)] : []),
|
|
317
|
+
],
|
|
318
|
+
};
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
// -- Controls -------------------------------------------------------------------------------------
|
|
323
|
+
|
|
324
|
+
const MEDIA = {
|
|
325
|
+
"play-pause": { label: "Play / Pause", icons: ["play", "pause"], undo: "play-pause" },
|
|
326
|
+
next: { label: "Next track", icons: ["skip-forward"], undo: "previous" },
|
|
327
|
+
previous: { label: "Previous track", icons: ["skip-back"], undo: "next" },
|
|
328
|
+
stop: { label: "Stop playback", icons: ["square"], undo: undefined },
|
|
329
|
+
"volume-up": { label: "Volume up", icons: ["volume-2", "plus"], undo: "volume-down" },
|
|
330
|
+
"volume-down": { label: "Volume down", icons: ["volume-1", "minus"], undo: "volume-up" },
|
|
331
|
+
mute: { label: "Mute", icons: ["volume-x"], undo: "mute" },
|
|
332
|
+
} as const satisfies Record<MediaKey, { label: string; icons: readonly string[]; undo?: MediaKey }>;
|
|
333
|
+
|
|
334
|
+
const media = defineAction({
|
|
335
|
+
id: "media",
|
|
336
|
+
permissions: ["input"],
|
|
337
|
+
name: "Media and volume",
|
|
338
|
+
description: "Presses a media or volume key, as a keyboard would.",
|
|
339
|
+
category: "Controls",
|
|
340
|
+
icon: "play",
|
|
341
|
+
slots: ["key", "sensor"],
|
|
342
|
+
settings: {
|
|
343
|
+
key: {
|
|
344
|
+
type: "select",
|
|
345
|
+
label: "Key",
|
|
346
|
+
default: "play-pause",
|
|
347
|
+
options: Object.entries(MEDIA).map(([value, m]) => ({ value, label: m.label })) as {
|
|
348
|
+
value: MediaKey;
|
|
349
|
+
label: string;
|
|
350
|
+
}[],
|
|
351
|
+
},
|
|
352
|
+
steps: {
|
|
353
|
+
type: "number",
|
|
354
|
+
label: "Presses",
|
|
355
|
+
description: "Each volume press is about 2%.",
|
|
356
|
+
default: 1,
|
|
357
|
+
min: 1,
|
|
358
|
+
max: 50,
|
|
359
|
+
visibleIf: { key: ["volume-up", "volume-down"] },
|
|
360
|
+
},
|
|
361
|
+
background: { type: "color", label: "Background", default: "#1f2937", appearance: true },
|
|
362
|
+
},
|
|
363
|
+
run: async ({ settings }) => {
|
|
364
|
+
const controls = await systemControls();
|
|
365
|
+
const { key } = settings;
|
|
366
|
+
const times = key === "volume-up" || key === "volume-down" ? settings.steps : 1;
|
|
367
|
+
const press = async (which: MediaKey) => {
|
|
368
|
+
for (let i = 0; i < times; i++) await controls.media(which);
|
|
369
|
+
};
|
|
370
|
+
await press(key);
|
|
371
|
+
const undo = MEDIA[key].undo;
|
|
372
|
+
return { label: MEDIA[key].label, ...(undo && { undo: () => press(undo) }) };
|
|
373
|
+
},
|
|
374
|
+
look: (s): Look => {
|
|
375
|
+
const { icons } = MEDIA[s.key];
|
|
376
|
+
return {
|
|
377
|
+
background: s.background,
|
|
378
|
+
layers: icons.map((name, i) =>
|
|
379
|
+
iconLayer(i === 0 ? "icon" : `icon-${i + 1}`, `lucide/${name}`, {
|
|
380
|
+
// The second icon of a pair is a smaller hint (play/pause are equals).
|
|
381
|
+
size: icons.length === 1 ? 58 : s.key === "play-pause" ? 36 : i === 0 ? 46 : 26,
|
|
382
|
+
row: "icons",
|
|
383
|
+
}),
|
|
384
|
+
),
|
|
385
|
+
};
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
const hotkey = defineAction({
|
|
390
|
+
id: "hotkey",
|
|
391
|
+
permissions: ["input"],
|
|
392
|
+
name: "Press a shortcut",
|
|
393
|
+
description: "Sends a keyboard shortcut to the focused app.",
|
|
394
|
+
category: "Controls",
|
|
395
|
+
icon: "keyboard",
|
|
396
|
+
slots: ["key", "sensor"],
|
|
397
|
+
settings: {
|
|
398
|
+
keys: { type: "text", label: "Shortcut", placeholder: "ctrl+shift+m" },
|
|
399
|
+
text: {
|
|
400
|
+
type: "text",
|
|
401
|
+
label: "Text",
|
|
402
|
+
placeholder: "Defaults to the shortcut",
|
|
403
|
+
appearance: true,
|
|
404
|
+
},
|
|
405
|
+
background: { type: "color", label: "Background", default: "#374151", appearance: true },
|
|
406
|
+
},
|
|
407
|
+
run: async ({ settings }) => {
|
|
408
|
+
const accelerator = parseAccelerator(settings.keys);
|
|
409
|
+
await (await systemControls()).hotkey(accelerator);
|
|
410
|
+
return { label: `Press ${formatAccelerator(accelerator)}` };
|
|
411
|
+
},
|
|
412
|
+
look: (s) =>
|
|
413
|
+
iconLook({ icon: "lucide/keyboard", caption: s.text || s.keys, background: s.background }),
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
const type = defineAction({
|
|
417
|
+
id: "type",
|
|
418
|
+
permissions: ["input"],
|
|
419
|
+
name: "Type text",
|
|
420
|
+
description: "Types text into the focused app. The text can use {{templates}}.",
|
|
421
|
+
category: "Controls",
|
|
422
|
+
icon: "text-cursor-input",
|
|
423
|
+
slots: ["key", "sensor"],
|
|
424
|
+
settings: {
|
|
425
|
+
content: { type: "text", label: "Text to type", multiline: true },
|
|
426
|
+
text: {
|
|
427
|
+
type: "text",
|
|
428
|
+
label: "Key text",
|
|
429
|
+
placeholder: "Optional, under the icon",
|
|
430
|
+
appearance: true,
|
|
431
|
+
},
|
|
432
|
+
background: { type: "color", label: "Background", default: "#374151", appearance: true },
|
|
433
|
+
},
|
|
434
|
+
run: async ({ settings }) => {
|
|
435
|
+
if (!settings.content) throw new Error("nothing to type");
|
|
436
|
+
await (await systemControls()).type(settings.content);
|
|
437
|
+
return { label: `Type ${settings.content.length} character(s)` };
|
|
438
|
+
},
|
|
439
|
+
look: (s) =>
|
|
440
|
+
iconLook({ icon: "lucide/text-cursor-input", caption: s.text, background: s.background }),
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const lock = defineAction({
|
|
444
|
+
id: "lock",
|
|
445
|
+
permissions: ["process"],
|
|
446
|
+
name: "Lock the screen",
|
|
447
|
+
category: "Controls",
|
|
448
|
+
icon: "lock",
|
|
449
|
+
slots: ["key", "sensor"],
|
|
450
|
+
settings: {
|
|
451
|
+
text: {
|
|
452
|
+
type: "text",
|
|
453
|
+
label: "Text",
|
|
454
|
+
placeholder: "Optional, under the icon",
|
|
455
|
+
appearance: true,
|
|
456
|
+
},
|
|
457
|
+
background: { type: "color", label: "Background", default: "#7c2d12", appearance: true },
|
|
458
|
+
},
|
|
459
|
+
run: async () => {
|
|
460
|
+
await (await systemControls()).lock();
|
|
461
|
+
return { label: "Lock the screen" };
|
|
462
|
+
},
|
|
463
|
+
look: (s) => iconLook({ icon: "lucide/lock", caption: s.text, background: s.background }),
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
const sleep = defineAction({
|
|
467
|
+
id: "sleep",
|
|
468
|
+
permissions: ["process"],
|
|
469
|
+
name: "Sleep",
|
|
470
|
+
description: "Puts the computer to sleep.",
|
|
471
|
+
category: "Controls",
|
|
472
|
+
icon: "moon",
|
|
473
|
+
slots: ["key", "sensor"],
|
|
474
|
+
settings: {
|
|
475
|
+
text: {
|
|
476
|
+
type: "text",
|
|
477
|
+
label: "Text",
|
|
478
|
+
placeholder: "Optional, under the icon",
|
|
479
|
+
appearance: true,
|
|
480
|
+
},
|
|
481
|
+
background: { type: "color", label: "Background", default: "#1e1b4b", appearance: true },
|
|
482
|
+
},
|
|
483
|
+
run: async () => {
|
|
484
|
+
await (await systemControls()).sleep();
|
|
485
|
+
return { label: "Sleep" };
|
|
486
|
+
},
|
|
487
|
+
look: (s) => iconLook({ icon: "lucide/moon", caption: s.text, background: s.background }),
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
/** `screenshot-20261130-142501.png`. */
|
|
491
|
+
export function screenshotName(at: Date): string {
|
|
492
|
+
const two = (n: number) => String(n).padStart(2, "0");
|
|
493
|
+
const day = `${at.getFullYear()}${two(at.getMonth() + 1)}${two(at.getDate())}`;
|
|
494
|
+
const time = `${two(at.getHours())}${two(at.getMinutes())}${two(at.getSeconds())}`;
|
|
495
|
+
return `screenshot-${day}-${time}.png`;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const screenshot = defineAction({
|
|
499
|
+
id: "screenshot",
|
|
500
|
+
permissions: ["process", "filesystem"],
|
|
501
|
+
name: "Screenshot",
|
|
502
|
+
description: "Saves the screen to a file (the result), or opens the region picker.",
|
|
503
|
+
category: "Controls",
|
|
504
|
+
icon: "camera",
|
|
505
|
+
slots: ["key", "sensor"],
|
|
506
|
+
settings: {
|
|
507
|
+
mode: {
|
|
508
|
+
type: "select",
|
|
509
|
+
label: "Capture",
|
|
510
|
+
default: "file",
|
|
511
|
+
options: [
|
|
512
|
+
{ value: "file", label: "Whole screen, to a file" },
|
|
513
|
+
{ value: "snip", label: "A region I choose" },
|
|
514
|
+
],
|
|
515
|
+
},
|
|
516
|
+
folder: {
|
|
517
|
+
type: "text",
|
|
518
|
+
label: "Folder",
|
|
519
|
+
placeholder: "Pictures/sdeck",
|
|
520
|
+
visibleIf: { mode: "file" },
|
|
521
|
+
},
|
|
522
|
+
text: {
|
|
523
|
+
type: "text",
|
|
524
|
+
label: "Text",
|
|
525
|
+
placeholder: "Optional, under the icon",
|
|
526
|
+
appearance: true,
|
|
527
|
+
},
|
|
528
|
+
background: { type: "color", label: "Background", default: "#065f46", appearance: true },
|
|
529
|
+
},
|
|
530
|
+
run: async ({ settings }) => {
|
|
531
|
+
const controls = await systemControls();
|
|
532
|
+
if (settings.mode === "snip") {
|
|
533
|
+
await controls.snip();
|
|
534
|
+
return { label: "Screenshot a region" };
|
|
535
|
+
}
|
|
536
|
+
const folder = settings.folder || join(homedir(), "Pictures", "sdeck");
|
|
537
|
+
const file = join(folder, screenshotName(new Date()));
|
|
538
|
+
await controls.screenshot(file);
|
|
539
|
+
return { result: file, label: `Screenshot ${file}` };
|
|
540
|
+
},
|
|
541
|
+
look: (s) =>
|
|
542
|
+
iconLook({
|
|
543
|
+
icon: s.mode === "snip" ? "lucide/scissors" : "lucide/camera",
|
|
544
|
+
caption: s.text,
|
|
545
|
+
background: s.background,
|
|
546
|
+
}),
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
const pingAction = defineAction({
|
|
550
|
+
id: "ping",
|
|
551
|
+
permissions: ["network"],
|
|
552
|
+
name: "Ping",
|
|
553
|
+
description: "Pings a host; the result is the round trip in ms. Fails when it does not answer.",
|
|
554
|
+
category: "Monitoring",
|
|
555
|
+
icon: "radio",
|
|
556
|
+
settings: {
|
|
557
|
+
host: { type: "text", label: "Host", default: "1.1.1.1" },
|
|
558
|
+
background: { type: "color", label: "Background", default: "#0d1117", appearance: true },
|
|
559
|
+
},
|
|
560
|
+
run: async ({ settings, signal }) => {
|
|
561
|
+
const ms = await ping(settings.host, signal);
|
|
562
|
+
return { result: ms, label: `Ping ${settings.host}: ${ms} ms` };
|
|
563
|
+
},
|
|
564
|
+
look: (s) => iconLook({ icon: "lucide/activity", caption: s.host, background: s.background }),
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// -- Shared sources ------------------------------------------------------------------------------
|
|
568
|
+
|
|
569
|
+
/** The running processes, read every 3 s while anything listens. */
|
|
570
|
+
const processes = new Shared<ReadonlySet<string>>((emit) => {
|
|
571
|
+
let stopped = false;
|
|
572
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
573
|
+
const tick = async () => {
|
|
574
|
+
try {
|
|
575
|
+
emit(new Set(await listProcesses()));
|
|
576
|
+
} catch {
|
|
577
|
+
// Try again next time.
|
|
578
|
+
}
|
|
579
|
+
if (!stopped) timer = setTimeout(tick, 3000);
|
|
580
|
+
};
|
|
581
|
+
void tick();
|
|
582
|
+
return () => {
|
|
583
|
+
stopped = true;
|
|
584
|
+
clearTimeout(timer);
|
|
585
|
+
};
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
/** Network addresses, read every 5 s. */
|
|
589
|
+
const network = new Shared<string[]>((emit) => {
|
|
590
|
+
emit(networkAddresses());
|
|
591
|
+
const timer = setInterval(() => emit(networkAddresses()), 5000);
|
|
592
|
+
return () => clearInterval(timer);
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
let idleError: Error | undefined;
|
|
596
|
+
/** Seconds since the last input. */
|
|
597
|
+
const idle = new Shared<number>((emit) => {
|
|
598
|
+
idleError = undefined;
|
|
599
|
+
return watchIdle(emit, (error) => {
|
|
600
|
+
idleError = error;
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
let foregroundError: Error | undefined;
|
|
605
|
+
/** The executable of the focused window. */
|
|
606
|
+
const foreground = new Shared<string>((emit) => {
|
|
607
|
+
foregroundError = undefined;
|
|
608
|
+
return watchForeground(emit, (error) => {
|
|
609
|
+
foregroundError = error;
|
|
610
|
+
});
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
// -- Measurements ---------------------------------------------------------------------------------
|
|
614
|
+
|
|
615
|
+
/** How long a measurement that cannot work here is left alone before trying again. */
|
|
616
|
+
const RETRY_MS = 60_000;
|
|
617
|
+
const unavailable = new Map<string, { reason: string; retryAt: number }>();
|
|
618
|
+
|
|
619
|
+
function report(context: PluginContext) {
|
|
620
|
+
const reasons = [...unavailable].map(([what, u]) => `${what}: ${u.reason}`);
|
|
621
|
+
context.status(
|
|
622
|
+
reasons.length > 0 ? `Unavailable here. ${reasons.join(". ")}` : undefined,
|
|
623
|
+
"info",
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
function markUnavailable(context: PluginContext, what: string, error: unknown) {
|
|
628
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
629
|
+
if (unavailable.get(what)?.reason !== reason) context.log(`${what} is unavailable:`, reason);
|
|
630
|
+
unavailable.set(what, { reason, retryAt: Date.now() + RETRY_MS });
|
|
631
|
+
report(context);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** `read()`, or `undefined` (with the reason in the plugin's status) when it cannot work. */
|
|
635
|
+
async function measured<T>(
|
|
636
|
+
context: PluginContext,
|
|
637
|
+
what: string,
|
|
638
|
+
read: () => Promise<T>,
|
|
639
|
+
): Promise<T | undefined> {
|
|
640
|
+
const failed = unavailable.get(what);
|
|
641
|
+
if (failed && Date.now() < failed.retryAt) return undefined;
|
|
642
|
+
try {
|
|
643
|
+
const value = await read();
|
|
644
|
+
if (unavailable.delete(what)) report(context);
|
|
645
|
+
return value;
|
|
646
|
+
} catch (error) {
|
|
647
|
+
markUnavailable(context, what, error);
|
|
648
|
+
return undefined;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
let gpuReading: { at: number; value: Promise<Gpu | undefined> } | undefined;
|
|
653
|
+
/** One `nvidia-smi` call feeds every GPU value. */
|
|
654
|
+
function gpu(context: PluginContext): Promise<Gpu | undefined> {
|
|
655
|
+
if (!gpuReading || Date.now() - gpuReading.at > 1500) {
|
|
656
|
+
gpuReading = { at: Date.now(), value: measured(context, "GPU", readGpu) };
|
|
657
|
+
}
|
|
658
|
+
return gpuReading.value;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const defaultDisk = () =>
|
|
662
|
+
process.platform === "win32" ? `${process.env.SystemDrive ?? "C:"}\\` : "/";
|
|
663
|
+
|
|
664
|
+
/** Usage of each disk in the plugin's "Disks" setting, by name (`c`, `/home`). */
|
|
665
|
+
async function disks(context: PluginContext): Promise<Record<string, Disk>> {
|
|
666
|
+
const list = String(context.settings().disks || defaultDisk())
|
|
667
|
+
.split(",")
|
|
668
|
+
.map((d) => d.trim())
|
|
669
|
+
.filter(Boolean);
|
|
670
|
+
const out: Record<string, Disk> = {};
|
|
671
|
+
for (const path of list) {
|
|
672
|
+
const name = /^[a-z]:/i.test(path) ? (path[0] as string).toLowerCase() : path;
|
|
673
|
+
const full = /^[a-z]:$/i.test(path) ? `${path}\\` : path;
|
|
674
|
+
const usage = await measured(context, `disk ${path}`, () => diskUsage(full));
|
|
675
|
+
if (usage) out[name] = usage;
|
|
676
|
+
}
|
|
677
|
+
return out;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** Network speed, shared by the two values it feeds. */
|
|
681
|
+
const throughputSource = new Shared<Counters>((emit) =>
|
|
682
|
+
watchNetwork(emit, (error) => networkError?.(error)),
|
|
683
|
+
);
|
|
684
|
+
let networkError: ((error: Error) => void) | undefined;
|
|
685
|
+
|
|
686
|
+
const netState = (id: string, name: string, pick: (c: Counters) => number): StateDef => ({
|
|
687
|
+
id,
|
|
688
|
+
name,
|
|
689
|
+
type: "number",
|
|
690
|
+
unit: "B/s",
|
|
691
|
+
permissions: ["process"],
|
|
692
|
+
start: (set, context) => {
|
|
693
|
+
networkError = (error) => markUnavailable(context, "network speed", error);
|
|
694
|
+
return throughputSource.listen((rate) => set(pick(rate)));
|
|
695
|
+
},
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
const gpuState = (
|
|
699
|
+
id: string,
|
|
700
|
+
name: string,
|
|
701
|
+
type: "number" | "string",
|
|
702
|
+
pick: (g: Gpu) => unknown,
|
|
703
|
+
unit?: string,
|
|
704
|
+
): StateDef => ({
|
|
705
|
+
id,
|
|
706
|
+
name,
|
|
707
|
+
type,
|
|
708
|
+
...(unit && { unit }),
|
|
709
|
+
permissions: ["process"],
|
|
710
|
+
poll: {
|
|
711
|
+
everyMs: 2000,
|
|
712
|
+
read: async (context) => {
|
|
713
|
+
const g = await gpu(context);
|
|
714
|
+
return g && pick(g);
|
|
715
|
+
},
|
|
716
|
+
},
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
const monitoring: StateDef[] = [
|
|
720
|
+
(() => {
|
|
721
|
+
let sample: (() => number[]) | undefined;
|
|
722
|
+
return {
|
|
723
|
+
id: "cpuCores",
|
|
724
|
+
permissions: [],
|
|
725
|
+
name: "CPU use per core",
|
|
726
|
+
type: "json" as const,
|
|
727
|
+
description: "system.cpuCores[0] is the first core.",
|
|
728
|
+
poll: {
|
|
729
|
+
everyMs: 2000,
|
|
730
|
+
read: () => {
|
|
731
|
+
sample ??= coreSampler();
|
|
732
|
+
return sample();
|
|
733
|
+
},
|
|
734
|
+
},
|
|
735
|
+
};
|
|
736
|
+
})(),
|
|
737
|
+
{
|
|
738
|
+
id: "disks",
|
|
739
|
+
permissions: ["filesystem"],
|
|
740
|
+
name: "Disks",
|
|
741
|
+
type: "json",
|
|
742
|
+
description: "system.disks.c.percent, .free, .total (choose them in the plugin's settings).",
|
|
743
|
+
poll: { everyMs: 15_000, read: disks },
|
|
744
|
+
},
|
|
745
|
+
{
|
|
746
|
+
id: "disk",
|
|
747
|
+
permissions: ["filesystem"],
|
|
748
|
+
name: "Disk space used",
|
|
749
|
+
type: "number",
|
|
750
|
+
unit: "%",
|
|
751
|
+
description: "The first disk in the plugin's settings.",
|
|
752
|
+
poll: {
|
|
753
|
+
everyMs: 15_000,
|
|
754
|
+
read: async (context) => Object.values(await disks(context))[0]?.percent,
|
|
755
|
+
},
|
|
756
|
+
},
|
|
757
|
+
netState("netDown", "Download speed", (c) => c.received),
|
|
758
|
+
netState("netUp", "Upload speed", (c) => c.sent),
|
|
759
|
+
{
|
|
760
|
+
id: "ping",
|
|
761
|
+
permissions: ["network"],
|
|
762
|
+
name: "Ping",
|
|
763
|
+
type: "number",
|
|
764
|
+
unit: "ms",
|
|
765
|
+
description: "To the host in the plugin's settings.",
|
|
766
|
+
poll: {
|
|
767
|
+
everyMs: 5000,
|
|
768
|
+
read: (context) => measured(context, "ping", () => ping(String(context.settings().pingHost))),
|
|
769
|
+
},
|
|
770
|
+
},
|
|
771
|
+
gpuState("gpu", "GPU load", "number", (g) => g.load, "%"),
|
|
772
|
+
gpuState("gpuTemp", "GPU temperature", "number", (g) => g.temperature, "°C"),
|
|
773
|
+
gpuState("gpuMemory", "GPU memory used", "number", (g) => g.memory, "%"),
|
|
774
|
+
gpuState("gpuName", "GPU name", "string", (g) => g.name),
|
|
775
|
+
{
|
|
776
|
+
id: "cpuTemp",
|
|
777
|
+
permissions: ["process"],
|
|
778
|
+
name: "CPU temperature",
|
|
779
|
+
type: "number",
|
|
780
|
+
unit: "°C",
|
|
781
|
+
poll: {
|
|
782
|
+
everyMs: 5000,
|
|
783
|
+
read: (context) => measured(context, "CPU temperature", () => cpuTemperature()),
|
|
784
|
+
},
|
|
785
|
+
},
|
|
786
|
+
{
|
|
787
|
+
id: "fps",
|
|
788
|
+
permissions: ["process"],
|
|
789
|
+
name: "Frame rate",
|
|
790
|
+
type: "number",
|
|
791
|
+
description: "Of the app in front, measured by PresentMon.",
|
|
792
|
+
start: (set, context) => {
|
|
793
|
+
// Kept current so the app in front is the one measured.
|
|
794
|
+
let front: string | undefined;
|
|
795
|
+
const stopFront = foreground.listen((app) => {
|
|
796
|
+
front = app;
|
|
797
|
+
});
|
|
798
|
+
let stop = () => {};
|
|
799
|
+
const begin = () => {
|
|
800
|
+
stop();
|
|
801
|
+
stop = watchFrameRate(
|
|
802
|
+
String(context.settings().presentMon),
|
|
803
|
+
() => front,
|
|
804
|
+
set,
|
|
805
|
+
(error) => markUnavailable(context, "frame rate", error),
|
|
806
|
+
);
|
|
807
|
+
};
|
|
808
|
+
begin();
|
|
809
|
+
let command = context.settings().presentMon;
|
|
810
|
+
const off = context.onSettings(() => {
|
|
811
|
+
if (context.settings().presentMon === command) return;
|
|
812
|
+
command = context.settings().presentMon;
|
|
813
|
+
unavailable.delete("frame rate");
|
|
814
|
+
begin();
|
|
815
|
+
});
|
|
816
|
+
return () => {
|
|
817
|
+
off();
|
|
818
|
+
stop();
|
|
819
|
+
stopFront();
|
|
820
|
+
};
|
|
821
|
+
},
|
|
822
|
+
},
|
|
823
|
+
];
|
|
824
|
+
|
|
825
|
+
// -- Triggers ------------------------------------------------------------------------------------
|
|
826
|
+
|
|
827
|
+
const processSetting = {
|
|
828
|
+
process: {
|
|
829
|
+
type: "text",
|
|
830
|
+
label: "Process",
|
|
831
|
+
placeholder: "javaw.exe",
|
|
832
|
+
description: "The executable name, as in the task manager (case does not matter).",
|
|
833
|
+
},
|
|
834
|
+
} as const;
|
|
835
|
+
|
|
836
|
+
/** Fires `fire` when `name` starts (`started`) or stops, ignoring the state it was in at first. */
|
|
837
|
+
function onProcess(name: string, want: "started" | "stopped", fire: (event: unknown) => void) {
|
|
838
|
+
const target = name.trim().toLowerCase();
|
|
839
|
+
if (!target) throw new Error("no process name");
|
|
840
|
+
let before: ReadonlySet<string> | undefined;
|
|
841
|
+
return processes.listen((now) => {
|
|
842
|
+
if (before) {
|
|
843
|
+
const changes = processChanges(before, now);
|
|
844
|
+
if (changes[want].includes(target)) fire({ process: target });
|
|
845
|
+
}
|
|
846
|
+
before = now;
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
const appOpen = defineTrigger({
|
|
851
|
+
id: "app-open",
|
|
852
|
+
permissions: ["process"],
|
|
853
|
+
name: "When an app opens",
|
|
854
|
+
description: "Runs when a process starts (checked every 3 s).",
|
|
855
|
+
settings: processSetting,
|
|
856
|
+
subscribe: ({ settings, fire }) => onProcess(settings.process, "started", fire),
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
const appClose = defineTrigger({
|
|
860
|
+
id: "app-close",
|
|
861
|
+
permissions: ["process"],
|
|
862
|
+
name: "When an app closes",
|
|
863
|
+
description: "Runs when a process exits (checked every 3 s).",
|
|
864
|
+
settings: processSetting,
|
|
865
|
+
subscribe: ({ settings, fire }) => onProcess(settings.process, "stopped", fire),
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
const fileCreated = defineTrigger({
|
|
869
|
+
id: "file-created",
|
|
870
|
+
permissions: ["filesystem"],
|
|
871
|
+
name: "When a file appears",
|
|
872
|
+
description: "Runs when a new file shows up in a folder (event.name, event.path).",
|
|
873
|
+
settings: {
|
|
874
|
+
folder: { type: "text", label: "Folder", placeholder: "C:\\Users\\me\\Downloads" },
|
|
875
|
+
pattern: {
|
|
876
|
+
type: "text",
|
|
877
|
+
label: "Name pattern",
|
|
878
|
+
placeholder: "*.png",
|
|
879
|
+
description: "Empty means any file.",
|
|
880
|
+
},
|
|
881
|
+
},
|
|
882
|
+
subscribe: ({ settings, fire }) => {
|
|
883
|
+
if (!settings.folder) throw new Error("no folder");
|
|
884
|
+
const pattern = namePattern(settings.pattern);
|
|
885
|
+
return watchFolder(settings.folder, (name) => {
|
|
886
|
+
if (pattern.test(name)) fire({ name, path: join(settings.folder, name) });
|
|
887
|
+
});
|
|
888
|
+
},
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
const networkChange = defineTrigger({
|
|
892
|
+
id: "network-change",
|
|
893
|
+
permissions: [],
|
|
894
|
+
name: "When the network changes",
|
|
895
|
+
description: "Runs when network addresses change (event.online, event.addresses).",
|
|
896
|
+
settings: {},
|
|
897
|
+
subscribe: ({ fire }) => {
|
|
898
|
+
let before: string | undefined;
|
|
899
|
+
return network.listen((addresses) => {
|
|
900
|
+
const key = addresses.join("\n");
|
|
901
|
+
if (before !== undefined && key !== before) {
|
|
902
|
+
fire({ online: addresses.length > 0, addresses });
|
|
903
|
+
}
|
|
904
|
+
before = key;
|
|
905
|
+
});
|
|
906
|
+
},
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
const idleTrigger = defineTrigger({
|
|
910
|
+
id: "idle",
|
|
911
|
+
permissions: ["process"],
|
|
912
|
+
name: "When the computer is idle",
|
|
913
|
+
description: "Runs after no keyboard or mouse input for a while, or when input comes back.",
|
|
914
|
+
settings: {
|
|
915
|
+
minutes: { type: "number", label: "Idle for (minutes)", default: 5, min: 1, max: 600 },
|
|
916
|
+
when: {
|
|
917
|
+
type: "select",
|
|
918
|
+
label: "Run",
|
|
919
|
+
default: "idle",
|
|
920
|
+
options: [
|
|
921
|
+
{ value: "idle", label: "When it becomes idle" },
|
|
922
|
+
{ value: "back", label: "When input comes back" },
|
|
923
|
+
],
|
|
924
|
+
},
|
|
925
|
+
},
|
|
926
|
+
subscribe: ({ settings, fire, plugin }) => {
|
|
927
|
+
const limit = settings.minutes * 60;
|
|
928
|
+
let isIdle = false;
|
|
929
|
+
const stop = idle.listen((seconds) => {
|
|
930
|
+
if (!isIdle && seconds >= limit) {
|
|
931
|
+
isIdle = true;
|
|
932
|
+
if (settings.when === "idle") fire({ seconds });
|
|
933
|
+
} else if (isIdle && seconds < limit) {
|
|
934
|
+
isIdle = false;
|
|
935
|
+
if (settings.when === "back") fire({ seconds });
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
const check = setTimeout(() => {
|
|
939
|
+
if (idleError) plugin.log("idle trigger:", idleError.message);
|
|
940
|
+
}, 5000);
|
|
941
|
+
return () => {
|
|
942
|
+
clearTimeout(check);
|
|
943
|
+
stop();
|
|
944
|
+
};
|
|
945
|
+
},
|
|
946
|
+
});
|
|
947
|
+
|
|
948
|
+
const appFocus = defineTrigger({
|
|
949
|
+
id: "app-focus",
|
|
950
|
+
permissions: ["process"],
|
|
951
|
+
name: "When an app comes to the front",
|
|
952
|
+
description: "Runs when a window of this app gets focus (event.app), or of any app if empty.",
|
|
953
|
+
settings: {
|
|
954
|
+
process: {
|
|
955
|
+
...processSetting.process,
|
|
956
|
+
description: "The executable name; empty means any app.",
|
|
957
|
+
},
|
|
958
|
+
},
|
|
959
|
+
subscribe: ({ settings, fire, plugin }) => {
|
|
960
|
+
const target = settings.process.trim().toLowerCase();
|
|
961
|
+
let first = true;
|
|
962
|
+
const stop = foreground.listen((app) => {
|
|
963
|
+
// The app in front when the trigger starts did not just come to the front.
|
|
964
|
+
if (!first && app && (!target || app === target)) fire({ app });
|
|
965
|
+
first = false;
|
|
966
|
+
});
|
|
967
|
+
const check = setTimeout(() => {
|
|
968
|
+
if (foregroundError) plugin.log("app focus trigger:", foregroundError.message);
|
|
969
|
+
}, 5000);
|
|
970
|
+
return () => {
|
|
971
|
+
clearTimeout(check);
|
|
972
|
+
stop();
|
|
973
|
+
};
|
|
974
|
+
},
|
|
975
|
+
});
|
|
976
|
+
|
|
977
|
+
export default definePlugin({
|
|
978
|
+
id: "@sdeck/plugin-system",
|
|
979
|
+
name: "System",
|
|
980
|
+
version: "0.3.0",
|
|
981
|
+
description:
|
|
982
|
+
"Open apps and run commands, press media keys and shortcuts, lock and sleep, take screenshots, and monitor the computer.",
|
|
983
|
+
author: "sdeck",
|
|
984
|
+
permissions: ["shell", "process", "input", "network", "filesystem"],
|
|
985
|
+
settings: {
|
|
986
|
+
pingHost: { type: "text", label: "Ping host", default: "1.1.1.1" },
|
|
987
|
+
disks: {
|
|
988
|
+
type: "text",
|
|
989
|
+
label: "Disks",
|
|
990
|
+
placeholder: process.platform === "win32" ? "C:, D:" : "/, /home",
|
|
991
|
+
description: "Comma separated. Empty means the system disk.",
|
|
992
|
+
},
|
|
993
|
+
presentMon: {
|
|
994
|
+
type: "text",
|
|
995
|
+
label: "PresentMon",
|
|
996
|
+
default: "PresentMon",
|
|
997
|
+
description: "Command or full path, for the frame rate (Windows).",
|
|
998
|
+
},
|
|
999
|
+
},
|
|
1000
|
+
actions: [open, command, media, hotkey, type, screenshot, lock, sleep, stats, pingAction],
|
|
1001
|
+
triggers: [appOpen, appClose, appFocus, fileCreated, networkChange, idleTrigger],
|
|
1002
|
+
state: [
|
|
1003
|
+
...monitoring,
|
|
1004
|
+
(() => {
|
|
1005
|
+
let sample: (() => number) | undefined;
|
|
1006
|
+
return {
|
|
1007
|
+
id: "cpu",
|
|
1008
|
+
permissions: [],
|
|
1009
|
+
name: "CPU use",
|
|
1010
|
+
type: "number" as const,
|
|
1011
|
+
unit: "%",
|
|
1012
|
+
poll: {
|
|
1013
|
+
everyMs: 2000,
|
|
1014
|
+
read: () => {
|
|
1015
|
+
sample ??= cpuSampler();
|
|
1016
|
+
return sample();
|
|
1017
|
+
},
|
|
1018
|
+
},
|
|
1019
|
+
};
|
|
1020
|
+
})(),
|
|
1021
|
+
{
|
|
1022
|
+
id: "memory",
|
|
1023
|
+
permissions: [],
|
|
1024
|
+
name: "Memory use",
|
|
1025
|
+
type: "number",
|
|
1026
|
+
unit: "%",
|
|
1027
|
+
poll: { everyMs: 2000, read: () => Math.round(100 * (1 - freemem() / totalmem())) },
|
|
1028
|
+
},
|
|
1029
|
+
{
|
|
1030
|
+
id: "memoryFree",
|
|
1031
|
+
permissions: [],
|
|
1032
|
+
name: "Free memory",
|
|
1033
|
+
type: "number",
|
|
1034
|
+
unit: "bytes",
|
|
1035
|
+
poll: { everyMs: 2000, read: () => freemem() },
|
|
1036
|
+
},
|
|
1037
|
+
{
|
|
1038
|
+
id: "processes",
|
|
1039
|
+
permissions: ["process"],
|
|
1040
|
+
name: "Running processes",
|
|
1041
|
+
type: "json",
|
|
1042
|
+
description: 'Lowercased names; e.g. contains(system.processes, "javaw.exe").',
|
|
1043
|
+
start: (set) => processes.listen((names) => set([...names])),
|
|
1044
|
+
},
|
|
1045
|
+
{
|
|
1046
|
+
id: "idle",
|
|
1047
|
+
permissions: ["process"],
|
|
1048
|
+
name: "Idle time",
|
|
1049
|
+
type: "number",
|
|
1050
|
+
unit: "s",
|
|
1051
|
+
description: "Seconds since the last keyboard or mouse input.",
|
|
1052
|
+
start: (set) => idle.listen(set),
|
|
1053
|
+
},
|
|
1054
|
+
{
|
|
1055
|
+
id: "foreground",
|
|
1056
|
+
permissions: ["process"],
|
|
1057
|
+
name: "App in front",
|
|
1058
|
+
type: "string",
|
|
1059
|
+
description:
|
|
1060
|
+
'The focused window\'s executable, lowercased ("javaw.exe"); empty when none. Profiles switch on it.',
|
|
1061
|
+
start: (set) => foreground.listen(set),
|
|
1062
|
+
},
|
|
1063
|
+
{
|
|
1064
|
+
id: "online",
|
|
1065
|
+
permissions: [],
|
|
1066
|
+
name: "Online",
|
|
1067
|
+
type: "boolean",
|
|
1068
|
+
description: "Whether any network interface has an address.",
|
|
1069
|
+
start: (set) => network.listen((addresses) => set(addresses.length > 0)),
|
|
1070
|
+
},
|
|
1071
|
+
],
|
|
1072
|
+
activate(context: PluginContext) {
|
|
1073
|
+
context.log("ready on", process.platform);
|
|
1074
|
+
},
|
|
1075
|
+
});
|