users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/cpu.jsx
d2b62c9d6f30a0ea88b2a235cde120cd10ba332b
· 4.0 KB · 153 lines
raw
| 1 | import * as Uebersicht from "uebersicht"; |
| 2 | import * as DataWidget from "./data-widget.jsx"; |
| 3 | import * as DataWidgetLoader from "./data-widget-loader.jsx"; |
| 4 | import Graph from "./graph.jsx"; |
| 5 | import * as Icons from "../icons/icons.jsx"; |
| 6 | import useWidgetRefresh from "../../hooks/use-widget-refresh"; |
| 7 | import useServerSocket from "../../hooks/use-server-socket"; |
| 8 | import { useSimpleBarContext } from "../simple-bar-context.jsx"; |
| 9 | import * as Utils from "../../utils"; |
| 10 | |
| 11 | export { cpuStyles as styles } from "../../styles/components/data/cpu"; |
| 12 | |
| 13 | const { React } = Uebersicht; |
| 14 | |
| 15 | const DEFAULT_REFRESH_FREQUENCY = 2000; |
| 16 | const GRAPH_LENGTH = 50; |
| 17 | |
| 18 | /** |
| 19 | * CPU Widget component |
| 20 | * @returns {JSX.Element|null} The CPU widget |
| 21 | */ |
| 22 | export const Widget = React.memo(() => { |
| 23 | const { displayIndex, settings } = useSimpleBarContext(); |
| 24 | const { widgets, cpuWidgetOptions } = settings; |
| 25 | const { cpuWidget } = widgets; |
| 26 | const { |
| 27 | refreshFrequency, |
| 28 | showOnDisplay, |
| 29 | displayAsGraph, |
| 30 | cpuMonitorApp, |
| 31 | showIcon, |
| 32 | cpuUsageThreshold, |
| 33 | } = cpuWidgetOptions; |
| 34 | |
| 35 | // Determine if the widget should be visible based on display settings |
| 36 | const visible = |
| 37 | Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && cpuWidget; |
| 38 | |
| 39 | // Set the refresh frequency for the widget |
| 40 | const refresh = React.useMemo( |
| 41 | () => |
| 42 | Utils.getRefreshFrequency(refreshFrequency, DEFAULT_REFRESH_FREQUENCY), |
| 43 | [refreshFrequency], |
| 44 | ); |
| 45 | |
| 46 | const [graph, setGraph] = React.useState([]); |
| 47 | const [state, setState] = React.useState(); |
| 48 | const [loading, setLoading] = React.useState(visible); |
| 49 | |
| 50 | /** |
| 51 | * Reset the widget state |
| 52 | */ |
| 53 | const resetWidget = () => { |
| 54 | setState(undefined); |
| 55 | setLoading(false); |
| 56 | setGraph([]); |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Fetch CPU usage data |
| 61 | */ |
| 62 | const getCpu = React.useCallback(async () => { |
| 63 | if (!visible) return; |
| 64 | try { |
| 65 | const usage = await Utils.cachedRun( |
| 66 | `top -l 2 | awk '/CPU usage/ && NR > 10 {gsub(/%/, "", $7); print int(100 - $7); exit}'`, |
| 67 | refresh, |
| 68 | ); |
| 69 | const formattedUsage = { usage: parseInt(usage, 10) }; |
| 70 | setState(formattedUsage); |
| 71 | if (displayAsGraph) { |
| 72 | Utils.addToGraphHistory(formattedUsage, setGraph, GRAPH_LENGTH); |
| 73 | } |
| 74 | setLoading(false); |
| 75 | } catch { |
| 76 | setTimeout(getCpu, 1000); |
| 77 | } |
| 78 | }, [displayAsGraph, setGraph, visible, refresh]); |
| 79 | |
| 80 | // Use server socket to fetch CPU data |
| 81 | useServerSocket("cpu", visible, getCpu, resetWidget, setLoading); |
| 82 | // Refresh the widget at the specified interval |
| 83 | useWidgetRefresh(visible, getCpu, refresh); |
| 84 | |
| 85 | if (loading) return <DataWidgetLoader.Widget className="cpu" />; |
| 86 | if (!state) return null; |
| 87 | |
| 88 | const { usage } = state; |
| 89 | const threshold = Number(cpuUsageThreshold) || 0; |
| 90 | const usageValue = Number(usage) || 0; |
| 91 | |
| 92 | if (threshold > 0 && usageValue < threshold) return null; |
| 93 | |
| 94 | // Handle click event to open CPU monitor app |
| 95 | const onClick = |
| 96 | cpuMonitorApp === "None" |
| 97 | ? undefined |
| 98 | : (e) => { |
| 99 | Utils.clickEffect(e); |
| 100 | openCpuUsageApp(cpuMonitorApp); |
| 101 | }; |
| 102 | |
| 103 | if (displayAsGraph) { |
| 104 | return ( |
| 105 | <DataWidget.Widget |
| 106 | classes="cpu cpu--graph" |
| 107 | onClick={onClick} |
| 108 | disableSlider |
| 109 | > |
| 110 | <Graph |
| 111 | className="cpu__graph" |
| 112 | caption={{ |
| 113 | usage: { |
| 114 | value: `${usage}%`, |
| 115 | icon: showIcon ? Icons.CPU : null, |
| 116 | color: "var(--yellow)", |
| 117 | }, |
| 118 | }} |
| 119 | values={graph} |
| 120 | maxLength={GRAPH_LENGTH} |
| 121 | maxValue={100} |
| 122 | /> |
| 123 | </DataWidget.Widget> |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | return ( |
| 128 | <DataWidget.Widget |
| 129 | classes="cpu" |
| 130 | Icon={showIcon ? Icons.CPU : null} |
| 131 | onClick={onClick} |
| 132 | > |
| 133 | <span className="cpu__usage">{usage}%</span> |
| 134 | </DataWidget.Widget> |
| 135 | ); |
| 136 | }); |
| 137 | |
| 138 | Widget.displayName = "Cpu"; |
| 139 | |
| 140 | /** |
| 141 | * Open the specified CPU usage monitoring application |
| 142 | * @param {string} app - The name of the application to open |
| 143 | */ |
| 144 | function openCpuUsageApp(app) { |
| 145 | switch (app) { |
| 146 | case "Activity Monitor": |
| 147 | Uebersicht.run(`open -a "Activity Monitor"`); |
| 148 | break; |
| 149 | case "Top": |
| 150 | Utils.runInUserTerminal("top"); |
| 151 | break; |
| 152 | } |
| 153 | } |