xonecas.io
822
Mp

macotron-plugin-caffeinate / commit 795ce5a

Add caffeinate toggle plugin

sacenox2026-10-29 795ce5aff34510ae21db18d6fd2aebac957763cd

2 files changed

README.md+48−0
@@ -0,0 +1,48 @@
1+# Caffeinate for Macotron
2+
3+Keep the Mac awake from the [Macotron](https://github.com/statico/macotron) menu bar. One click runs `caffeinate -dim`. Click again and that process stops. No terminal window stays open.
4+
5+- Click the cup to turn caffeinate on or off.
6+- Right-click or Control-click the cup for status.
7+- Run **Toggle Caffeinate** from the launcher, or bind it to a hotkey.
8+
9+A filled cup means a matching `caffeinate -dim` is running. A hollow cup means sleep is allowed.
10+
11+## Install
12+
13+1. Open Macotron. Go to Settings → Plugins → Catalog → Community.
14+2. Find **Caffeinate**. Press **Add**.
15+3. Read the source in the review sheet. Press **Add** again.
16+
17+Macotron scans the plugin before it runs.
18+
19+## How it works
20+
21+The process list decides the icon. The plugin does not store its own on or off flag, so a reload picks up a `caffeinate` that is already running and does not start a new one.
22+
23+Every five seconds the plugin runs `pgrep` for `caffeinate -dim`, including a copy started in a terminal. Other `caffeinate` commands, such as `caffeinate -i make`, are ignored.
24+
25+Turning it on runs this in the background:
26+
27+```sh
28+/usr/bin/caffeinate -dim -w $PPID >/dev/null 2>&1 &!
29+```
30+
31+`$PPID` is Macotron, because the command runs under `zsh -c`. caffeinate exits when Macotron quits, so quitting does not leave the Mac awake. `-d`, `-i`, and `-m` hold off idle display sleep, idle system sleep, and disk idle sleep.
32+
33+Turning it off runs `pkill` with the same pattern. The icon updates from a fresh `pgrep`, not from what the click asked for. The menu says "Keeping awake (caffeinate -dim)", "Sleep allowed", or "Checking...".
34+
35+## Debugging
36+
37+Plugin output goes to the system log under the `plugin` category.
38+
39+```sh
40+log stream --level info --style compact \
41+ --predicate 'subsystem == "io.statico.macotron" AND category == "plugin"'
42+```
43+
44+The plugin logs when it loads and when the state changes. A failed start or stop logs the shell stderr.
45+
46+## License
47+
48+MIT
caffeinate.js+102−0
@@ -0,0 +1,102 @@
1+macotron.plugin({
2+ title: "Caffeinate",
3+ description: "Keep the Mac awake with caffeinate -dim.",
4+ help: "Click the cup to toggle. Right-click for status.",
5+});
6+
7+// Also matches a bare `caffeinate -dim` from a terminal, so the icon reflects it and Off stops it. Other invocations such as `caffeinate -i make` do not match.
8+const MATCH = "^(/usr/bin/)?caffeinate -dim( -w [0-9]+)?$";
9+const SH = {
10+ // $PPID inside zsh -c is Macotron, so caffeinate exits when Macotron quits instead of leaving an assertion behind.
11+ // Redirect and disown. shell.run reads pipes to EOF, so a child holding them hangs the call.
12+ start: "/usr/bin/caffeinate -dim -w $PPID >/dev/null 2>&1 &!",
13+ stop: "/usr/bin/pkill -f '" + MATCH + "'",
14+ status: "/usr/bin/pgrep -f '" + MATCH + "'",
15+};
16+
17+const FROM_EXIT = { 0: "on", 1: "off" };
18+const VIEW = {
19+ unknown: { symbol: "cup.and.saucer", label: "Checking...", action: "Turn On", want: true },
20+ on: { symbol: "cup.and.saucer.fill", label: "Keeping awake (caffeinate -dim)", action: "Turn Off", want: false },
21+ off: { symbol: "cup.and.saucer", label: "Sleep allowed", action: "Turn On", want: true },
22+};
23+
24+let state = "unknown";
25+let busy = false;
26+
27+async function locked(fn) {
28+ while (busy) await macotron.sleep(20);
29+ busy = true;
30+ try {
31+ return await fn();
32+ } finally {
33+ busy = false;
34+ }
35+}
36+
37+async function probe() {
38+ const result = await macotron.shell.run(SH.status);
39+ const next = FROM_EXIT[result.exitCode];
40+ if (!next) {
41+ console.error("caffeinate: status failed:", result.stderr);
42+ return state;
43+ }
44+ if (next !== state) console.log("caffeinate: " + state + " -> " + next);
45+ state = next;
46+ return state;
47+}
48+
49+function paint() {
50+ const view = VIEW[state];
51+ macotron.menubar.status("caffeinate", {
52+ title: "",
53+ sfSymbol: view.symbol,
54+ onClick: toggle,
55+ menu: [
56+ { title: view.label },
57+ "-",
58+ { title: view.action, onClick: () => setOn(view.want) },
59+ ],
60+ });
61+}
62+
63+async function refresh() {
64+ if (busy) return;
65+ await locked(async () => {
66+ try {
67+ await probe();
68+ } catch (err) {
69+ console.error("caffeinate: status failed:", err);
70+ }
71+ paint();
72+ });
73+}
74+
75+async function setOn(on) {
76+ const want = on ? "on" : "off";
77+ await locked(async () => {
78+ let stderr = "";
79+ try {
80+ const result = await macotron.shell.run(on ? SH.start : SH.stop);
81+ stderr = result.stderr;
82+ await probe();
83+ } catch (err) {
84+ stderr = String(err);
85+ console.error("caffeinate: shell failed:", err);
86+ }
87+ paint();
88+ if (state === want) return;
89+ console.error("caffeinate: could not turn " + want, stderr);
90+ macotron.notify.toast("Caffeinate", "Could not turn " + want, { color: "error" });
91+ });
92+}
93+
94+function toggle() {
95+ return setOn(state !== "on");
96+}
97+
98+console.log("caffeinate: loaded");
99+paint();
100+refresh();
101+macotron.every(5000, refresh);
102+macotron.command("Toggle Caffeinate", "Keep the Mac awake with caffeinate -dim", toggle, { id: "caffeinate/toggle" });