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