aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/memory.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/memory.jsx')
-rwxr-xr-xusers/ryan/modules/simple-bar/simple-bar-source/lib/components/data/memory.jsx134
1 files changed, 134 insertions, 0 deletions
diff --git a/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/memory.jsx b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/memory.jsx
new file mode 100755
index 0000000..e4361db
--- /dev/null
+++ b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/memory.jsx
@@ -0,0 +1,134 @@
1import * as Uebersicht from "uebersicht";
2import * as DataWidget from "./data-widget.jsx";
3import * as DataWidgetLoader from "./data-widget-loader.jsx";
4import useWidgetRefresh from "../../hooks/use-widget-refresh";
5import useServerSocket from "../../hooks/use-server-socket";
6import { useSimpleBarContext } from "../simple-bar-context.jsx";
7import * as Utils from "../../utils";
8
9export { memoryStyles as styles } from "../../styles/components/data/memory";
10
11const { React } = Uebersicht;
12
13const DEFAULT_REFRESH_FREQUENCY = 4000;
14
15/**
16 * Memory Widget component
17 * @returns {JSX.Element|null} The memory widget component
18 */
19export const Widget = () => {
20 const { displayIndex, settings } = useSimpleBarContext();
21 const { widgets, memoryWidgetOptions } = settings;
22 const { memoryWidget } = widgets;
23 const {
24 refreshFrequency,
25 showOnDisplay,
26 memoryMonitorApp,
27 showIcon,
28 memoryUsageThreshold,
29 } = memoryWidgetOptions;
30
31 // Determine the refresh frequency for the widget
32 const refresh = React.useMemo(
33 () =>
34 Utils.getRefreshFrequency(refreshFrequency, DEFAULT_REFRESH_FREQUENCY),
35 [refreshFrequency],
36 );
37
38 // Determine if the widget should be visible
39 const visible =
40 Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && memoryWidget;
41
42 const [state, setState] = Uebersicht.React.useState();
43 const [loading, setLoading] = Uebersicht.React.useState(visible);
44
45 /**
46 * Reset the widget state
47 */
48 const resetWidget = () => {
49 setState(undefined);
50 setLoading(false);
51 };
52
53 /**
54 * Fetch memory usage data
55 */
56 const getMemory = React.useCallback(async () => {
57 const output = await Utils.cachedRun(
58 'vm_stat | awk \'BEGIN {page_size=4096} /page size of/ {page_size=$8} /Pages free/ {free=$3} /Pages inactive/ {inactive=$3} /Pages speculative/ {spec=$4} /Pages active/ {active=$3} /Pages wired/ {wired=$4} END {gsub(/\\./, "", free); gsub(/\\./, "", inactive); gsub(/\\./, "", spec); gsub(/\\./, "", active); gsub(/\\./, "", wired); available=free+inactive+spec; total=available+active+wired; printf "%.0f", (available/total)*100}\'',
59 refresh,
60 );
61 const free = parseInt(Utils.cleanupOutput(output), 10);
62 setState({ free });
63 setLoading(false);
64 }, [setLoading, setState, refresh]);
65
66 // Use server socket to get memory data
67 useServerSocket("memory", visible, getMemory, resetWidget, setLoading);
68 // Refresh the widget at the specified interval
69 useWidgetRefresh(visible, getMemory, refresh);
70
71 if (loading) return <DataWidgetLoader.Widget className="memory" />;
72 if (!state) return null;
73
74 const { free } = state;
75 const used = 100 - free;
76 const threshold = Number(memoryUsageThreshold) || 0;
77
78 if (threshold > 0 && used < threshold) return null;
79
80 // Handle click event to open memory usage app
81 const onClick =
82 memoryMonitorApp === "None"
83 ? undefined
84 : (e) => {
85 Utils.clickEffect(e);
86 openMemoryUsageApp(memoryMonitorApp);
87 };
88
89 /**
90 * Pie chart component for memory usage
91 * @returns {JSX.Element} The pie chart component
92 */
93 const Pie = () => {
94 return (
95 <div
96 className="memory__pie"
97 style={{
98 backgroundImage: `conic-gradient(var(--pie-color) ${used}%, var(--main-alt) ${used}% 100%)`,
99 }}
100 />
101 );
102 };
103
104 const classes = Utils.classNames("memory", {
105 "memory--low": used <= 30,
106 "memory--medium": used > 30 && used <= 70,
107 "memory--high": used > 70,
108 });
109
110 return (
111 <DataWidget.Widget
112 classes={classes}
113 Icon={showIcon ? Pie : null}
114 onClick={onClick}
115 >
116 <div className="memory__content">{used}%</div>
117 </DataWidget.Widget>
118 );
119};
120
121/**
122 * Open the specified memory usage application
123 * @param {string} app - The name of the application to open
124 */
125function openMemoryUsageApp(app) {
126 switch (app) {
127 case "Activity Monitor":
128 Uebersicht.run(`open -a "Activity Monitor"`);
129 break;
130 case "Top":
131 Utils.runInUserTerminal("top");
132 break;
133 }
134}