summaryrefslogtreecommitdiff
path: root/services/SystemUsage.qml
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-05-19 21:03:34 +0800
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-05-19 21:03:34 +0800
commit35994f730803a34b67c741c3f21976c31da0e5cd (patch)
treeb97836dd641ea1efa379ba0adfb5ad880067741e /services/SystemUsage.qml
parentdashboard: fix media cover art (diff)
downloadcaelestia-shell-35994f730803a34b67c741c3f21976c31da0e5cd.tar.gz
caelestia-shell-35994f730803a34b67c741c3f21976c31da0e5cd.tar.bz2
caelestia-shell-35994f730803a34b67c741c3f21976c31da0e5cd.zip
dashboard: resources
Diffstat (limited to 'services/SystemUsage.qml')
-rw-r--r--services/SystemUsage.qml84
1 files changed, 84 insertions, 0 deletions
diff --git a/services/SystemUsage.qml b/services/SystemUsage.qml
new file mode 100644
index 0000000..c5503b9
--- /dev/null
+++ b/services/SystemUsage.qml
@@ -0,0 +1,84 @@
+pragma Singleton
+
+import Quickshell
+import Quickshell.Io
+import QtQuick
+
+Singleton {
+ id: root
+
+ property real cpuPerc
+ property int memUsed
+ property int memTotal
+ readonly property real memPerc: memTotal > 0 ? memUsed / memTotal : 0
+ property int storageUsed
+ property int storageTotal
+ property real storagePerc: storageTotal > 0 ? storageUsed / storageTotal : 0
+
+ property int lastCpuIdle
+ property int lastCpuTotal
+
+ Timer {
+ running: true
+ interval: 1000
+ repeat: true
+ onTriggered: {
+ stat.reload();
+ meminfo.reload();
+ storage.running = true;
+ }
+ }
+
+ FileView {
+ id: stat
+
+ path: "/proc/stat"
+ onLoaded: {
+ const data = text().match(/^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/);
+ if (data) {
+ const stats = data.slice(1).map(n => parseInt(n, 10));
+ const total = stats.reduce((a, b) => a + b, 0);
+ const idle = stats[3];
+
+ const totalDiff = total - root.lastCpuTotal;
+ const idleDiff = idle - root.lastCpuIdle;
+ root.cpuPerc = totalDiff > 0 ? (1 - idleDiff / totalDiff) : 0;
+
+ root.lastCpuTotal = total;
+ root.lastCpuIdle = idle;
+ }
+ }
+ }
+
+ FileView {
+ id: meminfo
+
+ path: "/proc/meminfo"
+ onLoaded: {
+ const data = text();
+ root.memTotal = parseInt(data.match(/MemTotal: *(\d+)/)[1], 10) || 1;
+ root.memUsed = (root.memTotal - parseInt(data.match(/MemAvailable: *(\d+)/)[1], 10)) || 0;
+ }
+ }
+
+ Process {
+ id: storage
+
+ running: true
+ command: ["sh", "-c", "df | grep '^/dev/' | awk '{print $3, $4}'"]
+ stdout: SplitParser {
+ splitMarker: ""
+ onRead: data => {
+ let used = 0;
+ let avail = 0;
+ for (const line of data.trim().split("\n")) {
+ const [u, a] = line.split(" ");
+ used += parseInt(u, 10);
+ avail += parseInt(a, 10);
+ }
+ root.storageUsed = used;
+ root.storageTotal = used + avail;
+ }
+ }
+ }
+}