import { MenuBarExtra, getPreferenceValues } from "@raycast/api"; import { system } from "@appy/api"; import { useEffect, useState } from "react"; // Menu bar space is short (and shorter beside a notch): every number keeps a fixed width, padded with figure spaces, // so the item never changes size and the icons beside it never jump. const FIG = "\u2007"; const pad = (s: string, n: number) => FIG.repeat(Math.max(0, n - s.length)) + s; const pct = (n: number) => pad(String(Math.min(99, Math.round(n))), 2) + "%"; const rate = (b: number) => pad(b < 1e3 ? "0K" : b < 1e6 ? Math.round(b / 1e3) + "K" : b < 1e7 ? (b / 1e6).toFixed(1) + "M" : Math.round(b / 1e6) + "M", 4); // Stays running: asks Appy for fresh readings every 2 seconds and re-renders the title. export default function Command() { const show = (getPreferenceValues<{ show?: string }>().show || "cpu,mem").split(","); const [title, setTitle] = useState(""); useEffect(() => { let alive = true; const tick = async () => { try { const s: any = await system.stats(); if (!alive) return; const parts: string[] = []; if (show.includes("cpu")) parts.push("CPU " + pct(s.cpu.usage)); if (show.includes("mem")) parts.push("MEM " + pct(s.memory.usedPercent)); if (show.includes("net")) parts.push("↓" + rate(s.network.down) + " ↑" + rate(s.network.up)); if (show.includes("bat") && s.battery) parts.push((s.battery.charging ? "⚡︎" : "") + Math.round(s.battery.percent) + "%"); setTitle(parts.join(" ")); } catch {} }; tick(); const timer = setInterval(tick, 2000); return () => { alive = false; clearInterval(timer); }; }, []); return ; }