about-system 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,152 @@
1
+ /**
2
+ * System status and health information functions
3
+ * @module info/system-status
4
+ */
5
+
6
+ import fs from "fs";
7
+ import os from "os";
8
+ import type { InfoContext } from "../types/internal-types";
9
+ import { IS_LINUX } from "../utils/platform";
10
+ import { execCommand, commandExists } from "../utils/command";
11
+ import { getCachedValue, setCachedValue } from "../cache/cache";
12
+
13
+ /**
14
+ * Gets system load averages
15
+ * Reads 1, 5, and 15 minute load averages from /proc/loadavg (Linux only)
16
+ * @returns Space-separated load averages (1m 5m 15m) or empty string
17
+ * @example "0.52 0.58 0.59", "2.10 1.95 1.88"
18
+ */
19
+ export function load_average(context: InfoContext): string {
20
+ if (!IS_LINUX) return "";
21
+
22
+ try {
23
+ const loadavg = fs.readFileSync("/proc/loadavg", "utf8");
24
+ const loads = loadavg.split(" ").slice(0, 3);
25
+ return loads.join(" ");
26
+ } catch {}
27
+ return "";
28
+ }
29
+
30
+ /**
31
+ * Gets battery charge level and charging status
32
+ * Reads from /sys/class/power_supply/BAT0 (Linux laptops only)
33
+ * @param context - Info context with cache
34
+ * @returns Battery percentage with optional + for charging, or empty string
35
+ * @example "85%", "42%+", "100%"
36
+ */
37
+ export function battery(context: InfoContext): string {
38
+ const cached = getCachedValue(context.cache, "battery");
39
+ if (cached !== null) return cached;
40
+
41
+ if (!IS_LINUX) {
42
+ setCachedValue(context.cache, "battery", "");
43
+ return "";
44
+ }
45
+
46
+ try {
47
+ const batteryPath = "/sys/class/power_supply/BAT0";
48
+ const capacityPath = `${batteryPath}/capacity`;
49
+ const statusPath = `${batteryPath}/status`;
50
+
51
+ if (fs.existsSync(capacityPath)) {
52
+ const capacity = fs.readFileSync(capacityPath, "utf8").trim();
53
+ const status = fs.existsSync(statusPath)
54
+ ? fs.readFileSync(statusPath, "utf8").trim()
55
+ : "Unknown";
56
+
57
+ const batteryPercent = parseInt(capacity);
58
+ const isCharging = status === "Charging";
59
+ const result = `${batteryPercent}%${isCharging ? "+" : ""}`;
60
+ setCachedValue(context.cache, "battery", result);
61
+ return result;
62
+ }
63
+ } catch {}
64
+
65
+ setCachedValue(context.cache, "battery", "");
66
+ return "";
67
+ }
68
+
69
+ /**
70
+ * Gets system temperature in Celsius
71
+ * Reads from thermal zone or hwmon sensors (Linux only)
72
+ * @param context - Info context with cache
73
+ * @returns Temperature with °C suffix or empty string
74
+ * @example "45°C", "62°C"
75
+ */
76
+ export function temperature(context: InfoContext): string {
77
+ const cached = getCachedValue(context.cache, "temperature");
78
+ if (cached !== null) return cached;
79
+
80
+ if (!IS_LINUX) {
81
+ setCachedValue(context.cache, "temperature", "");
82
+ return "";
83
+ }
84
+
85
+ try {
86
+ const tempSources = [
87
+ "/sys/class/thermal/thermal_zone0/temp",
88
+ "/sys/class/hwmon/hwmon0/temp1_input",
89
+ "/sys/class/hwmon/hwmon1/temp1_input",
90
+ ];
91
+
92
+ for (const source of tempSources) {
93
+ if (fs.existsSync(source)) {
94
+ const temp = fs.readFileSync(source, "utf8").trim();
95
+ const tempC = Math.round(parseInt(temp) / 1000);
96
+
97
+ if (tempC > 0 && tempC < 150) {
98
+ const result = `${tempC}°C`;
99
+ setCachedValue(context.cache, "temperature", result);
100
+ return result;
101
+ }
102
+ }
103
+ }
104
+ } catch {}
105
+
106
+ setCachedValue(context.cache, "temperature", "");
107
+ return "";
108
+ }
109
+
110
+ /**
111
+ * Gets count of running system services
112
+ * Uses systemctl or service command (Linux only)
113
+ * @param context - Info context with cache
114
+ * @returns Service count with "services" suffix or empty string
115
+ * @example "125 services", "89 services"
116
+ */
117
+ export function services_running(context: InfoContext): string {
118
+ const cached = getCachedValue(context.cache, "services_running");
119
+ if (cached !== null) return cached;
120
+
121
+ if (!IS_LINUX) {
122
+ setCachedValue(context.cache, "services_running", "");
123
+ return "";
124
+ }
125
+
126
+ try {
127
+ let serviceCount = 0;
128
+
129
+ if (commandExists("systemctl")) {
130
+ const services = execCommand(
131
+ "systemctl list-units --type=service --state=running --no-pager"
132
+ );
133
+ serviceCount = services
134
+ .split("\n")
135
+ .filter((line) => line.includes(".service")).length;
136
+ } else if (commandExists("service")) {
137
+ const services = execCommand("service --status-all");
138
+ serviceCount = services
139
+ .split("\n")
140
+ .filter((line) => line.includes("+")).length;
141
+ }
142
+
143
+ if (serviceCount > 0) {
144
+ const result = `${serviceCount} services`;
145
+ setCachedValue(context.cache, "services_running", result);
146
+ return result;
147
+ }
148
+ } catch {}
149
+
150
+ setCachedValue(context.cache, "services_running", "");
151
+ return "";
152
+ }