xonecas.io
822
Mp

macotron-plugin-caffeinate / files

macotron-plugin-caffeinate / caffeinate.js

2.9 KB
1macotron.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.
8const MATCH = "^(/usr/bin/)?caffeinate -dim( -w [0-9]+)?$";
9const 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
17const FROM_EXIT = { 0: "on", 1: "off" };
18const 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
24let state = "unknown";
25let busy = false;
26
27async 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
37async 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
49function 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
63async 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
75async 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
94function toggle() {
95  return setOn(state !== "on");
96}
97
98console.log("caffeinate: loaded");
99paint();
100refresh();
101macotron.every(5000, refresh);
102macotron.command("Toggle Caffeinate", "Keep the Mac awake with caffeinate -dim", toggle, { id: "caffeinate/toggle" });